_v@\
|* / (0 / \
/;* <
/0):\(1/# + /#+\
: ; >~< /\ _ /
#|v\/ 1) \ /
(0* \#:(1 \
\\# \(0__0) /
_ / (0\
/^^: 0):/
\#+ _#_
/0):^\
: / /
#
(0 >#* _#_
\ _<
\ /
Test it!Test it!
I'm going to add an explanation forNow, we have the real primality test number on stack 0.
Then, which just takeswe initialise the input n modulo every number iloop:
<
/# + /
/
reflects the IP right
#
redefines (0|0)
+
at (2|0) pushes 2+0=2
/
reflects the IP up
<
reflects the IP left
Now, we have a 2 (1 < i < nthe counter) on the main stack and prints zero if the result is zeroinput number on stack 0.
Then we have the real loop:
/\
/;* <
/0):\ /#+\
: ; \ _ /
#|v\/
(0*
\\#
_ / (0\
/^^: 0):/
\#+ _#_
/0):^\
: / /
#
(0 >#* _#_
\ _<
\ /
*
pushes x×y
;
pops that again
/
reflects the IP down
:
duplicates the counter
;
discards the duplicate
\
reflects the IP right
/
reflects the IP up
\
reflects the IP left
:
duplicates the counter again
0)
pulls the input number from stack 0
/
reflects the IP down
:
duplicates the input
#
redefines (0|0)
(0
pushes the duplicated input to stack 0
\
reflects the IP right
\
reflects the IP down
_
at (1|3) pops the counter and the input and pushes whether they're equal
^
pops the test and reflects the IP left if true (i.e. if we have tested all numbers less than the input and haven't found a factor → the number is prime):
/
reflects the IP down
\
reflects the IP right
#
redefines (0|0)
+
at (1|0) pushes 1+0=1
_
at (3|0) converts to string
#
redefines (0|0)
_
at (1|0) prints
- then the IP leaves the grid and the program ends
- else the IP is reflected right:
^
reflects the IP up
#
redefines (0|0)
*
pushes 0×-1=0
v
pops the 0 and reflects the IP left
|
reflects the IP right
v
reflects the IP down
*
pushes 0×-1=0
#
redefines (0|0)
^
pops the zero and reflects the IP right
:
duplicates the counter
0)
pulls the input from stack 0
:
duplicates it
/
reflects the IP up
\
reflects the IP left
(0
pushes the duplicated input to stack 0
/
reflects the IP down
_
at (2|3) checks if the input is greater than the counter. Note that this is only false if the input is 1 as else the previous check applies before.
^
reflects the IP right if the check was false (i.e. input is < 2):
\
reflects the IP down
/
reflects the IP left
/
reflects the IP down
>
enters the 'output zero' part, see below
- else the IP is reflected left:
:
duplicates the counter (once again)
0)
pulls the input from stack 0 (once again)
:
duplicates the input (once again)
#
redefines (0|0)
(0
pushes the input to stack 0 (once again)
\
reflects the IP right
_
at (2|2) pops input and counter and pushes input modulo counter
<
pops the result and reflects the IP up if 0 (it's a factor):
>
reflects the IP right into the 'output zero' part, see below
- else (it's no factor) the IP is reflected down:
\
reflects the IP right
/
reflects the IP up
/
reflects the IP right
#
redefines (0|0)
+
at (1|0) pushes 1+0=1
\
reflects the IP down
/
reflects the IP left
_
at (0|1) adds the 1 to the counter (counter++)
\
reflects the IP up
/
reflects the IP right
\
reflects the IP down
<
enters the loop again
Now for the 'output zero' part:
>#* _#_
>
reflects the IP right
#
redefines (0|0)
*
at (1|0) pushes 1×0=0
_
at (3|0) converts to string
#
redefines (0|0)
_
at (1|0) prints '0'
- then the IP leaves the grid and the program ends