| Bytes | Lang | Time | Link |
|---|---|---|---|
| 041 | Janet | 250508T182302Z | Adam |
| 030 | Juby | 250505T014401Z | Jordan |
| 010 | K ngn/k | 201223T152107Z | coltim |
| 010 | J | 230721T060425Z | south |
| 004 | Thunno 2 | 230720T162846Z | The Thon |
| 045 | JavaScript | 230201T121859Z | Shaggy |
| nan | 230201T104153Z | The Thon | |
| 005 | Nibbles | 230201T102430Z | Adam |
| 007 | Pyt | 230201T013529Z | Kip the |
| 005 | Vyxal | 221107T090736Z | emanresu |
| 049 | Prolog SWI | 220903T122853Z | Jo King |
| 040 | AWK | 201223T060204Z | Pedro Ma |
| 032 | Perl 5 MListUtil=uniq na | 170905T211656Z | Xcali |
| 006 | APL Dyalog Unicode | 201222T195001Z | Kamila S |
| 071 | Whispers v2 | 201016T131413Z | caird co |
| 025 | Pari/GP | 170906T122142Z | alephalp |
| 077 | Java OpenJDK 8 | 170728T160230Z | Xanderha |
| 047 | Google Sheets | 170727T135143Z | Engineer |
| 131 | 8th | 170728T154701Z | Chaos Ma |
| 032 | Swift | 170727T135809Z | Mr. Xcod |
| 053 | PHP | 170728T163141Z | Xanderha |
| 3130 | Ruby | 170727T165105Z | Alexis A |
| 005 | MATL | 170728T093840Z | Cinaski |
| 006 | Japt | 170727T134253Z | Shaggy |
| 028 | R | 170728T083514Z | Leaky Nu |
| 035 | PowerShell | 170728T073702Z | Joey |
| 009 | Brachylog | 170727T134504Z | Fatalize |
| 059 | Python 2 | 170728T044913Z | Husnain |
| 006 | Pyth | 170728T034138Z | Anders K |
| 013 | Pyth | 170728T032803Z | alleks |
| nan | 170728T023645Z | Brad Gil | |
| 045 | Haskell | 170727T135301Z | user4594 |
| 044 | Haskell | 170727T194244Z | Anders K |
| 052 | JavaScript ES6 | 170727T143842Z | Arnauld |
| 040 | Clojure | 170727T153550Z | MattPutn |
| 070 | Retina | 170727T152736Z | Neil |
| 030 | Scala | 170727T140041Z | V. Court |
| 048 | JavaScript ES6 | 170727T145416Z | Neil |
| 030 | Mathematica | 170727T145203Z | ZaMoC |
| 027 | Octave | 170727T140234Z | Luis Men |
| 012 | CJam | 170727T135728Z | Business |
| 005 | 05AB1E | 170727T135212Z | Riley |
| 063 | C# | 170727T134706Z | TheLetha |
| 011 | Actually | 170727T134511Z | user4594 |
| 037 | Python 3 | 170727T134345Z | totallyh |
| 005 | Jelly | 170727T134314Z | Business |
Janet, 41 bytes
|(distinct(seq[i :down[$ 0]](%(* i i)$)))
K (ngn/k), 11 10 bytes
{?x!*/&=x}
&=xgenerate a list containing(0..x; 0..x)*/multiply the two items of the list togetherx!mod the result byx?(implicitly) return the distinct last digits
Thunno 2, 4 bytes
L²ŒU
Explanation
L²ŒU # Implicit input
L # Push [0..input)
² # Square each value
Œ # Mod by input
U # Uniquify
# Implicit output
JavaScript, 45 bytes
n=>(g=x=>x?g(g[x*x%n]=x-1):Object.keys(g))(n)
40 bytes
With output as an array that could contain multiple empty items.
n=>(g=x=>x?g(x-1,a[y=x*x%n]=y):a)(a=[n])
Thunno D, \$ 6 \log_{256}(96) \approx \$ 4.94 bytes
R2^$ZU
Thunno, \$ 7 \log_{256}(96) \approx \$ 5.76 bytes
DR2^$ZU
Explanation
DR2^$ZU # Implicit input
DR # Duplicate and get range(input)
2^ # Square each number
$ # Mod by the input
ZU # Uniquify
# Implicit output
Pyt, 7 bytes
Đř²⇹%Ụş
Đ implicit input (n); Đuplicate on stack
ř řangify (push [1,2,...,n])
² square
⇹% modulo n
Ụş get Ụnique elements and şort in ascending order; implicit print
Prolog (SWI), 49 bytes
N+X:-setof(K,A^(between(0,N,A),K is A^2mod N),X).
Creates a set of values from K=(A^2)%N, where 0<=A<=N.
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.
}
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_
}
APL (Dyalog Unicode), 6 bytes
∪⊢|⍳×⍳
Explanation:
∪⊢|⍳×⍳
⍳×⍳ ⍝ square every number in range from 1 to ⍵
⊢| ⍝ modulo ⍵
∪ ⍝ unique elements
Whispers v2, 71 bytes
> Input
>> [1)
>> L²
>> L%1
>> Each 3 2
>> Each 4 5
>> {6}
>> Output 7
The TIO Footer simply sorts the output, remove it to see the unsorted set.
Java (OpenJDK 8), 86 77 bytes
n->java.util.stream.IntStream.range(1,n+1).map(a->a*a%n).distinct().toArray()
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.
8th, 138 131 bytes
Code
[] swap dup >r ( 2 ^ r@ n:mod a:push ) 1 rot loop rdrop ' n:cmp a:sort ' n:cmp >r -1 a:@ swap ( tuck r@ w:exec ) a:filter rdrop nip
Explanation
[] - Create output array
swap dup >r - Save input for later use
( 2 ^ r@ n:mod a:push ) 1 rot loop - Compute square end
rdrop - Clean r-stack
' n:cmp a:sort - Sort output array
' n:cmp >r -1 a:@ swap ( tuck r@ w:exec ) a:filter rdrop nip - Get rid of consecutive duplicates from array
SED (Stack Effect Diagram) is: a -- a
Usage and example
: f [] swap dup >r ( 2 n:^ r@ n:mod a:push ) 1 rot loop rdrop ' n:cmp a:sort ' n:cmp >r -1 a:@ swap ( tuck r@ w:exec ) a:filter rdrop nip ;
ok> 120 f .
[0,1,4,9,16,24,25,36,40,49,60,64,76,81,84,96,100,105]
Swift, 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})}
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 basem.Set(...)- Removes the duplicates.m in- Assigns the base to a variablem
PHP, 53 bytes
for(;$i<$t=$argv[1];)$a[$z=$i++**2%$t]++?:print"$z
";
Loop from 0 to the input number, using the n^2 mod base formula to mark numbers that have been used. It goes to that position in an array, checking if it's been incremented and outputting it if it hasn't. It then increments it afterwards so duplicate values don't get printed.
Japt, 7 6 bytes
Dz%UÃâ
1 byte saved thanks to 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.
Brachylog, 10 9 bytes
>ℕ^₂;?%≜ᶠ
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]
Pyth, 6 bytes
{%RQ*R
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
Pyth, 13 bytes
VQ aY.^N2Q){Y
Lame attempt at explaining:
VQ for N in [0 .. input-1]
blank space to supress implicit print inside the loop
.^N2Q N ^ 2 % input
aY append that to Y, which is initially an empty list
) end for
{Y deduplicate and implicit print
To sort the output, insert an S on any side of the {
I think there should be a shorter way...
Perl 6, 19 bytes
{set (^$_)»²X%$_}
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
}
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)))
Clojure, 40 bytes
#(set(map(fn[x](mod(* x x)%))(range %)))
Retina, 70 bytes
.+
$*
;$`¶$`
1(?=.*;(.*))|;1*
$1
(1+)(?=((.*¶)+\1)?$)
D`1*¶
^|1+
$.&
Try it online! Warning: Slow for large inputs. Slightly faster 72-byte version:
.+
$*
$'¶$';
1(?=.*;(.*))|;1*
$1
+s`^((1+)¶.*)\2
$1
^1+
D`1*¶
^|1+
$.&
Scala, 32 30 bytes
Simple use of the easy tip from OP.
(0 to n-1).map(x=>x*x%n).toSet
-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)?
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.
CJam, 12 bytes
{_,_.*\f%_&}
Anonymous block accepting a number and returning a list.
Explanation
_, Copy n and get the range [0 .. n-1]
_.* Multiply each element by itself (square each)
\f% Mod each by n
_& Deduplicate
05AB1E, 5 bytes
Lns%ê
Try it online! or as a Test Suite
L # Range 1 .. input
n # Square each
s% # Mod by input
ê # Uniquify (also sorts as a bonus)
Actually, 11 bytes
;╗r⌠²╜@%⌡M╔
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
Python 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)}]
Jelly, 5 bytes
R²%³Q
Explanation
R²%³Q Main link, argument: n
R Range from 1 to n
² Square each
%³ Mod each by n
Q Deduplicate
