All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g1901_2000.s1925_count_square_sum_triples.Solution Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g1901_2000.s1925_count_square_sum_triples;

// #Easy #Math #Enumeration #2022_05_14_Time_25_ms_(48.78%)_Space_41.3_MB_(41.26%)

/**
 * 1925 - Count Square Sum Triples\.
 *
 * Easy
 *
 * 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`
**/
public class Solution {
    public int countTriples(int n) {
        int count = 0;
        for (int i = 1; i < n; i++) {
            for (int j = 1; j < n; j++) {
                int product = i * i + j * j;
                double sq = Math.sqrt(product);
                if (sq <= n && (sq - Math.floor(sq) == 0)) {
                    count++;
                }
            }
        }
        return count;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy