Created
December 8, 2025 17:16
-
-
Save tatsuyax25/4792ef379f6906ae3e4bfce04b2d7ed4 to your computer and use it in GitHub Desktop.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number} n | |
| * @return {number} | |
| */ | |
| var countTriples = function(n) { | |
| // Initialize a counter to keep track of valid triples | |
| let count = 0; | |
| // Loop through all possible values of 'a' | |
| for (let a = 1; a <= n; a++) { | |
| // Loop through all possible values of 'b' | |
| for (let b = 1; b <= n; b++) { | |
| // Compute a^2 + b^2 | |
| let sum = a * a + b * b; | |
| // Take the square root to see if it's a perfect square | |
| let c = Math.sqrt(sum); | |
| // Check two conditions: | |
| // 1. c must be an integer (so sum is a perfect square) | |
| // 2. c must be within the range [1, n] | |
| if (Number.isInteger(c) && c <= n) { | |
| // If both conditions are satisfied, we found a valid triple (a, b, c) | |
| count++; | |
| } | |
| } | |
| } | |
| // Return the total number of valid triples | |
| return count; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment