# Challenge

So, um, it seems that, while we have plenty of challenges that work with square numbers or numbers of other shapes, we don't have one that simply asks:

Given an integer n (where n>=0) as input return a truthy value if n is a perfect square or a falsey value if not.

## Rules

• You may take input by any reasonable, convenient means as long as it's permitted by standard I/O rules.
• You need not handle inputs greater than what your chosen language can natively handle nor which would lead to floating point inaccuracies.
• Output should be one of two consistent truthy/falsey values (e.g., true or false, 1 or 0) - truthy if the input is a perfect square, falsey if it's not.
• This is so lowest byte count wins.

## Test Cases

Input:  0
Output: true

Input:  1
Output: true

Input:  64
Output: true

Input:  88
Output: false

Input:  2147483647
Output: false

• @Neil I realized my mistake. I retract that suggestion, and instead offer 18014398509481982 (2**54-2), which is representable with a double, and causes answers that use sqrt to fail. – Mego Jun 8 '17 at 12:24
• @Mego I'm probably wrong or just misunderstanding what you're saying, but I'm sure 2**54-2 is still larger than a double can safely handle, at least in JavaScript 18014398509481982 > 9007199254740991 – Tom Jun 8 '17 at 12:32
• @Mego I think the limiting value is 9007199515875288. It's not the square of 94906267, because that's not representable in a double, but if you take its square root, then you get that integer as the result. – Neil Jun 8 '17 at 12:33
• @Tom Type 2**54-2 into a JS console, and compare what you get with 18014398509481982 (the exact value). JS outputs the exact value, therefore 2**54-2 is representable with a double. If that still doesn't convince you, take the binary data 0100001101001111111111111111111111111111111111111111111111111111, interpret it as a IEEE-754 double-precision float, and see what value you get. – Mego Jun 8 '17 at 12:41
• Sorry, guys, stepped away for lunch and ... well, that escalated! And there I thought this would be a nice, simple challenge! Would adding a rule that you need not handle inputs that lead to floating point inaccuracies in your chosen language cover it? – Shaggy Jun 8 '17 at 13:17

# 05AB1E, 2 bytes

Å²


Try it online!

A built-in function that checks if a number is a perfect square.

New contributor
Makonede is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

# J, 8 bytes

(=~<.)%:


Explanation:

• %: square root
• =~ is the argument equal to itself
• <. floor of
• =~<. a J hook, which modifies the right argument by applying <.
• so, "is the floor of the square root equal to itself?"

Note: If we want to save the above to a variable as a verb, we must do, eg:

issq=.(=~<.)@%:

• Could you add a TIO (or equivalent) to this, please? – Shaggy Jun 9 '17 at 8:19
• I tried it, but it's not producing any output. Not sure if the J interpreter is down or if I'm missing something, but if you paste the following test into jconsole: (=~<.)%: 1 4 9 16 ,: 2 3 7 21 it will return a table with a row of 1s on top of a row of 0s, as expected. I tried putting the same into the "Code" section of TIO and then hitting run, but I get no output. – Jonah Jun 9 '17 at 18:24

# Python 2, 33 bytes

 g = lambda x: (x**.5).is_integer()

• Welcome to PPCG! There seems to be a bit of unnecessary whitespace in your entry, such as after the colon, and also anonymous lambdas are valid submissions, meaning you can omit the g= from the start. Also I believe %1!=0 is shorter than .is_integer() – Skidsdev Jun 15 '17 at 7:57
• @Mayube You probably meant ==0? – Martin Ender Jun 15 '17 at 8:04
• @MartinEnder I did, yes – Skidsdev Jun 15 '17 at 8:48

# Python 2, 25 bytes

lambda n:int(n**.5)**2==n


Try it online!

# Ruby, 16 bytes

->n{n**0.5%1==0}


Trivial solution.

# C++, 43 bytes

bool c(int n){int t=sqrt(n);return t*t==n;}


New version: https://repl.it/repls/CompatibleJumpyCoordinates

If your number is perfect square then it will return 1, otherwise it will return 0;

Old version: https://repl.it/repls/WateryTroubledProjects (not valid because it is the snippet only by @Jonathan Frech)

If your number is perfect square then it will be exited with status 1, otherwise it will not exist;

• Note that in its current form, this answer would be regarded as a snippet, which is not allowed. Please fix your answer to be of a valid form, i.e. a full program or function. – Jonathan Frech Mar 3 '19 at 15:43
• @JonathanFrech, thank you for your comment, can I add only the function like my answer above? – Chau Giang Mar 3 '19 at 16:17
• Yes, in its current form your answer appears to be valid. – Jonathan Frech Mar 3 '19 at 18:31

# BFASM, 204 bytes

The function is taking input in r2 and outputs its value into r1.

lbl 1
mov r3,1
lbl 2
mov r4,r3
mul r4,r3
le_ r4,r2
jz_ r4,3
mov r4,r2
mod r4,r3
jz_ r4,4
lbl 5
inc r3
jmp 2
lbl 4
mov r4,r2
div r4,r3
eq_ r4,r3
jz_ r4,5
mov r1,1
ret
lbl 3
clr r1
ret

• I think you may have posted this to the wrong challenge. – Shaggy Aug 27 '19 at 9:48
• @Shaggy why did I? – Kamila Szewczyk Aug 27 '19 at 9:48
• The challenge is about determining whether a number is a perfect square or not, which neither of your solutions appears to do. – Shaggy Aug 27 '19 at 9:51
• @Shaggy I'm streteching the I/O rules a bit, because it's inpractical as heck to read a number in Brainfuck, therefore I perform a single read. There are some integer based brainfuck interpreters so theoretically this solution is valid. – Kamila Szewczyk Aug 27 '19 at 9:52
• Unless we have a consensus on this input format, I'm going to say it's not valid for this challenge as the spec explicitly states that you will be given an integer as input. – Shaggy Aug 27 '19 at 9:55

# Rockstar, 55 bytes

First time posting a solution to one of my own challenges, if you'll forgive me the indulgence.

Outputs 1 for truthy or nothing for falsey.

listen to N
X's0
while N-X
let X be+1
if X*X is N
say 1


Try it here (Code will need to be pasted in)