[Question]
  [
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](https://math.stackexchange.com/a/2372342/467325) 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 \$N^2\$, so an easy (but maybe not golfiest) way to find all possible last digits for \$N^2\$ is to find \$n^2 \mod 10\$ for all \$0 \le n < 10\$. Each result is a possible last digit. For base-\$m\$, you could find \$n^2 \mod m\$ for all \$0 \le 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 \$N^2\$ won't overflow (If you can test all the way up to \$N^2\$, 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](/questions/tagged/code-golf "show questions tagged '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](https://codegolf.stackexchange.com/questions/35664/minimal-cover-of-bases-for-quadratic-residue-testing-of-squareness)).
  
[Answer]
# Google Sheets, ~~52~~ ~~51~~ 47 bytes
```
=ArrayFormula(Join(",",Unique(Mod(Row(A:A)^2,A1
```
Saved 4 bytes thanks to Taylor Scott
Sheets will automatically add 4 closing parentheses to the end of the formula.
It doesn't return the results in ascending order but it does return the correct results.
[![Results](https://i.stack.imgur.com/lkB07.png)](https://i.stack.imgur.com/lkB07.png)
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 5 bytes
```
R²%³Q
```
[Try it online!](https://tio.run/##y0rNyan8/z/o0CbVQ5sD////b2wIAA "Jelly – Try It Online")
### Explanation
```
R²%³Q   Main link, argument: n
R       Range from 1 to n
 ²      Square each
  %³    Mod each by n
    Q   Deduplicate
```
[Answer]
# [Swift](https://www.swift.org), ~~47 35~~ 32\* bytes
\* *-3 thanks to @Alexander.*
*Possibly the first time in history Swift ~~ties~~ beats Python?*
```
{m in Set((0..<m).map{$0*$0%m})}
```
**[Try it online!](http://swift.sandbox.bluemix.net/#/repl/5979f5c2e2406b7625f9fe3f)**
---
# Explanation
* `(0..<m).map{}` - Iterates through the range `[0...m)` and map the following results:
* `$0*$0%m` - The square of each integer modulo the base `m`.
* `Set(...)` - Removes the duplicates.
* `m in` - Assigns the base to a variable `m`
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 5 bytes
```
Lns%ê
```
[Try it online!](https://tio.run/##MzBNTDJM/f/fJ69Y9fCq//8NjQwA "05AB1E – Try It Online") or as a [Test Suite](https://tio.run/##MzBNTDJM/V9TVln53yevWPXwqv86/w25jLgMDbgMzbiMDbkMjQwA "05AB1E – Try It Online")
```
L     # Range 1 .. input
 n    # Square each
  s%  # Mod by input
    ê # Uniquify (also sorts as a bonus)
```
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), 6 bytes
```
∪⊢|⍳×⍳
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///P@1R24RHHasedS2qedS7@fB0IAEUVDA04AISZiACiI0NAQ "APL (Dyalog Unicode) – Try It Online")
Explanation:
```
∪⊢|⍳×⍳
   ⍳×⍳    ⍝ square every number in range from 1 to ⍵
 ⊢|       ⍝ modulo ⍵
∪         ⍝ unique elements
```
[Answer]
# Pyth, 6 bytes
```
{%RQ*R
```
[Try it online](https://pyth.herokuapp.com/?code=%7B%25RQ%2aR&test_suite=1&test_suite_input=1%0A2%0A10%0A16%0A31%0A120)
### How it works
```
{%RQ*RdQ    implicit variables
       Q    autoinitialized to eval(input())
    *R      over [0, …, Q-1], map d ↦ d times
      d         d
 %R         map d ↦ d modulo
   Q            Q
{           deduplicate
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt/), ~~7~~ 6 bytes
```
Ç²%UÃâ
```
[Test it](http://ethproductions.github.io/japt/?v=1.4.5&code=x7IlVcPi&input=MTIw)
1 byte saved thanks to [Oliver](https://codegolf.stackexchange.com/users/61613/oliver)
---
## Explanation
Implicit input of integer `U`.
```
Ç   Ã
```
Create an array of integers from `0` to `U-1`, inclusive and pass each though a function.
```
²
```
Square.
```
%U
```
Modulo `U`.
```
â
```
Get all unique elements in the array and implicitly output the result.
[Answer]
# [Python 3](https://docs.python.org/3/), ~~40~~ ~~39~~ 37 bytes
*-1 byte thanks to Mr. Xcoder. -2 bytes thanks to Business Cat.*
```
lambda m:[*{n*n%m for n in range(m)}]
```
[Try it online!](https://tio.run/##HYuxCoAgFEV3v@IugYZDFjQEfUk1GGUJ@QpziejbzTpwhwP3HFdYd6qiafu4aTdOGq7p8ptyyhzM7kGwBK9pmbkTzxDDfIYTLTolUUqoIq2WqJKqshgY@yL7Rf@zYUgc3lLghlsh4gs "Python 3 – Try It Online")
[Answer]
# C#, 63 bytes
```
using System.Linq;m=>new int[m].Select((_,n)=>n*n%m).Distinct()
```
[Try it online!](https://tio.run/##lY5BawIxEIXv@RWDICRlDeqhl@3uxapYKggeeihStulYBpIJ7mQrUvztayw99GiP8@Z7j8/JKESOfSfEn7A9ScJQqr@XfSY@lEo534jARn0rSU0iB1@RPmDdEGuTw0XH7oE4Fb@tWfQeXaLIYpfI2JKzqzl3Advm3eMVrWvYQwV9qGrGI@TkNezsFq89rd8KNlWt@Y7NMBj7SJKI88P0WWaWZ6NH@9JSwiyIWlKbne1TzD4DGBSw1xNjTHkbOr0dnYz/wd7/sOqszv0F "C# (Mono) – Try It Online")
[Answer]
## JavaScript (ES6), 52 bytes
```
f=(m,k=m,x={})=>k?f(x[k*k%m]=m,k-1,x):Object.keys(x)
```
### Test cases
```
f=(m,k=m,x={})=>k?f(x[k*k%m]=m,k-1,x):Object.keys(x)
;[1, 2, 10, 16, 31, 120]
.map(m => console.log(m + ' -> ' + f(m)))
```
---
## Non-recursive version, ~~60~~ 58 bytes
*Saved 2 bytes thanks to @ThePirateBay*
```
m=>(a=[...Array(m).keys()]).filter(v=>a.some(n=>n*n%m==v))
```
### Test cases
```
let f =
m=>(a=[...Array(m).keys()]).filter(v=>a.some(n=>n*n%m==v))
;[1, 2, 10, 16, 31, 120]
.map(m => console.log(m + ' -> ' + f(m)))
```
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), ~~10~~ 9 bytes
```
>ℕ^₂;?%≜ᶠ
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/r/3@5Ry9S4R01N1vaqjzrnPNy24P9/QyOD/1EA "Brachylog – Try It Online")
### Explanation
```
       ≜ᶠ       Find all numbers satisfying those constraints:
    ;?%           It must be the result of X mod Input where X…
  ^₂              …is a square…
>ℕ                …of an integer in [0, …, Input - 1]
```
[Answer]
# [Actually](https://github.com/Mego/Seriously), 11 bytes
```
;╗r⌠²╜@%⌡M╔
```
[Try it online!](https://tio.run/##ASQA2/9hY3R1YWxsef//O@KVl3LijKDCsuKVnEAl4oyhTeKVlP//MTA "Actually – Try It Online")
Explanation:
```
;╗r⌠²╜@%⌡M╔
;╗           store a copy of m in register 0
  r          range(m)
   ⌠²╜@%⌡M   for n in range:
    ²          n**2
     ╜@%       mod m
          ╔  remove duplicates
```
[Answer]
# [CJam](https://sourceforge.net/p/cjam), 12 bytes
```
{_,_.*\f%_&}
```
Anonymous block accepting a number and returning a list.
[Try it online!](https://tio.run/##S85KzP1fWPe/Ol4nXk8rJk01Xq32f13Bf2NDAA "CJam – Try It Online")
### Explanation
```
_,          Copy n and get the range [0 .. n-1]
  _.*       Multiply each element by itself (square each)
     \f%    Mod each by n
        _&  Deduplicate
```
[Answer]
# [Haskell](https://www.haskell.org/), 45 bytes
```
import Data.List
f m=nub[n^2`mod`m|n<-[0..m]]
```
-4 bytes from Anders Kaseorg
[Try it online!](https://tio.run/##y0gszk7Nyfn/PzO3IL@oRMElsSRRzyezuIQrTSHXNq80KTovzighNz8lIbcmz0Y32kBPLzc29n9uYmaebUFRZl6JSpqCocF/AA "Haskell – Try It Online")
[Answer]
# [MATL](https://github.com/lmendo/MATL), ~~6~~ 5 bytes
-1 byte thanks to @LuisMendo
```
:UG\u
```
[Try it online!](https://tio.run/##y00syfn/3yrUPab0/39DAwA "MATL – Try It Online")
[Answer]
# AWK, ~~44~~ ~~43~~ 40 bytes
```
{for(;i++<$0;)if(!a[j=i*i%$0]++)print j}
```
I managed to cut off 3 bytes after revisiting this old answer.
Step by step:
```
{
for(;i++<$0;)        For all numbers less of equal to the input...
            
if(!a[j=i*i%$0]++)   Increments the element j of the array a.
                     j is the last digit of each i*i.
                     If the element a[j] is being incremented
                     for the first time (a[j]++ returns 0,
                     so negating it (!a[j]++) returns 1)...
print j              Prints j.
}
```
[Try it online!](https://tio.run/##SyzP/v@/Oi2/SMM6U1vbRsXAWjMzTUMxMTrLNlMrU1XFIFZbW7OgKDOvRCGr9v9/QwMA "AWK – Try It Online")
### Former answer (43 bytes)
```
{for(;i++<$0;)a[i*i%$0];for(j in a)print j}
```
Edit: Incrementing the `i` variable during the `for` condition saved one more byte.
Step by step:
```
{                        
for(;               # starts the loop with no previous statement.
    i++<$0;)        # loops while _i_ is less than the input ($0).
                    # also increments 1 to the _i_ variable after
                    # it is evaluated.
    a[i*i%$0];      # fiat the _i*i%$0_ element of the _a_ array!
                    # by only stating _array[element]_, the element exists.
                    # i*i%$0 means: i squared (mod $0), i.e., the last digit.
for(j in a)         # for every element _j_ existing in the _a_ array,
    print j         # prints the element _j_
}
```
[Try it online!](https://tio.run/##SyzP/v@/Oi2/SMM6U1vbRsXAWjMxOlMrU1XFINYaJJylkJmnkKhZUJSZV6KQVfv/v6EBAA "AWK – Try It Online")
[Answer]
# [Octave](https://www.gnu.org/software/octave/), 27 bytes
```
@(n)unique(mod((1:n).^2,n))
```
[Try it online!](https://tio.run/##y08uSSxL/Z@mYKugp6f330EjT7M0L7OwNFUjNz9FQ8PQKk9TL85IJ09T83@ahqEmV5qGEYgwNACTZiDSGCxsaGSg@R8A "Octave – Try It Online")
[Answer]
# Mathematica, 30 bytes
```
Union@Table[Mod[i^2,#],{i,#}]&
```
[Try it online!](https://tio.run/##y00sychMLv6fZvs/NC8zP88hJDEpJzXaNz8lOjPOSEc5Vqc6U0e5Nlbtf0BRZl6JgkOagoOhkcF/AA "Mathics – Try It Online")
[Answer]
## JavaScript (ES6), 48 bytes
```
f=
n=>[...new Set([...Array(n)].map((_,i)=>i*i%n))]
```
```
<input type=number min=0 oninput=o.textContent=f(+this.value)><pre id=o>
```
43 bytes if returning a `Set` instead of an array is acceptable.
[Answer]
# [Scala](http://www.scala-lang.org/), ~~32~~ 30 bytes
Simple use of the *easy tip* from OP.
```
(0 to n-1).map(x=>x*x%n).toSet
```
[Try it online!](https://tio.run/##K05OzEn8n5@UlZpcouCbmJmnkFpRkpqXUqzgWFBQzcWZkpqmUFxYmliU6pqXopFn5ZlXomkVnFoSDWTE2lb/1zBQKMlXyNM11NTLTSzQqLC1q9CqUM3T1CvJB6r6X8vFVVCUmVeSk6ehZKiga6egpI0wzVBTEyFrhC5rhCxraICh2QBF3gxD3gxZ3hjDcmMU2w2NMC0wAtlQ@x8A "Scala – Try It Online")
-2 bytes thanks to @MrXcoder, with priorities (no need for `()` around `*` operation)
Wondering: is this possible to *implicitly* tell the compiler to understand things like `(0 to n-1)map(x=>x*x%n)toSet` (without having to `import scala.language.postfixOps`)?
[Answer]
# [Haskell](https://www.haskell.org/), 44 bytes
```
f m=[k|k<-[0..m],or[mod(n^2)m==k|n<-[0..m]]]
```
[Try it online!](https://tio.run/##NY2xCsIwGAZf5aM4JPRvaSI4mT6BTo4xSkClkv5JaSsuffdoBbfjbrjOT@He9zk/wMaGJewr29Q1O0qj5XQT8aIlGxOW@C/OZfbPCAP2w/EKcWZULYbXfJrHQ8QGU5feYJQlirUUK/2c@F6khFWkSTWkdrRVpHTj8gc "Haskell – Try It Online")
[Answer]
# [Perl 6](https://perl6.org), 19 bytes
```
{set (^$_)»²X%$_}
```
[Test it](https://tio.run/##VZDBbsIwDIbveQoLlZEOU5LQhlYVE9edt8MO09DWBgmplIqEaRPipXbdjRfrHECF5RT//@/PlhuzrXS7swY@dVTkbP0Nd8WmNDBr99Y44G/BIjz@Hn9e@sHi0JI9d8Y6mEG1qo3l5EXFZv3Bx6/lcOyrx9rlzAOfKZezpnqvYXhqytlys730jx6AQ7Cqm51DuJ@br8YUzpQQwp4B0Jigk2bQ2dGTIcrZ3xq7q/wifl1@RoXeXNlRaUxTdRm8whB6l6nRcu34oD9K7CD021yyUUMH4WEvZ4cWJPhHpmCguj9KBlJcK0xQY4wZqfpGPSkTeaMo0hKcYooZShJilBplijJDJVAlqFJiKPGP4TMq9u6EpgiMM9QCdYxTjanElCIEEZQWyR8 "Perl 6 – Try It Online")
## Expanded:
```
{ # bare block lambda with implicit param ｢$_｣
  set        # turn the following into a Set (shorter than ｢unique｣)
      (
        ^$_  # a Range upto (and excluding) ｢$_｣
      )»²    # square each of them (possibly in parallel)
    X%       # cross modulus the squared values by
      $_     # the input
}
```
[Answer]
# [Ruby](https://www.ruby-lang.org/), ~~31~~ 30 bytes
```
->m{(0..m).map{|n|n*n%m}.uniq}
```
[Try it online!](https://tio.run/##KypNqvxfYBvzX9cut1rDQE8vV1MvN7GguiavJk8rTzW3Vq80L7Ow9n@0oY6RjqGBjqGZjrGhjqGRQaxeamJyRnVNbo1CQWlJsYKScnVurZWCcnVBdG5srVLtfwA "Ruby – Try It Online")
[Answer]
# [Whispers v2](https://github.com/cairdcoinheringaahing/Whispers), 71 bytes
```
> Input
>> [1)
>> L²
>> L%1
>> Each 3 2
>> Each 4 5
>> {6}
>> Output 7
```
[Try it online!](https://tio.run/##K8/ILC5ILSo2@v/fTsEzr6C0hMvOTiHaUBNE@RzaBKZUDUGUa2JyhoKxghGcbaJgCmJXm9WCKP/SEqBuBfP/QLb5wy27kMQs/xsaGQAA "Whispers v2 – Try It Online")
The TIO Footer simply sorts the output, remove it to see the unsorted set.
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 5 bytes
```
ɾ²$%U
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyIiLCIiLCLJvsKyJCVVIiwiIiwiMTIwIl0=)
```
ɾ     # 1...n
 ²    # Each squared
   %  # Modulo...
  $   # input
    U # Uniquify the result
```
[Answer]
# [Nibbles](http://golfscript.com/nibbles/index.html), 5 bytes
```
`$.,$%^$~@
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m708LzMpKSe1eMGCpaUlaboWqxJU9HRUVONU6hwgAlDxBRsMuYy4DA24DM24jA25DI0MIOIA)
```
`$ Deduplicate
.   map
,    range
$     input
%    mod
^     power
$      it
~      2
@     input
```
[Answer]
# [Thunno 2](https://github.com/Thunno/Thunno2), 4 [bytes](https://github.com/Thunno/Thunno2/blob/main/docs/codepage.md)
```
L²ŒU
```
[Try it online!](https://Not-Thonnu.github.io/run#aGVhZGVyPSZjb2RlPUwlQzIlQjIlQzUlOTJVJmZvb3Rlcj0maW5wdXQ9MTIwJmZsYWdzPQ==)
#### Explanation
```
L²ŒU  # Implicit input
L     # Push [0..input)
 ²    # Square each value
  Œ   # Mod by input
   U  # Uniquify
      # Implicit output
```
[Answer]
# [K (ngn/k)](https://codeberg.org/ngn/k), ~~11~~ 10 bytes
```
{?x!*/&=x}
```
[Try it online!](https://ngn.codeberg.page/k#eJxVj9FOwzAMRd/zFUYgBMhd4zRN01bAM098wIS0qkvXaaPd1lR0msa34w4GLC++Prm5savs8DxcPYS3j8NRCJ8dbqb7z11WAcCQz9pVfjeriuU6H/J9vrt/Owo/pRzlG1eVS6BRkBwVxGBAQ3oi5kR+uojOHZABFbOTLCigFBJQrPhWA0mwp7dK5nDpjzg4BaPBEtskKA2JYRWD1aAlpAYMjyREKGrvN10WhmU7d4t2XU06X5QrN5R10SzcpGzfw23vOr9smy6kyMjEhnX7Ecxb1wW+dkG37YudC1wzDxUZG+nr7yIEvDSb3kPwBK+9ZyV4xvEwkILXOWskMS7z22GMBjWmYtzmj55IRP+IYhZjghZTJAYaySBZpBSVRBWjspyh5EXG6FF6vI34F4k6RSPRaEwMWkLLFg6R7JbxF36gcJU=)
* `&=x` generate a list containing `(0..x; 0..x)`
* `*/` multiply the two items of the list together
* `x!` mod the result by `x`
* `?` (implicitly) return the distinct last digits
[Answer]
# [Retina](https://github.com/m-ender/retina), 70 bytes
```
.+
$*
;$`¶$`
1(?=.*;(.*))|;1*
$1
(1+)(?=((.*¶)+\1)?$)
D`1*¶
^|1+
$.&
```
[Try it online!](https://tio.run/##K0otycxL/P9fT5tLRYuLy1ol4dA2lQQuQw17Wz0taw09LU3NGmtDLS4VQy4NQ21NoLAGUPDQNk3tGENNexVNLi6XBEMgnyuuxhBohJ7a//@GBgA "Retina – Try It Online") Warning: Slow for large inputs. Slightly faster 72-byte version:
```
.+
$*
$'¶$';
1(?=.*;(.*))|;1*
$1
+s`^((1+)¶.*)\2
$1
^1+
D`1*¶
^|1+
$.&
```
[Try it online!](https://tio.run/##K0otycxL/P9fT5tLRYuLS0X90DYVdWsuQw17Wz0taw09LU3NGmtDLS4VQy7t4oQ4DQ1Dbc1D24DCMUYgsThDbS4ulwRDrUPbuOJqgBwVPbX//40NAQ "Retina – Try It Online")
[Answer]
# Clojure, 40 bytes
```
#(set(map(fn[x](mod(* x x)%))(range %)))
```
]