Challenges

+8
−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?

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+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

# 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 # Ruby, 16 bytes ->n{n**0.5%1==0}  Try it online! Why does this post require moderator attention? You might want to add some details to your flag. #### 0 comment threads +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 # 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 +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 +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 +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?

Does nothing (1 comment)
+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?