###Explanation
###Explanation
Explanation
Reflections, 194194 181 bytes
_v@\ |* / (0 / \ /;* < /0):\(1/# + /#+\ : ; >~< /\ _ / #|v\/ 1) \ / (0* \#:(1 \ \\# \(0__0) / _ / (0\ /^^: 0):/ \#+ _#_ /0):^\ : / / # (0 >#* _#_ \ _< \ / 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 again0)pulls the input number from stack 0/reflects the IP down:duplicates the input#redefines (0|0)(0pushes 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=0vpops the 0 and reflects the IP left|reflects the IP rightvreflects the IP down*pushes 0×-1=0#redefines (0|0)^pops the zero and reflects the IP right:duplicates the counter0)pulls the input from stack 0:duplicates it/reflects the IP up\reflects the IP left(0pushes 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)(0pushes 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
Reflections, 194 bytes
_v@\ |* / (0 / \ /;* < /0):\(1/# + /#+\ : ; >~< / _ / #|v\/ 1) \ / (0* \#:(1 \ \\# \(0__0) / _ / (0\ /^^: 0):/ \#+ _#_ /0):^\ : / / # (0 >#* _#_ \ _< \ / I'm going to add an explanation for the real primality test, which just takes the input n modulo every number i (1 < i < n) and prints zero if the result is zero.
Reflections, 194 181 bytes
_v@\ |* / (0 /\ /;* < /0):\(1/# + /#+\ : ; >~< \ _ / #|v\/ 1) (0* \#:(1 \ \\# \(0__0) / _ / (0\ /^^: 0):/ \#+ _#_ /0):^\ : / / # (0 >#* _#_ \ _< \ / Now, we have the test number on stack 0.
Then, we initialise the loop:
< /# + / /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 (the counter) on the main stack and the input 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 again0)pulls the input number from stack 0/reflects the IP down:duplicates the input#redefines (0|0)(0pushes 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=0vpops the 0 and reflects the IP left|reflects the IP rightvreflects the IP down*pushes 0×-1=0#redefines (0|0)^pops the zero and reflects the IP right:duplicates the counter0)pulls the input from stack 0:duplicates it/reflects the IP up\reflects the IP left(0pushes 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)(0pushes 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
Reflections, 194 bytes
_v@\ |* / (0 / \ /;* < /0):\(1/# + /#+\ : ; >~< / _ / #|v\/ 1) \ / (0* \#:(1 \ \\# \(0__0) / _ / (0\ /^^: 0):/ \#+ _#_ /0):^\ : / / # (0 >#* _#_ \ _< \ / Outputs 1 for prime, else 0.
###Explanation
First we parse the number:
_v@\ |* / (0 * (1/ -> IP leaves here >~< 1) \#:(1 \ \(0__0) / _reads a line from inputvreflects the IP down/reflects the IP left*at (1|1) pushes 1×1=1|reflects the IP right*pushes another 1/reflects the IP upvpops a value off the stack and reflects the IP right as it's true@at (4|0) converts all input to numbers\reflects the IP down(0moves the first digit to stack 0*at (5|2) pushes 5×2=10(1moves the 10 to stack 1>reflects the IP right~pushes the number of left digits<pops that number and reflects the IP down if it's not 0:1)moves the 10 from stack 1 to the main stack\reflects the IP right#redefines (0|0):doubles the 10(1moves the top 10 to stack 1 again\reflects the IP down/reflects the IP left0)pulls the previous result from stack 0_at (1|1) multiplies the previous result and 10_at (0|1) adds the next digit(0pushes the result to stack 0\reflects the IP up>enters the loop again
- if it's zero, reflect the IP up
/reflects the IP right
I'm going to add an explanation for the real primality test, which just takes the input n modulo every number i (1 < i < n) and prints zero if the result is zero.