A square triple (a,b,c)
is a triple where a
, b
, and c
are integers and a2 + b2 = c2
.
Given an integer n
, return the number of square triples such that 1 <= a, b, c <= n
.
Example 1:
Input: n = 5 Output: 2 Explanation: The square triples are (3,4,5) and (4,3,5).
Example 2:
Input: n = 10 Output: 4 Explanation: The square triples are (3,4,5), (4,3,5), (6,8,10), and (8,6,10).
Constraints:
1 <= n <= 250
program main
implicit none
integer :: n
integer :: result
print '(A)', 'Example 1:'
n = 5
result = countSquareTriples(n)
print '(I0)', result
print '(A)', ''
print '(A)', 'Example 2:'
n = 10
result = countSquareTriples(n)
print '(I0)', result
print '(A)', ''
contains
function countSquareTriples(n) result(count)
implicit none
integer, intent(in) :: n
integer :: count
integer :: i, j, k
count = 0
do i = 1, n
do j = i, n
k = sqrt(i**2 + j**2)
if (k == int(k)) then
count = count + 1
end if
end do
end do
end function countSquareTriples
end program main
temp.f95:31:25: 31 | k = sqrt(i**2 + j**2) | 1 Error: ‘x’ argument of ‘sqrt’ intrinsic at (1) must be REAL or COMPLEX
```
program square_triples
implicit none
integer :: n, a, b, c, count
! Read input
read *, n
! Initialize count
count = 0
! Loop over all possible values of a, b, and c
do a = 1, n
do b = 1, n
do c = 1, n
! Check if the triple is a square triple
if (a**2 + b**2 == c**2) then
! Increment count
count = count + 1
end if
end do
end do
end do
! Print output
write (*,*) count
end program square_triples
```
temp.f95:1:2: 1 | ``` | 1 Error: Invalid character in name at (1) temp.f95:29:2: 29 | ``` | 1 Error: Invalid character in name at (1)
def reverse(x):
rev = 0
while x != 0:
rev = rev * 10 + x % 10
x = x // 10
return rev
def count_nice_pairs(nums):
MOD = 1_000_000_007
count = {}
result = 0
for num in nums:
diff = num - reverse(num)
count[diff] = count.get(diff, 0) + 1
for val in count.values():
result = (result + ((val * (val - 1)) // 2) % MOD) % MOD
return result
int reverse(int x) {
int rev = 0;
while (x != 0) {
rev = rev * 10 + x % 10;
x = x / 10;
}
return rev;
}
int countNicePairs(vector<int>& nums) {
const int MOD = 1e9 + 7;
unordered_map<int, int> count;
int result = 0;
for (int num : nums) {
int diff = num - reverse(num);
count[diff]++;
}
for (auto &[_, val] : count) {
result = (result + ((val * (val - 1LL)) / 2) % MOD) % MOD;
}
return result;
}