20
2
In Base-10, all perfect squares end in 0, 1, 4, 5, 6, or 9.
In Base-16, all perfect squares end in 0, 1, 4, or 9.
Nilknarf describes why this is and how to work this out very well in this answer, but I'll also give a brief description here:
When squaring a Base-10 number, N, the "ones" digit is not affected by what's in the "tens" digit, or the "hundreds" digit, and so on. Only the "ones" digit in N affects the "ones" digit in N2, so an easy (but maybe not golfiest) way to find all possible last digits for N2 is to find n2 mod 10 for all 0 <= n < 10. Each result is a possible last digit. For Base-m, you could find n2 mod m for all 0 <= n < m.
Write a program which, when given the input N, outputs all possible last digits for a perfect square in Base-N (without duplicates). You may assume N is greater than 0, and that N is small enough that N2 won't overflow (If you can test all the way up to N2, I'll give you a finite amount of brownie points, but know that the exchange rate of brownie points to real points is infinity to one).
Tests:
Input -> Output
1 -> 0
2 -> 0,1
10 -> 0,1,5,6,4,9
16 -> 0,1,4,9
31 -> 0,1,2,4,5,7,8,9,10,14,16,18,19,20,25,28
120 -> 0,1,4,9,16,24,25,36,40,49,60,64,76,81,84,96,100,105
this is code-golf, so standard rules apply!
(If you find this too easy, or you want a more in-depth question on the topic, consider this question: Minimal cover of bases for quadratic residue testing of squareness).
1Does the output array need to be sorted? – Shaggy – 2017-07-27T13:41:05.233
@Shaggy Nope! Mego, Duplication is not allowed. Theoretically, N could be enormous, so duplicates would make the output pretty unreadable. I'll adit the question – Lord Farquaad – 2017-07-27T13:41:43.087
Is outputting a set acceptable? – totallyhuman – 2017-07-27T13:47:09.307
2@totallyhuman Why wouldn't it be valid? Sets are unordered collections and it must not be sorted, so... – Mr. Xcoder – 2017-07-27T13:47:54.163