Comments on It's Hip to be Square
Parent
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 [ …
2y ago
[dc], 9 bytes ?dvd-^p …
2y ago
J, 6 bytes ```J 0=1|%: `` …
3y ago
Post
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.
0 comment threads