
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Is a number sum of two perfect squares in JavaScript
Perfect Square Numbers:
A natural number in mathematics is called a perfect square if it can be obtained by multiplying any other natural number into that very number.
For instance, 9, 16, 81, 289 are all perfect squares.
We are required to write a JavaScript function that takes in a natural number, say num, as the only argument. The function should determine whether there exists two such number m and n such that −
(m * m) + (n * n) = num
If there exists such numbers, our function should return true, false otherwise.
For example −
If the input number is −
const num = 389;
Then the output should be −
const output = true;
because 389 = (17 * 17) + (10 * 10)
Example
The code for this will be −
const num = 389; const canSumSquares = (num = 2) => { let left = 0, right = Math.floor(Math.sqrt(num)); while(left <= right){ if (left * left + right * right === num) { return true; } else if (left * left + right * right < num) { left++; } else { right--; }; }; return false; }; console.log(canSumSquares(num));
Output
And the output in the console will be −
true
- Related Articles
- Smallest number of perfect squares that sums up to n in JavaScript
- Perfect Squares in C++
- Square pyramidal number (Sum of Squares)
- Program to count number of perfect squares are added up to form a number in C++
- Python Program to Find all Numbers in a Range which are Perfect Squares and Sum of all Digits in the Number is Less than 10
- Find two consecutive positive integers, sum of whose squares is 365.
- Sum of perimeter of all the squares in a rectangle using JavaScript
- Find the greatest number of two digits which is a perfect square.
- Find two consecutive odd positive integers, sum of whose squares is 970.
- Sum of the areas of two squares is $400\ cm^2$. If the difference of their perimeters is 16 cm, find the sides of two squares.
- Python program to filter perfect squares in a given series
- Sum of the areas of two squares is $640\ m^2$. If the difference of their perimeter is 64 m, find the sides of the two squares.
- Sum of the areas of two squares is 400 cm$^{2}$. If the difference of their perimeter is 16 cm, Find the sides of the two squares.
- Sum of the areas of two squares is $468\ m^2$. If the difference of their perimeters is 24 m, find the sides of the two squares.
- What is a Perfect number?

Advertisements