| Bytes | Lang | Time | Link |
|---|---|---|---|
| 004 | Thunno 2 | 230729T173600Z | The Thon |
| 005 | Vyxal | 210711T112007Z | Wasif |
| 008 | Japt | 180925T124821Z | Shaggy |
| 004 | MathGolf | 180917T213306Z | Jo King |
| 018 | Attache | 180925T034329Z | Conor O& |
| 008 | Brachylog v2 | 180915T143113Z | ais523 |
| 095 | F# | 180922T003930Z | Ciaran_M |
| 054 | PowerShell | 160610T143322Z | AdmBorkB |
| 004 | Neim | 180915T221909Z | Okx |
| 055 | R | 180915T100941Z | J.Doe |
| 013 | K oK | 180826T074557Z | mkst |
| 039 | Ruby | 180829T092118Z | Kirill L |
| 048 | JavaScript Node.js | 180826T061328Z | user8232 |
| 036 | Mathematica | 160610T163818Z | Lynn |
| 012 | Actually | 160620T064731Z | user4594 |
| 042 | Perl 5 | 160610T185308Z | msh210 |
| 069 | Retina | 160611T000711Z | Digital |
| 066 | Javascript | 160610T141310Z | Bál |
| nan | 160611T171102Z | Kaz | |
| 072 | TXR Lisp | 160611T162043Z | Kaz |
| 078 | Hoon | 160611T153725Z | RenderSe |
| 103 | C# | 160611T152145Z | downrep_ |
| 010 | 05AB1E | 160610T143521Z | Emigna |
| 056 | JavaScript ES7 | 160610T234758Z | Neil |
| 059 | Haskell | 160610T215414Z | Dém |
| 141 | C | 160610T194616Z | Khaled.K |
| 035 | Perl 6 | 160610T211720Z | Brad Gil |
| 110 | Clojure | 160610T200146Z | cliffroo |
| 061 | Javascript | 160610T193457Z | Washingt |
| 027 | J | 160610T144111Z | Leaky Nu |
| 042 | MATLAB | 160610T171018Z | Suever |
| 008 | Jolf | 160610T170456Z | Conor O& |
| 053 | Python 2 | 160610T165701Z | Dennis |
| 038 | Julia | 160610T164538Z | Glen O |
| 053 | q | 160610T160525Z | C. Quill |
| 026 | Brachylog | 160610T155603Z | Fatalize |
| 213 | Oracle SQL 11.2 | 160610T155020Z | Jeto |
| 076 | Python 2 | 160610T150715Z | Denker |
| 013 | MATL | 160610T145404Z | Suever |
| 014 | CJam | 160610T143150Z | Denker |
| 007 | Jelly | 160610T141011Z | Leaky Nu |
| 010 | Pyth | 160610T141325Z | Leaky Nu |
| 006 | Pyke | 160610T140456Z | Blue |
Thunno 2, 4 bytes
æSƲ
Explanation
æSƲ # Implicit input
æ # Filter by:
S # Digit sum
Ʋ # Is a perfect square
# Implicit output
Vyxal, 5 bytes
'f∑∆²
' # Keep elements in input list
f∑ # Whose sum of digits is
∆² # A perfect square
MathGolf, 5 4 bytes
gÅΣ°
Explanation:
gÅ Filter by the next two instructions
Σ The digit sum
° Is a perfect square?
MathGolf is still in development, so I assume implicit input is coming soon to shave off that first byte. Yay!
Attache, 18 bytes
`\&:(IsSquare@Sum)
Explanation
`\ is the filter operator and &: applies the function IsSquare@Sum as it's predicate.
Brachylog v2, 8 bytes
{ẹ+√ℤ&}ˢ
Explanation
{ẹ+√ℤ&}ˢ
{ }ˢ Map the following operation over {the input}, discarding elements that error:
ẹ Split into a list of digits
+ Sum that list
√ Take its square root
ℤ Assert that the result is an integer
& Return to the original value
The & means that the elements output are the same as those in the input list, but the ℤ will error out if the block's input isn't a square number, so we get the input list with elements with non-square digit sums discarded.
Note that there might at first seem to be a floating point inaccuracy issue here (some very large non-square integers have integer square roots due to rounding). However, Brachylog supports bignum arithmetic, and actually has this behaviour factored into its implementation of √: a number which is a perfect square will have its square root reported as an integer, whereas a number which is not a perfect square (but close enough that its square root is integral) will have its square root reported as a float with an integral value. Conveniently, ℤ only permits the former sort of return value, giving an assertion failure for the latter.
F#, 95 bytes
let s n=Seq.exists(fun x->x*x=n){1..n}
let i x=Seq.filter(string>>Seq.sumBy(int>>(+)(-48))>>s)x
Makes a lot of use of the >> operator (the function composition operator) which chains functions together. s determines if there is an integer square for n.
The main function i maps each number into a string, then sums the digits in the string. Each digit is the character's integer representation minus 48 (e.g. '4' |> int = 52 - 48 = 4). Then it uses s to determine if the digit sums has an integer square. If it does, it's included in the returned sequence. If it does not, then it's excluded (Seq.filter).
PowerShell, 64 54 bytes
$args|?{!([math]::Sqrt(([char[]]"$_"-join'+'|iex))%1)}
-10 bytes thanks to mazzy
Takes input as command-line arguments (see examples below), which gets processed in PowerShell into the array $args. We pipe that to ? an alias for Where-Object (functions similar to filter) in order to select our output. Our selection is based on the .NET call [math]::Sqrt() of the digit-sum of the number is an integer with !(...%1). Integers will result in 0, which when noted becomes True while non-integer roots become False.
As mentioned elsewhere "returning" an empty array is meaningless, as it's converted to $null as soon as it leaves scope, so the output for an empty input is nothing.
Examples
PS C:\Tools\Scripts\golfing> .\return-integers-with-square-digit-sums.ps1 1 4 9 16 25 1111
1
4
9
1111
PS C:\Tools\Scripts\golfing> .\return-integers-with-square-digit-sums.ps1 1431 2 0 22 999999999
1431
0
22
999999999
PS C:\Tools\Scripts\golfing> .\return-integers-with-square-digit-sums.ps1 22228 4 113125 22345
22228
4
22345
PS C:\Tools\Scripts\golfing> .\return-integers-with-square-digit-sums.ps1
PS C:\Tools\Scripts\golfing> .\return-integers-with-square-digit-sums.ps1 1337 4444
4444
R, 57 55 bytes
Use Filter on the vector. Assumes 32 bit integers so 10 digits max.
Corner cases: returns NULL for the empty vector and numeric(0) for a vector with no valid numbers. These both have length zero so should be acceptable.
-2 thanks to @Giuseppe
Filter(function(n)!sum(n%/%10^(0:10)%%10)^.5%%1,scan())
K (oK), 19 17 13 bytes
Solution:
(~1!%+/.:'$)#
Explanation:
(~1!%+/.:'$)# / the solution
( )# / apply function to list
$ / convert to string
.:' / value (.:) each (')
+/ / sum
% / square-root
1! / modulo 1
~ / not
Notes:
- -2 bytes with smarter way of identifying squares
- -4 bytes thanks to ngn
JavaScript (Node.js), 48 bytes
a=>a.filter(b=>eval([...b+""].join`+`)**.5%1==0)
Explanation
a => // lambda function taking one argument
a.filter( // filter the list
eval( // begin eval
[...b+""] // convert number to array of digits
.join`+` // join them with + sign
) // close eval. we achieved sum of all digits of number
**.5 // square root of number
%1==0 // check for perfect square
) // end filter and return value
Mathematica, 39 36 bytes
An anonymous function:
Select[AtomQ@√Tr@IntegerDigits@#&]
LLlAMnYP saved a byte. Thank you!
Martin Ender saved three more by replacing IntegerQ with AtomQ. Clever! (The result of √ will be exact, so it returns a compound expression like Sqrt[5] if its argument isn’t a square.)
Actually, 12 bytes
`$♂≈Σ;√≈²=`░
Explanation:
`$♂≈Σ;√≈²=`░
`$♂≈Σ;√≈²=`░ filter list, taking only values where the following function is true:
$♂≈Σ to string, map int, sum (digital sum, we'll call this n)
;√≈²= compare equality to int(sqrt(n))**2
Perl 5, 42 bytes
41, plus 1 for -pe instead of -e
my$s;map$s+=$_,/./g;$_ x=sqrt$s==~~sqrt$s
Explanation:
-pgets each input integer on a new line and assigns$_to that string.my$sinitializes the variable$sto nothing, anew for each input integer.map$s+=$_,/./ggrabs each numeric character and numerically adds it to$s. (The newline becomes 0 when numified.)sqrt$s==~~sqrt$stests whether$shas a nonintegral square root, and$_ x=makes$_into itself or the empty string depending on that test.-pprints$_
Thanks to Brad Gilbert b2gills for saving three bytes.
Also 41 plus 1:
my$s;s/./$s+=$&/ger;$_ x=sqrt$s==~~sqrt$s
s/./$s+=$&/geradds each numeric character to$s(and the newline is 0 as above)
Retina, 69
Because testing for perfect squares in retina. This can be modified for generalised integer square root calculation.
.+ $&a$& +`\b\d $*b \bb $&: +`(\bb+):(bb\1) $1 $2: G`(:a|0$) .*a
Input is a newline-separated list.
- Stage 1 - repeat the number on each line, and separate with
a - Stage 2 - convert each digit before the
ato unary expressed asbs, separated with spaces - Stage 3 - remove spaces - each unary now represents the digit sum
- Stage 4 and 5 - Use the fact that perfect squares may be expressed 1 + 3 + 5 + 7 + ... . Split each unary up accordingly
- Stage 6 - grep filter just the ones that exactly split into the above form
- Stage 7 - discard all but the original number
Javascript 66 bytes
a=>a.filter(b=>(e=Math.sqrt((b+"").split``.reduce((c,d)=>c-+-d)))==(e|0))
Thanks for SergioFC for saving 7 bytes
C: 113 111 — based on other C answer.
This is a complete program with main. The input list is the argument list.
Old style C is used, but with the C99 fmod function.
q(char*n){double m=0;while(*n)m+=*n++-48;return!fmod(sqrt(m),1);}main(c,v)char**v;{while(*++v)if(q(*v))puts(*v);}q(char*n){double m=0;while(*n)m+=*n++-48;return!fmod(sqrt(m),1);}main(c,v)char**v;{while(*++v)q(*v)&&puts(*v);}
Run:
$ gcc golf.c -o golf -lm golf.c:1:1: warning: data definition has no type or storage class [enabled by default] golf.c: In function ‘q’: golf.c:1:52: warning: incompatible implicit declaration of built-in function ‘fmod’ [enabled by default] golf.c:1:57: warning: incompatible implicit declaration of built-in function ‘sqrt’ [enabled by default] $ ./golf 1 4 9 16 25 1111 1 4 9 1111 $ ./golf $ ./golf 421337 99 123456 1133557799 $
TXR Lisp: 85 81 72 byte (op ...) denoting anon. function:
(op keep-if (opip tostring (mapcar chr-digit) (apply +) (= @1 [[dup *] (isqrt @1)])))(op keep-if (opip `@1` (mapcar chr-digit) (apply +) (= @1 [[dup *] (isqrt @1)])))(op keep-if(opip`@1`(mapcar chr-digit)(apply +)(=@1[[dup *](isqrt@1)])))
$ txr -i
1> (op keep-if(opip`@1`(mapcar chr-digit)(apply +)(=@1[[dup *](isqrt@1)])))
#<interpreted fun: lambda #:rest-0117>
2> [*1 '(1 4 9 16 25 1111)]
(1 4 9 1111)
3> [*1 '(1431 2 0 22 999999999)]
(1431 0 22 999999999)
4> [*1 ()]
nil
5> [*1 '(22228 4 113125 22345)]
(22228 4 22345)
6> [*1 '(421337 99 123456789 1133557799)]
nil
7>
isqrt is integer square root. This will handle large integers; no floating-point hacks here.
TXR Lisp is fundamentally a Lisp-2 dialect, like ANSI Common Lisp or Emacs Lisp. However, the square brackets [whatever ...] notation is syntactic sugar for the operator (dwim whatever ...) which evaluates using a form of Lisp-1 namespace semantics rather than Lisp-2: functions and variable bindings in one space rather than separate spaces. dwim is "deep"; it's not a macro transforming Lisp-2 to Lisp-2; it has visibility into the lexical environments to deliver the actual Lisp-1-like semantics.
In [dup *], the dup combinator takes a binary function (or a function that can be used as binary), and returns a unary function which applies two copies of its argument to that function as two arguments; i.e [dup *] denotes a squaring function.
op is a rich partial evaluation macro, and opip combines left-to-right pipeline with op: op pipe. (op mapcar +) curries the mapcar function. In (opip (mapcar +) ...),mapcaris similarly curried (theop` is implicit), and part of a pipeline.
The TXR Lisp REPL allows numbered references to prior results, modulo 100: *1 is the value produced in the most recent command line numbered 1, or 101, or 201, ...
Hoon, 78 bytes
|*
*
%+
skim
+<
|=
@ui
=(0 q:(sqt (roll (scan (slag 2 <+<>) (star dit)) add)))
++skim only keeps the elements of the list that the function returns yes for. We cast all the atoms in the list to @ui in the sample, for the sole reason that Urbit's integers have mandatory group seperators: if you try to cast 1.000 to a string, it will return "1.000" instead of "1000". <@ui1.000>, however, gives "0i1000", so we cast and then cut off the first two letters of the string.
We then parse the string with the parser combinator (star dit), which is like the [0-9]* regex but also converts the character to an atom. We fold over the resulting list with add.
We call sqt on the number. In Hoon, the only data types are atoms or tuples, so it returns two atoms intead of a float: the nearest perfect square root, and the remainder. We can then just test if the remainder is 0.
C#, 103 bytes
int[] f(int[] a){return a.Where(n=>Math.Sqrt((n+"").Select(m=>int.Parse(m+"")).Sum())%1==0).ToArray();}
Its really sad i have to turn chars and ints into strings all the time to work on them, i wish there was a better solution than adding an empty literal "" to cast into string.
any help on golfing this down appreciated
05AB1E, 19 10 bytes
vySOtDï->—
Explanation
vy # for each int in list
SO # digit sum
tDï- # difference between sqrt() and int(sqrt())
> # increase by 1 giving 1 (true) when equal
— # print current int in list if truthy
Edit: Saved 9 bytes thanks to @Adnan
JavaScript (ES7), 56 bytes
a=>a.filter(n=>[...''+n].reduce((x,y)=>+x+ +y)**.5%1==0)
Haskell - 70 60 59 bytes
f=filter(\x->elem(sum.map(read.pure).show$x)$map(^2)[0..x])
Usage:
> f [0..100]
[0,1,4,9,10,13,18,22,27,31,36,40,45,54,63,72,79,81,88,90,97,100]
Quite straightforward; computes the sum of digits and checks if floor(sqrt(y))^2 == y
Edit: Stole the idea of checking list of squares from C. Quilley
C, 143 141 bytes
- saved 2 bytes, @user6188402
i;q(char*n){double m=0;while(*n)m+=*n++-48;m=sqrt(m)-(int)sqrt(m);return !m;}s(n,s)char**n;{i=-1;while(++i<s)if(q(n[i]))printf("%s\n",n[i]);}
Ungolfed try online
int q(char*n)
{
double m=0;
while(*n) // sum digits
m+=*n++-48;
// get the decimal part of its square root
m=sqrt(m)-(int)sqrt(m);
// true if decimal part is zero
return !m;
}
// input is text, can be a file
void s(char**n, int s)
{
int i=-1;
while(++i<s) // for each number in input
if(q(n[i])) // if is square
printf("%s\n",n[i]); // output is terminal
}
Perl 6, 38 35 bytes
{.grep: {($/=sqrt [+] .comb)==$/.Int}}
{.grep: {($/=.comb.sum.sqrt)==$/.Int}}
{.grep: {($/=sqrt [+] .comb)==^$/}}
{.grep: {($/=.comb.sum.sqrt)==^$/}}
Test:
#! /usr/bin/env perl6
use v6.c;
use Test;
my @tests = (
[1,4,9,16,25,1111] => [1,4,9,1111],
[1431,2,0,22,999999999] => [1431,0,22,999999999],
[22228,4,113125,22345] => [22228,4,22345],
[] => [],
[421337,99,123456789,1133557799] => [],
);
plan +@tests;
my &sq-digit-sum = {.grep: {($/=sqrt [+] .comb)==^$/}}
for @tests -> $_ ( :key($input), :value($expected) ) {
is sq-digit-sum($input), $expected, .gist
}
1..5
ok 1 - [1 4 9 16 25 1111] => [1 4 9 1111]
ok 2 - [1431 2 0 22 999999999] => [1431 0 22 999999999]
ok 3 - [22228 4 113125 22345] => [22228 4 22345]
ok 4 - [] => []
ok 5 - [421337 99 123456789 1133557799] => []
Clojure, 110 bytes
(fn[t](filter(fn[x](let[a(reduce +(*(count(str x))-48)(map int(str x)))](some #(=(* % %)a)(range(inc a)))))t))
Calculates the sum of number digits and then filters out those for which there doesn't exist a number which squared is equal to the sum.
You can see the result here – https://ideone.com/ciKOje
Javascript, 61 65
f=
a=>a.filter(x=>(r=Math.sqrt(eval([...`${x}`].join`+`)))==~~r)
j=JSON.stringify;
console.log([
[1,4,9,16,25,1111],
[1431,2,0,22,999999999],
[22228,4,113125,22345],
[],
[421337,99,123456789,1133557799]
].map(x=>j(x)+' -> '+j(f(x))).join`
`)
J, 33 27 bytes
6 bytes thanks to @miles.
#~[:(=<.)@%:+/"1@(10&#.inv)
In online interpreters, inv is not instored. Change that to ^:_1 instead.
Usage
>> f =: #~[:(=<.)@%:+/"1@(10&#.inv)
>> f 1 4 9 16 25 1111 0
<< 1 4 9 1111 0
Where >> is STDIN and << is STDOUT.
Slightly ungolfed
to_base_10 =: 10&#.^:_1
sum =: +/"1
sqrt =: %:
floor =: <.
itself =: ]
equals =: =
of =: @
is_integer =: equals floor
test =: is_integer of sqrt
copies_of =: #
f =: copies_of~ [: test (sum of to_base_10)
Previous 33-byte version
(]=*:@<.@%:)@(+/"1@(10#.^:_1]))#]
Usage
>> f =: (]=*:@<.@%:)@(+/"1@(10#.^:_1]))#]
>> f 1 4 9 16 25 1111 0
<< 1 4 9 1111 0
Where >> is STDIN and << is STDOUT.
Slightly ungolfed
to_base_10 =: 10#.^:_1]
sum =: +/"1
sqrt =: %:
floor =: <.
square =: *:
itself =: ]
equals =: =
of =: @
test =: itself equals square of floor of sqrt
copies_of =: #
f =: (test of (sum of to_base_10)) copies_of itself
MATLAB, 52 43 42 bytes
@(x)x(~mod(sum(dec2base(x,10)'-48).^.5,1))
Creates an anonymous function named ans that can be called with an array as input: ans([22228,4,113125,22345]).
Online Demo. The online demo is in Octave which doesn't work for the empty input, but MATLAB does.
Explanation
We convert each element in the input array to base 10 which will yield a 2D character array where each row contains the digits of a number in the array. To convert these characters to numbers, we subtract 48 (ASCII for '0'). We then sum across the rows, take the square root, and determine whether each value is a perfect square ~mod 1. We then use this boolean to filter the input array.
Jolf, 8 bytes
ψxd!iUuH
Explanation
ψxd!iUuH
ψxd filter the input according to the input
uH digit sum of H (element)
U sqrt of
!i is an integer?
Julia - 38 bytes
!X=filter(i->√sum(digits(i))%1==0,X)
It's pretty easy to see what this does. digits converts a number into a list of its digits, sum thus calculates the digit-sum, √ will then produce a whole number if the number is a square, otherwise there will be a fractional part. %1 will return only the fractional part, and if it's zero (==0), filter will keep it on the list, otherwise it gets filtered out.
Used as ![22228,4,113125,22345]
q - 53 characters
{x where({(sum({"J"$x}')($:)x)in((!:)x+1)*(!:)x+1}')x}
The interesting bit is the inner lambda, which casts to a string, sums the digit (by casting each of them to an int), and then checks if they are a in a list of squares.
Brachylog, 26 bytes
:1f.
e.(:ef+~^[X:2]h>0;.0)
Example:
?- run_from_file('code.brachylog',[1431:2:0:22:999999999],Z).
Z = [1431, 0, 22, 999999999]
Explanation
This is a situation where something works a bit too well... the ~^[X:2] part is true for both positive and negative X, so to avoid duplicates I have to specify that X > 0.
The ;.0 part is here due to a bug (enumerate doesn't work on the integer 0).
Main predicate
:1f. Find all values of Input which satisfy predicate 1Predicate 1
e. Unify output with an element of the input ( :ef Find all elements of Output (i.e. all digits) + Sum the digits ~^[X:2] True if that sum is the result of X², whatever X is h>0 Impose that X > 0 ; OR .0 True if Output is 0 )
Oracle SQL 11.2, 213 bytes
WITH v AS(SELECT a,SQRT(XMLQUERY(REGEXP_REPLACE(a,'(\d)','+\1')RETURNING CONTENT).GETNUMBERVAL())s FROM(SELECT TRIM(COLUMN_VALUE)a FROM XMLTABLE(('"'||REPLACE(:1,',','","')||'"'))))SELECT a FROM v WHERE s=CEIL(s);
Un-golfed
WITH v AS
(
SELECT a,SQRT(XMLQUERY(
REGEXP_REPLACE(a,'(\d)','+\1') -- Add a + in front of each digit
RETURNING CONTENT
).GETNUMBERVAL())s -- Evaluate the expression generated by the added +
FROM
(SELECT TRIM(COLUMN_VALUE)a FROM XMLTABLE(('"'||REPLACE(:1,',','","')||'"'))) -- Split string on ','
)
SELECT a FROM v WHERE s=CEIL(s) -- Is a square if square has no decimal part
Python 2, 76 bytes
lambda l:filter(lambda n:eval(("sum(map(int,`n`))**.5==int("*2)[:-6]+")"),l)
Some abuse of eval to check for a square number, rest is pretty unspectacular.
The eval statement evaluates to sum(map(int,n))**.5==int(sum(map(int,n))**.5)
MATL, 16 14 13 bytes
"@tV!UsX^1\?x
Explanation
% Implicitly grab input
" % For each number in the input
@t % Get this element and duplicate
V % Convert to it's string representation
! % Transpose the string so each digit is on it's own row
U % Convert each row to a number (separates the digits)
s % Compute the sum of the digits
X^ % Compute the square root
1\ % mod with 1 to determine if the square root is an integer
?x % If there is a remainder, then remove this element from the stack
% Implicitly display the stack contents
CJam, 14 bytes
Thanks to @FryAmTheEggman for saving one byte!
{{Ab:+mq_i=},}
This is an unnamed block that expects the input list on the stack and leaves the filtered list on it.
Explanation
{ e# start a new block
Ab e# convert to base 10 -> split number into digits
:+ e# sum th digits
mq e# get the square root
_ e# duplicate the result
i e# convert to integer
= e# check if the converted square root and the original one are equal
} e# end block
, e# filter the input list
Jelly, 8 7 bytes
1 byte thanks to @Sp3000.
DSƲðÐf
Explanation
DSƲðÐf Main monadic chain. Argument: z
Ðf Filter for truthiness:
D convert to base 10
S sum
Ʋ is perfect square
Pyth, 10 bytes
fsI@sjT;2Q
Explanation
fsI@sjT;2Q
f Q Filter for the following in Q(input):
jT; convert to base 10
s sum
@ 2 square-root
sI is integer (is invariant under flooring)
Pyke, 6 bytes
#s,DBq
# - Filter list on truthiness:
s - digital_root(^)
, - sqrt(^)
Bq - int(^) == ^