It's Hip to be Square
Challenge
A catalogue type challenge 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.
- 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
orfalse
,1
or0
) - truthy if the input is a perfect square, falsey if it's not. - This is code-golf 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
[Python 3], 20 19 bytes …
3y ago
[Ruby], 16 bytes ```ruby - …
3y ago
[APL (Dyalog Extended)], 3 byt …
3y ago
[Haskell], 24 bytes Probabl …
3y ago
[Prolog (SWI)], 42 bytes …
3y ago
Japt, 6 3 bytes Cut in half …
3y ago
[Python 3], 46 bytes Saved …
3y ago
[Husk], 5 bytes ±£Θİ□ …
3y ago
[C (gcc)], 57 64 bytes …
3y ago
Vyxal, 2 bytes ``` ∆² ``` …
3y ago
[C (gcc)], 40 bytes …
3y ago
[PHP], 69 37 bytes …
3y ago
Sclipting, (UTF-16) 8 bytes …
3y ago
[C (clang)], 189 181 176 173 b …
3y ago
C (gcc), 37 bytes ``` C f( …
2y ago
Lua 5.4, 9 bytes ``` lua n …
2y ago
JavaScript (V8), 13 bytes ` …
2y ago
Embed ESCR, 28 characters [ …
3y ago
[dc], 9 bytes ?dvd-^p …
3y ago
J, 6 bytes ```J 0=1|%: `` …
3y ago
20 answers
Japt, 6 3 bytes
Cut in half thanks to Shaggy!
¬v1
Golfed thanks to Shaggy's interpreter auto-golf feature.
¬v1
¬ //Square root
v1 //Is that an integer?
Python 3, 46 bytes
Saved 8 bytes thanks to Shaggy!
lambda n:[n-i*i or exit(1)for i in range(1+n)]
Python 3.8 (pre-release), 50 54 52 bytes
Fixed a silly mistake thanks to Shaggy and saved 2 bytes!
for i in range(1+(x:=int(input()))):x-i*i or exit(1)
Here's a stupid solution that outputs using the exit code (1 if it's a perfect square, 0 otherwise).
C (clang), 189 181 176 173 bytes
i,n,x,y;main(){scanf("%i",&x);y=1,n=10001;int z[n];for(i=0;i<n;i++,y++){z[i]=pow(y,2);}for(i=0;i<n;i++){if(x==z[i]){puts("True");break;}if(x!=z[i]&&i==n-1){puts("False");}}}
If libraries also count in the code, the total byte count will be 226 218 213 210.
1 comment thread
dc, 9 bytes
_?dvd*-^p
Comparisons are expensive in dc, so you have to get a bit creative. I came up with $0^{n-\lfloor\sqrt{n}\rfloor^2}$.
0 comment threads
C (gcc), 37 bytes
f(n,p){while(++p*p<n);return p*p==n;}
The solution is based on the simple fact that:
$$\forall n \in \mathbb{N}, n \text{ is a perfect square} \Longleftrightarrow \exists p \in \mathbb{N}, p \le n \text{ / } p^2=n$$
0 comment threads
Embed ESCR, 28 characters
[= [exp [rnd [sqrt n]] 2] n]
The number to test is in N. There are 4 nested functions. From inner to outer: SQRT takes the square root of N. This produces a floating point result. RND rounds that to the nearest integer. EXP is used to raise that integer to the power of 2 (square it). This produces an integer since both inputs are integers. "=" compares the squared result to N. The result is a boolean value, which is either "TRUE" or "FALSE" in its text representation.
Here is a complete program illustrating the above applied to input values 0 to 20:
loop with n from 0 to 20 show n ": " [= [exp [rnd [sqrt n]] 2] n] endloop
Its output is:
0: TRUE 1: TRUE 2: FALSE 3: FALSE 4: TRUE 5: FALSE 6: FALSE 7: FALSE 8: FALSE 9: TRUE 10: FALSE 11: FALSE 12: FALSE 13: FALSE 14: FALSE 15: FALSE 16: TRUE 17: FALSE 18: FALSE 19: FALSE 20: FALSE
0 comment threads
Lua 5.4, 9 bytes
n^.5%1==0
The entire compressed code is 33 bytes:
function f(n)return n^.5%1==0 end
It's not clear whether the rules allow the first form (i.e. only the relevant expression) or the second form (i.e. the entire piece of code that does the check). I see some use the former, some use the latter.
0 comment threads