JavaScript: Sum of two square numbers equal to an integer
JavaScript Math: Exercise-110 with Solution
Sum of Two Squares Equals Integer
Write a JavaScript program to check whether the sum of two square integers is equal to another given integer.
For example a2 + b2 = c where c is the given integer.
Example: 13 = 22 + 32
Test Data:
(2) -> true
(5) -> true
(13) -> true
(15) -> false
Sample Solution:
Solution-1
JavaScript Code:
Output:
n = 2 Check sum of two square integers is equal to said integer: true n = 5 Check sum of two square integers is equal to said integer: true n = 13 Check sum of two square integers is equal to said integer: true n = 15 Check sum of two square integers is equal to said integer: false
Flowchart:

Live Demo:
Solution-2
JavaScript Code:
Output:
n = 2 Check sum of two square integers is equal to said integer: true n = 5 Check sum of two square integers is equal to said integer: true n = 13 Check sum of two square integers is equal to said integer: true n = 15 Check sum of two square integers is equal to said integer: false
Flowchart:

Live Demo:
For more Practice: Solve these Related Problems:
- Write a JavaScript function that checks if a given integer can be represented as the sum of two square numbers.
- Write a JavaScript function that iterates through possible square numbers and tests if any two add up to the given integer.
- Write a JavaScript function that uses nested loops to examine combinations of squares up to the square root of the integer.
- Write a JavaScript function that validates the input and returns a boolean indicating if the sum of two squares equals the integer.
Improve this sample solution and post your code through Disqus.
Previous: Count all numbers with unique digits in a range.
Next: Kth smallest number in a multiplication table.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.