문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음
square triple(a, b, c)는 a, b, c는 정수이고 a^2 + b^2 = c^2이다.
정수 n이 주어졌을때, 1 <= a, b, c <= n을 만족하는 square triples의 개수를 반환해라.
#1
Input: n = 5
Output: 2
Explanation: square triples는 (3, 4, 5), (4, 3, 5)이다.
#2
Input: n = 10
Output: 4
Explanation: square triples는 (3, 4, 5), (4, 3, 5), (6, 8, 10), (8, 6, 10)이다.
class Solution {
public int countTriples(int n) {
int count = 0;
for(int i = 3; i <= 3 * n / 4; i++){
for(int j = i + 1; j < n; j++){
int sum = i * i + j * j;
int num = (int) Math.sqrt(sum);
if(num <= n){
if(num * num == sum){
count += 2;
}
}else{
break;
}
}
}
return count;
}
}