Count Square Sum Triples

🏠 ⬅️ ➡️

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

Note: This problem is from LeetCode.
Compiled
Executed
Correct
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
Compiled
Executed
Correct
```
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
```
🌐 Data from online sources
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
  1. Define a helper function 'reverse' to find the reverse of a number.
  2. Initialize a dictionary 'count' to store the difference between the number and its reverse and their count in the nums array.
  3. Initialize a result variable.
  4. Traverse the nums array, calculate the difference between num and its reverse, and update the count.
  5. Traverse the count dictionary's values, and for each value, add ((value * (value - 1)) / 2) % MOD to the current result, and take the modulus by 1e9 + 7.
  6. Return the result as the final count of nice pairs.
🌐 Data from online sources
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;
}
  1. Define a helper function 'reverse' to find the reverse of a number.
  2. Initialize a dictionary 'count' to store the difference between the number and its reverse and their count in the nums array.
  3. Initialize a result variable.
  4. Traverse the nums array, calculate the difference between num and its reverse, and update the count.
  5. Traverse the count dictionary's values, and for each value, add ((value * (value - 1)) / 2) % MOD to the current result, and take the modulus by 1e9 + 7.
  6. Return the result as the final count of nice pairs.