[Question]
  [
## Introduction
You may know and love your normal unit circle. But mathematicans are crazy and thus they have abstracted the concept to any point that satisfies `x*x+y*y=1`. Because Cryptographers1 are also weird, they *love* finite fields and *sometimes* finite rings (it is not like they have much choice though), so let's combine this!
## The Challenge
### Input
A positive integer larger than one in your favorite encoding. Let's call this number n.
### Output
You will output the "picture" (which consists of n times n characters) of the unit circle modulo the input integer as ASCII-Art using "X" (upper-case latin X) and " " (a space). Trailing spaces and newlines are allowed.
### More Details
You have to span a coordinate system from bottom-left to top-right. Whenever a point fulfills the circle equation, place an X at the position, otherwise place a space.
The condition for a point to be considered part of the circle border is:  
`mod(x*x+y*y,n)==1`.
Here a quick illustration of the coordinate-system:
```
(0,4)(1,4)(2,4)(3,4)(4,4)
(0,3)(1,3)(2,3)(3,3)(4,3)
(0,2)(1,2)(2,2)(3,2)(4,2)
(0,1)(1,1)(2,1)(3,1)(4,1)
(0,0)(1,0)(2,0)(3,0)(4,0)
```
If it helps you, you may also invert the direction of any of the axes, but the examples assume this orientation.
## Who wins?
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") so the shortest code in byte wins! [Only the default I/O methods are allowed](https://codegolf.meta.stackexchange.com/q/2447/55329) and all standard loopholes are banned.
## Examples
Input: 2
```
X 
 X
```
Input: 3
```
X  
X  
 XX
```
Input: 5
```
X    
     
     
X    
 X  X
```
Input: 7
```
X      
  X  X 
       
       
  X  X 
X      
 X    X
```
Input: 11
```
X          
           
     XX    
           
   X    X  
   X    X  
           
     XX    
           
X          
 X        X
```
Input: 42
```
X                                         
         X                       X        
                                          
                                          
            X                 X           
       X                           X      
      X                             X     
                     X                    
  X             X         X             X 
                                          
                                          
     X             X   X             X    
X                                         
               X           X              
              X             X             
         X                       X        
                                          
                                          
            X                 X           
                     X                    
        X           X X           X       
                     X                    
            X                 X           
                                          
                                          
         X                       X        
              X             X             
               X           X              
X                                         
     X             X   X             X    
                                          
                                          
  X             X         X             X 
                     X                    
      X                             X     
       X                           X      
            X                 X           
                                          
                                          
         X                       X        
X                                         
 X           X               X           X
```
---
1 I suggest you take a look at my profile if you're wondering here.
  
[Answer]
# Bash + GNU Utilities, 59
```
x={0..$[$1-1]}d*
eval echo $x$x+$1%1-0r^56*32+P|dc|fold -$1
```
Input `n` given as a command-line parameter. The y-axis is inverted.
[Try it online](https://tio.run/nexus/bash#@19hW22gp6cSrWKoaxhbm6LFlVqWmKOQmpyRr6BSoVKhrWKoaqhrUBRnaqZlbKQdUJOSXJOWn5OioKti@P//fxMjAA).
[Answer]
# [Octave](https://www.gnu.org/software/octave/), ~~45~~ 44 bytes
```
@(n)[(mod((x=(0:n-1).^2)+x',n)==1)*56+32,'']
```
[Try it online!](https://tio.run/nexus/octave#@@@gkacZrZGbn6KhUWGrYWCVp2uoqRdnpKldoa6Tp2lra6ipZWqmbWyko64e@z8xr1jD0FDzPwA "Octave – TIO Nexus")
[Answer]
# [Haskell](https://www.haskell.org/), 68 bytes
```
f n|r<-[0..n-1]=unlines[[last$' ':['X'|mod(x*x+y*y)n==1]|y<-r]|x<-r]
```
[Try it online!](https://tio.run/nexus/haskell#@5@mkFdTZKMbbaCnl6drGGtbmpeTmZdaHB2dk1hcoqKuoG4VrR6hXpObn6JRoVWhXalVqZlna2sYW1Npo1sUW1MBIv/nJmbmKdgqFKUmpvjkKdjZ2SoUlJYElxQp6Cmk/TcxAgA "Haskell – TIO Nexus") The y-axis is flipped. Usage: `f 42` returns a newline delimited string.
This is a nested list comprehension where both `x` and `y` are drawn from the range `[0..n-1]`. `last$' ':['X'|mod(x*x+y*y)n==1]` is a shorter form of `if mod(x*x+y*y)n==1 then 'X' else ' '`. The list comprehension evaluates to a list of strings which is turned into a single newline separated string by `unlines`.
[Answer]
## Mathematica, 56 48 bytes
*Edit: Thanks to Greg Martin and Martin Ender for saving 8 bytes.*
```
Grid@Array[If[Mod[#^2+#2^2,x]==1,X]&,{x=#,#},0]&
```
## Original solution:
```
Grid@Table[If[Tr[{i-1,j-1}^2]~Mod~#==1,X,],{i,#},{j,#}]&
```
[Answer]
## [CJam](https://sourceforge.net/p/cjam), 23 bytes
```
ri:X,2f#_ff{+X%(S'X?}N*
```
[Try it online!](https://tio.run/nexus/cjam#@1@UaRWhY5SmHJ@WVq0doaoRrB5hX@un9f@/iREA "CJam – TIO Nexus")
```
ri:X    e# Read input, convert to integer, store in X.
,       e# Turn into range [0 1 ... X-1].
2f#     e# Square each value in the range.
_ff{    e# 2D map over all pairs from that list.
  +     e#   Add the two values in the current pair.
  X%    e#   Take the sum modulo X.
  (     e#   Decrement, so that x^2+y^2==1 becomes 0 (falsy) and everything
        e#   else becomes truthy.
  S'X?  e#   Select space of 'X' accordingly.
}
N*      e# Join rows with linefeeds.
```
[Answer]
## JavaScript (ES6), 81 bytes
```
f=
n=>[...Array(n)].map((_,x,a)=>a.map((_,y)=>(x*x+y*y)%n-1?` `:`X`).join``).join`
`
```
```
<input type=number oninput=o.textContent=f(+this.value)><pre id=o>
```
The Y-axis is the reverse of the OP.
[Answer]
# [Röda](https://github.com/fergusq/roda), 74 bytes
```
f n{seq n-1,0|{|y|seq 0,n-1|{|x|["X"]if[(x^2+y^2)%n=1]else[" "]}_;["
"]}_}
```
[Try it online!](https://tio.run/nexus/roda#@5@mkFddnFqokKdrqGNQU11TWQPiGegA@UBeRU20UoRSbGZatEZFnJF2ZZyRpmqerWFsak5xarSSglJsbbx1tBIXiK79n5uYmadQzcWZpmBixFX7HwA "Röda – TIO Nexus")
Ungolfed:
```
function f(n) {
    seq(n-1, 0) | for y do
        seq(0, n-1) | for x do
            if [ (x^2 + y^2) % n = 1 ] do
                push("X")
            else
                push(" ")
            done
        done
        print("")
    done
}
```
[Answer]
# [Python 3](https://docs.python.org/3/), ~~87~~ 83 bytes
```
lambda n:"\n".join("".join(" X"[(y*y+x*x)%n==1]for x in range(n))for y in range(n))
```
[Try it online!](https://tio.run/nexus/python3#S7ON@Z@TmJuUkqiQZ6UUk6ekl5WfmaehBKMVIpSiNSq1KrUrtCo0VfNsbQ1j0/KLFCoUMvMUihLz0lM18jQ1QSKVKCL/QUKZIKFoIx0FYx0FUx0Fcx0FQ0MdBROjWCsuhYKizLwSjUxNGCsNyIZzNP8DAA "Python 3 – TIO Nexus")
The y-axis is inverted
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~14~~ 13 bytes
```
R²+þ`%=1ị⁾X Y
```
The x-axis is inverted.
[Try it online!](https://tio.run/nexus/jelly#@x90aJP24X0JqraGD3d3P2rcF6EQ@f//fxMjAA "Jelly – TIO Nexus")
### How it works
```
R²+þ`%=1ị⁾X Y  Main link. Argument: n
R              Range; yield [1, ..., n].
 ²             Square; yield [1², ..., n²].
  +þ`          Self table addition; compute x+y for all x and y in [1², ..., n²],
               grouping by the values of y.
     %         Take all sums modulo n.
      =1       Compare them with 1, yielding 1 or 0.
        ị⁾X    Index into "X ".
            Y  Separate by linefeeds.
```
[Answer]
# [dc](https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html), 79 bytes
```
?dsRsQ[88P]sl[32P]sH[0sM[lM2^lR2^+lQ%d1=l1!=HlM1+dsMlQ>c]dscx10PlR1-dsR0<S]dsSx
```
The `y`-axis is inverted whereas the `x`-axis is not.
[Try it online!](https://tio.run/nexus/dc#@2@fUhxUHBhtYREQW5wTbWwEpDyiDYp9o3N8jeJygozitHMCVVMMbXMMFW09cnwNtVOKfXMC7ZJjU4qTKwwNAnKCDHWBRhjYBANFgiv@/zcxAgA "dc – TIO Nexus")
[Answer]
# [Python 3](https://docs.python.org/3/), (102 98 95 bytes)
### y-axis inverted
```
n=int(input());r=range(n);p=print
for i in r:
 for j in r:p(end=' 'if(i*i+j*j)%n-1else'X')
 p()
```
[Try it online!](https://tio.run/nexus/python3#JckxCoAwDADAva/IIk0UB8VJ6T9cBVtJkRiivr8qjscVCSwXsuh9IdFkwRbZIgpNGtTec@kwYGABGx18yD8Uo6zBg@eEXHOT60yVtF3cz@hnTw4UqZShdw8 "Python 3 – TIO Nexus")
* saved 4 bytes: omitted variable c in c=' 'if(i*i+j*j)%n-1else'X'
* saved 3 bytes: Thanks to  [ovs](https://codegolf.stackexchange.com/users/64121/ovs) (modified print statement)
[Answer]
## [Lithp](https://github.com/andrakis/node-lithp), 125 bytes
```
#N::((join(map(seq(- N 1)0)(scope #Y::((join(map(seq 0(- N 1))(scope #X::
((?(== 1(@(+(* X X)(* Y Y))N))"X" " "))))""))))"\n")
```
Linebreak for readability.
[Try it online!](https://andrakis.github.io/ide2/index.html?code=JSBBbiBlbnRyeSBmb3IgYSBDb2RlZ29sZiBjaGFsbGVuZ2U6CiUgaHR0cDovL2NvZGVnb2xmLnN0YWNrZXhjaGFuZ2UuY29tL3F1ZXN0aW9ucy8xMTQ4NDcvZHJhdy1tZS10aGUtd2VpcmQtdW5pdC1jaXJjbGUKJSBGb3IgYmVzdCByZXN1bHRzLCBleHBhbmQgdGhlIG91dHB1dCB3aW5kb3cgYmVsb3cuCigKICAgIChpbXBvcnQgbGlzdHMpCiAgICAlIFVuZ29sZmVkOgogICAgKGRlZiBmICNOIDo6ICgKICAgICAgICAoam9pbiAobWFwIChzZXEgKC0gTiAxKSAwKSAoc2NvcGUgI1kgOjogKAogICAgICAgICAgICAoam9pbiAobWFwIChzZXEgMCAoLSBOIDEpKSAoc2NvcGUgI1ggOjogKAogICAgICAgICAgICAgICAgKD8gKD09IDEgKEAgKCsgKCogWCBYKSAoKiBZIFkpKSBOKSkgIlgiICIgIikKICAgICAgICAgICAgKSkpICIiKQogICAgICAgICkpKSAiXG4iKQogICAgKSkKICAgICUgR29sZmVkOgogICAgKGRlZiBmICNOOjooKGpvaW4obWFwKHNlcSgtIE4gMSkwKShzY29wZSAjWTo6KChqb2luKG1hcChzZXEgMCgtIE4gMSkpKHNjb3BlICNYOjooKD8oPT0gMShAKCsoKiBYIFgpKCogWSBZKSlOKSkiWCIgIiAiKSkpKSIiKSkpKSJcbiIpKSkKICAgIAogICAgJSBUZXN0czoKICAgIChlYWNoIChzZXEgMiAzMCkgKHNjb3BlICNOIDo6ICgKICAgICAgICAocHJpbnQgIkZvciBOPSIgTiAiOlxuIiAoZiBOKSkKICAgICkpKQogICAgKHByaW50IChmIDQwKSkKKQ==)
Not the shortest. I think I need some sort of shorthand module. See the Try it Online link for further explanation, ungolfed version, and some tests. For best results, expand the output window to see more.
[Answer]
# [Python 3](https://docs.python.org/3/), 82 bytes
```
f=lambda n,k=0:k<n>f(n,k+1)!=print(''.join(' X'[(k*k+j*j)%n==1]for j in range(n)))
```
[Try it online!](https://tio.run/nexus/python3#DcZBDkAwEADAr6yDdFdFECex3iERhwqVtixp/L/MaZLl01zrZkDKwHUfBhkt/tcNZfxEJy8qVfnbCSqY1IyhCNoXnnJhbhZ7R/DgBKKRY0chomSxayl9 "Python 3 – TIO Nexus")
[Answer]
# [GNU APL](https://www.gnu.org/software/apl/), 41 chars, 59 bytes
Reads an integer and displays the circle.
```
N←⎕◊⌽{(⍵+1)⊃' ' 'X'}¨{1=(N|(+/⍵*2))}¨⍳N N
```
### Ungolfed
```
N←⎕
⌽                           ⍝ flip the X axis so 0,0 is bottom left
{
    (⍵+1) ⊃ ' ' 'X'         ⍝ substitute space for 0, X for 1
} ¨ {
    1=(N|(+/⍵*2))           ⍝ mod(x*x+y*y, 1)==1
} ¨ ⍳N N                    ⍝ generate an NxN grid of coordinates
```
[Answer]
# [MATL](https://github.com/lmendo/MATL), 13 bytes
```
:qU&+G\1=88*c
```
Origin is at top left. So the output is flipped upside down compared with the examples in the challenge.
Try at [MATL online!](https://matl.io/?code=%3AqU%26%2BG%5C1%3D88%2ac&inputs=11&version=19.9.0)
### Explanation
```
:      % Input n implicitly. Push [1 2 ... n]
q      % Subtract one (element-wise)
U      % Square (element-wise)
&+     % Matrix of pairwise sums
G      % Push n
\      % Modulo
1=     % Equal to 1? (element-wise)
88*    % Multiply by 88 (ASCII code of 'X')
c      % Convert to char. Char 0 will be displayed as a space
       % Display implicitly
```
[Answer]
# Haskell, 115 bytes
```
n#(a,b)|mod(a*a+b*b)n==1='X'|1>0=' '
m n=map(n#)<$>zipWith(zipWith(,))(replicate n[0..n-1])(replicate n<$>[0..n-1])
```
The y axis is inverted.
[Try it online!](https://tio.run/nexus/haskell#VctBCsJADEDRvacItDBJraXj2vQaCuIiowUDJg5lVtK7j24UXH148Ks3KH2i1Z43lE62qUvkzJHDKaxxGjlA2Bg4m2T0hg7t9NJ81HLHb3siXOb80KuUGfw8DoPv4uUPP9vPq4k6MORFvUALBvv6Bg "Haskell – TIO Nexus")
All those parentheses are kind of annoying me...
## Explanation
```
n#(a,b)|mod(a*a+b*b)n==1='X'|1>0=' '
n#(a,b)                                 --Operator #, takes a number n and a tuple (a,b)
       |mod(a*a+b*b)n==1                --Test if the mod equals 1
                        ='X'            --If so, return 'X'
                            |1>0=' '    --Otherwise, return ' '
m n=map(n#)<$>zipWith(zipWith(,))(replicate n[0..n-1])(replicate n<$>[0..n-1])
m n=                                                                           --Make a new function m with argument n
                                 (replicate n[0..n-1])                         --Make a list of [[0,1,2,3..n-1],[0,1,2,3..n-1],(n times)]
                                                      (replicate n<$>[0..n-1]) --Make a list of [[0,0,0(n times)],[1,1,1(n times)]..[n-1,n-1,n-1(n times)]
              zipWith(zipWith(,))                                              --Combine them into a list of list of tuples
    map(n#)<$>                                                                 --Apply the # operator to every tuple in the list with the argument n
```
[Answer]
# [J](http://jsoftware.com/), 20 bytes
```
' x'{~1=[|+/~@:*:@i.
```
[Try it online!](https://tio.run/nexus/j#@59ma6WuUKFeXWdoG12jrV/nYKVl5ZCp9z81OSNfIU3BxIgrtSKzRMHgPwA "J – TIO Nexus")
[Answer]
# [GolfScript](http://www.golfscript.com/golfscript/), 34 bytes
```
~:c,{.*}%:a{:b;a{b+c%1=' x'=}%}%n*
```
[Try it online!](https://tio.run/nexus/golfscript#@19nlaxTradVq2qVWG2VZJ1YnaSdrGpoq65QoW5bq1qrmqf1/7@JEQA "GolfScript – TIO Nexus")
I really don't like using variables...
]