Perfect square is an element of algebraic structure which is equal to the square (algebra) of another element.
Questions tagged [perfect-square]
87 questions
1601
votes
37 answers
Fastest way to determine if an integer's square root is an integer
I'm looking for the fastest way to determine if a long value is a perfect square (i.e. its square root is another integer):
I've done it the easy way, by using the built-in Math.sqrt()
function, but I'm wondering if there is a way to do it faster…

Kip
- 107,154
- 87
- 232
- 265
114
votes
26 answers
Check if a number is a perfect square
How could I check if a number is a perfect square?
Speed is of no concern, for now, just working.
See also: Integer square root in python.
delete
97
votes
3 answers
What's a good algorithm to determine if an input is a perfect square?
Possible Duplicate:
Fastest way to determine if an integer's square root is an integer
What's a way to see if a number is a perfect square?
bool IsPerfectSquare(long input)
{
// TODO
}
I'm using C# but this is language agnostic.
Bonus points…

Michael Haren
- 105,752
- 40
- 168
- 205
32
votes
4 answers
How to square all the values in a vector in R?
I would like to square every value in data, and I am thinking about using a for loop like this:
data = rnorm(100, mean=0, sd=1)
Newdata = {L = NULL; for (i in data) {i = i*i} L = i return (L)}

user3230065
- 321
- 1
- 3
- 3
16
votes
7 answers
What's the best way in JavaScript to test if a given parameter is a square number?
I created a function that will test to see if a given parameter is a square number.
Read about square numbers here: https://en.wikipedia.org/?title=Square_number
If the number is a square number, it returns true and otherwise false. Negative…

Marco V
- 2,553
- 8
- 36
- 58
12
votes
1 answer
Perfect square or not?
This is a code to check if a number is perfect square or not. Why does it work ?
static bool IsSquare(int n)
{
int i = 1;
for (; ; )
{
if (n < 0)
return false;
if (n == 0)
return true;
n -=…

Kaushal
- 143
- 1
- 1
- 6
11
votes
1 answer
How to square each element of an array in Array class in Ruby?
Part of my code is as follows:
class Array
def square!
self.map {|num| num ** 2}
self
end
end
When I call:
[1,2,3].square!
I expect to get [1,4,9], but instead I get [1,2,3]. Why is this the case? When I call:
[1,2,3].map {|num| num **…

mrdziuban
- 749
- 3
- 11
- 23
9
votes
1 answer
How to check if an integer is a perfect square
How could I write an if-then statement that checks if an inputted integer is a perfect square or not (i.e. if I took the square root, it would be an integer as well: 4, 9, 16, 25, 36, etc.) in DrJava? Thank you!

Brooke
- 103
- 1
- 1
- 3
7
votes
2 answers
perfect square algorithm - explanation for the implementation
This question is a follow-up on the post here: Fastest way to determine if an integer's square root is an integer, What's a good algorithm to determine if an input is a perfect square?.
One of the posts there had this solution to find if a given…

brain storm
- 30,124
- 69
- 225
- 393
6
votes
4 answers
List of numbers whose squares are the sum of two squares
I've just started learning Python and have started doing some problems just to help buid my skills however I am pretty stuck on this question.
Make a list containing all positive integers up to 1000 whose squares can be expressed as a sum of two…

Dizzle
- 63
- 1
- 4
5
votes
2 answers
Combinations of three positive numbers x, y, z so that x + y, x - y, y + z, y - z, x + z and x - z are perfect squares
Good morning, I'm new here and I bring a small problem. I'm having trouble develop efficient an algorithm for the following problem:
I need to find combinations of three positive numbers x, y and z so that x + y, x - y, y + z, y - z, x + z and x - z…

user2156850
- 51
- 1
- 5
5
votes
12 answers
Can I rely on this to judge a square number in C++?
Can I rely on
sqrt((float)a)*sqrt((float)a)==a
or
(int)sqrt((float)a)*(int)sqrt((float)a)==a
to check whether a number is a perfect square? Why or why not?
int a is the number to be judged. I'm using Visual Studio 2005.
Edit: Thanks for all…
phoenie
4
votes
3 answers
How to find perfect squares in a range efficiently when the inputs are large numbers in Python
The question is how to find perfect squares in a given range efficiently when the inputs are very large numbers. My solution is giving Time Limit Exceeded error. I have already checked the following links, but they didn't solve my problem:
- Python…

titan7585
- 300
- 1
- 5
- 9
4
votes
5 answers
formula for finding a square of a number
I need to display the squares of the numbers 1-10 using a for loop. This is what I have so far. I don't know what I am missing. Any help would be much appreciated.
for (int counter = 1; counter <= 10; counter++)
{ …

Vinnie Centofanti
- 43
- 1
- 1
- 5
3
votes
1 answer
Compute pairs from two lists that when multiplied make a perfect square
You are given two lists and you have to find out the pairs which make a perfect square.
For example in:
a = [2, 6, 10, 13, 17, 18]
b = [3, 7, 8, 9, 11, 15]
There are two pairs (2,8) and (8,18).
Is there any method efficient than the…