Challenges

+9
−0

# 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 or false, 1 or 0) - 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
Why does this post require moderator attention?
Why should this post be closed?

+5
−0

# Python 3, 20 19 bytes

lambda n:n**.5%1==0


-1 byte thanks to @shaggy : 0.5 -> .5

Try it online!

Why does this post require moderator attention?

+4
−0

# Ruby, 16 bytes

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


Try it online!

Why does this post require moderator attention?

+4
−0

# APL (Dyalog Extended), 3 bytes

√∊…


Try it online!

Is the square root in the range?

Why does this post require moderator attention?

+4
−0

Probably the optimal solution. Credits go to nimi from PPCG.

f n=elem n$map(^2)[0..n]  Try it online! ### My original solution, 25 bytes f a=any((==a).(^2))[0..a]  Try it online! Why does this post require moderator attention? You might want to add some details to your flag. #### 1 comment thread +3 −0 # Japt, 6 3 bytes Cut in half thanks to Shaggy! ¬v1  Try it online! Golfed thanks to Shaggy's interpreter auto-golf feature. ¬v1 ¬ //Square root v1 //Is that an integer?  Why does this post require moderator attention? You might want to add some details to your flag. #### 1 comment thread You can reduce that by half (7 comments) +3 −0 # Prolog (SWI), 42 bytes R-S:-S is R*R;R<S,M is R+1,M-S. f(S):-0-S.  Try it online! Why does this post require moderator attention? You might want to add some details to your flag. #### 0 comment threads +2 −0 # Vyxal, 2 bytes ∆²  Try it Online! Woooooooo built-in elements ftw. Why does this post require moderator attention? You might want to add some details to your flag. #### 1 comment thread General (1 comment) +2 −0 # Husk, 5 bytes ±£Θİ□  Try it online! similar to haskell, checks if the number is in the infinite list of squares. Why does this post require moderator attention? You might want to add some details to your flag. #### 1 comment thread +2 −0 # Python 3, 46 bytes Saved 8 bytes thanks to Shaggy! lambda n:[n-i*i or exit(1)for i in range(1+n)]  Try it online! # Python 3.8 (pre-release), 5054 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)  Try it online! Here's a stupid solution that outputs using the exit code (1 if it's a perfect square, 0 otherwise). Why does this post require moderator attention? You might want to add some details to your flag. #### 2 comment threads Incorrect output (6 comments) Cheaper to set x outside the loop (2 comments) +2 −0 # C (gcc), 57 64 bytes x,s;main(){scanf("%d",&x);s=sqrt(x);printf("%d",s*s==x);}  Try it online! Why does this post require moderator attention? You might want to add some details to your flag. #### 1 comment thread +1 −0 # C (gcc), 40 bytes i;f(n){for(i=1;n>0;n-=i,i+=2);return!n;}  Try it online! Why does this post require moderator attention? You might want to add some details to your flag. #### 0 comment threads +1 −0 # PHP, 69 37 bytes <?=($y=sqrt($x=fgets(STDIN)))*$y==$x;  Try it online! Golfed 32 bytes thanks to @Shaggy's advice. Why does this post require moderator attention? You might want to add some details to your flag. #### 1 comment thread Does nothing (1 comment) +1 −0 # C (clang), 189181176 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");}}}  Try it online! If libraries also count in the code, the total byte count will be 226 218 213 210. Why does this post require moderator attention? You might want to add some details to your flag. #### 1 comment thread +1 −0 ## Sclipting, (UTF-16) 8 bytes 根❶圜同  Takes the square root, pushes a copy, rounds the copy, and compares. Basically, checks that the square root is an integer. Why does this post require moderator attention? You might want to add some details to your flag. #### 1 comment thread +0 −0 # dc, 9 bytes _?dvd*-^p  Try it online! Comparisons are expensive in dc, so you have to get a bit creative. I came up with$0^{n-\lfloor\sqrt{n}\rfloor^2}\$.

Why does this post require moderator attention?

+0
−0

## 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
Why does this post require moderator attention?

+0
−0

# C (gcc), 37 bytes

f(n,p){while(++p*p<n);return p*p==n;}


Try it online!

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$$

Why does this post require moderator attention?

+0
−0

# J, 6 bytes

0=1|%:


Try it online!

Why does this post require moderator attention?

+0
−0

# Lua 5.4, 9 bytes

n^.5%1==0


Try it online!

The entire compressed code is 33 bytes:

function f(n)return n^.5%1==0 end


Try it here.

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.

Why does this post require moderator attention?

+0
−0

# JavaScript (V8), 13 bytes

x=>x**.5%1==0


Try it online!

Why does this post require moderator attention?