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

Dashboard
Notifications
Mark all as read
Challenges

It's Hip to be Square

+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?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

14 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!

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

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

APL (Dyalog Extended), 3 bytes

√∊…

Try it online!

Is the square root in the range?

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

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+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

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

This fails for `0`, which should return a truthy value. (4 comments)
+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

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

Strictly speaking, both outputs here are truthy as they're both non-empty strings. (4 comments)
+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).

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

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

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+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

Can you add a [TIO](https://tio.run/) or equivalent, please? (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.

Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Does nothing (1 comment)

Sign up to answer this question »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!