Generate the group table for Z_n
APL (10)
(Assuming ⎕IO=0
. It works on ngn/apl by default, other APLs tend to need an ⎕IO←0
first.)
{⍵|∘.+⍨⍳⍵}
Explanation:
⍳⍵
: the numbers [0..⍵)∘.+⍨
: create a sum table⍵|
: numbers in the tablemod
⍵
GolfScript (13 chars)
I understand from your comment on Claudiu's answer that whitespace between the elements of a row is not necessary. On that understanding:
~.,{.n\(+}@(*
Online demo
Dissection:
~ Parse the input into an integer
., Duplicate it, turn the second into an array [0,...,n-1]
{ Loop: top of stack is the previous row
.n\ Push a newline and a copy of the previous row
(+ Rotate the first element to the end to get the new row
}@(* Perform loop n-1 times
If whitespace is necessary, for 20 chars:
~.,{.(+}@(*]{' '*n}/
Python 2, 66 bytes
def f(n):R=range(n);exec"print''.join(map(str,R));R+=R.pop(0),;"*n
Rotates the list by popping and re-appending.
Python 3, 53 bytes
def f(n):*R,=range(n);[print(*R[i:]+R[:i])for i in R]
Uses the same method as @mbomb007, but abusing print
as a function.