31
Introduction and Credit
We all know and love our awesome rules to test whether a number is divisble by 11 or 3, which is just some clever sum over the digits of the number. Now this challenge takes this to a new level, by requiring you to compute the sum of the digits and then checking whether the result is a perfect integer square, neither of which operations usually can be done very short. As this property is also very hard to see when looking at a number, we want this to be done for entire lists of numbers so we can save human work. So this is your challenge now!
This was an assignment at my university functional programming course. This assignment is now closed and has been discussed in class and I have my professor's permission to post it here (I asked explicitely).
Specification
Input
Your input is a list of non-negative integers, in any standard I/O format.
You may choose the list format as your language needs it
Output
The output is a list of integers, in any standard I/O format.
What to do?
Filter out every integer from the input list for which the sum of the digits is not a a square (of an integer).
The order of the elements may not be changed, e.g. if you get [1,5,9]
you may not return [9,1]
Potential corner cases
0 is a non-negative integer and thus a valid input and 0 is also a valid integer root, e.g. 0 counts as an integer square.
The empty list is a valid input and output as well.
Who wins?
This is code-golf so the shortest answer in bytes wins!
Standard rules apply of course.
Test Cases
[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] -> []
Step-by-Step Example
Example input: [1337,4444]
Handling first number:
Sum of the digits of 1337: 1+3+3+7=14
14 is not an integer square, thus will be dropped!
Handling second number:
Sum of the digits of 4444: 4+4+4+4=16
16 is an integer square because 4*4=16, can get into the output list!
Example output: [4444]
11Nice first challenge, and welcome to the site! – James – 2016-06-10T14:14:25.190
For future challenges note the sandbox. It's a place where we put challenges before we put them on the main site so hey can get reviewed and their content queried so they will (hopefully) get better received on main. Not that this is a bad question though (I actually quite like it)
– Blue – 2016-06-10T14:16:57.160@muddyfish, I've read about this and considered posting there but decided not to do it, because I was confident, that there's nothing I could miss / do horribly wrong here :) Of course if I have even some doubt there could be something I miss I'll post there. – SEJPM – 2016-06-10T14:21:07.290
12While it is totally fine to avoid the sandbox, had you posted there I would have suggested that you make the challenge only about testing an individual integer. The interesting task is the test, wrapping that task with a filter isn't particularly interesting. All it seems to do is make the challenge substantially more difficult in esoteric languages that do not have arrays as types. That may sound a bit harsh, but this is still an excellent first post. Just saying that the sandbox is there because no matter how sure you are you didn't miss anything, you missed something. – FryAmTheEggman – 2016-06-10T14:29:24.367
1@FryAmTheEggman I can say for Mathematica that making this function listable complicates things in a slightly non-trivial way, so it's not exactly boring. – LLlAMnYP – 2016-06-17T13:42:20.167
Is the output list required to be in the same order as the input? EDIT: Yup, it is. I'm just blind. – moonheart08 – 2018-09-23T00:50:01.410