Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

It's Hip to be Square

+10
−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
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

20 answers

+5
−0

Python 3, 20 19 bytes

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

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

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Welcome to Code Golf :) You can save a byte with `.5` instead of `0.5`. (2 comments)
+4
−0

Haskell, 24 bytes

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!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

You should be able to get this down to 24 by mapping and squaring the range and then checking if it c... (3 comments)
+4
−0

Ruby, 16 bytes

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

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+4
−0

APL (Dyalog Extended), 3 bytes

√∊…

Try it online!

Is the square root in the range?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Fails for `0`, which should be truthy. I think you'll need `√∊0,⍳`, unless APL has a 0-based range bu... (3 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!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+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?
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

You can reduce that by half (7 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!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Strictly speaking, both outputs here are truthy as they're both non-empty strings. (4 comments)
+2
−0

Vyxal, 2 bytes

∆²

Try it Online!

Woooooooo built-in elements ftw.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General (1 comment)
+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), 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)

Try it online!

Here's a stupid solution that outputs using the exit code (1 if it's a perfect square, 0 otherwise).

History
Why does this post require attention from curators or moderators?
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

Husk, 5 bytes

±£Θİ□

Try it online!

similar to haskell, checks if the number is in the infinite list of squares.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

This fails for `0`, which should return a truthy value. (4 comments)
+1
−0

PHP, 69 37 bytes

<?=($y=sqrt($x=fgets(STDIN)))*$y==$x;

Try it online!

Golfed 32 bytes thanks to @Shaggy's advice.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

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

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Can you add a [TIO](https://tio.run/) or equivalent, please? (4 comments)
+1
−0

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");}}}

Try it online!

If libraries also count in the code, the total byte count will be 226 218 213 210.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

This seems to return `false` for all squares greater than `4`. Also, we're not restricted to using TI... (4 comments)
+1
−0

C (gcc), 40 bytes

i;f(n){for(i=1;n>0;n-=i,i+=2);return!n;}

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+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}$.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

J, 6 bytes

0=1|%:

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+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
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

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

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

JavaScript (V8), 13 bytes

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

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »