코딩 테스트/Level 1
25. 정수 제곱근 판별
컴닥
2019. 10. 23. 23:20
반응형
https://programmers.co.kr/learn/courses/30/lessons/12934
파이썬
def solution(n):
x = n**0.5
return -1 if (x % 1) else (x+1)**2
자바스크립트
function solution(n) {
let num = Math.sqrt(n)
return (num == parseInt(num)) ? (num + 1) ** 2 : -1
}
자바
class Solution {
public long solution(long n) {
var a = (long) Math.sqrt(n);
return (a * a == n) ? (a + 1) * (a + 1): -1;
}
}
고
import "math"
func solution(n int64) int64 {
var x = int64(math.Sqrt(float64(n)))
if x*x == n {
return (x + 1) * (x + 1)
}
return -1
}
코틀린
import kotlin.math.sqrt
class Solution {
fun solution(n: Long): Long {
val a = sqrt(n.toDouble()).toLong()
return if (a * a == n) (a + 1) * (a + 1) else -1
}
}
C#
using System;
public class Solution
{
public long solution(long n)
{
long temp = (long)Math.Sqrt((double)n);
return (temp * temp == n) ? (temp + 1) * (temp + 1) : -1;
}
}
반응형