Skip to main content
Commonmark migration
Source Link

###Explanation

Explanation

###Explanation

Explanation

added explanation, improved score
Source Link
wastl
  • 3.9k
  • 14
  • 33

Reflections, 194194 181 bytes

  _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

Reflections, 194 bytes

  _v@\
|* / (0     /   \
   /;*      <
/0):\(1/# + /#+\
:  ; >~<   / _ /
#|v\/ 1)   \    /
(0*    \#:(1 \
\\#  \(0__0) /
 _  / (0\
/^^: 0):/
\#+ _#_
/0):^\
:  / /
#
(0 >#* _#_
\ _<
   \        /

Test it!

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 >#* _#_
\ _<
   \        /

Test it!

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 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
Source Link
wastl
  • 3.9k
  • 14
  • 33

Reflections, 194 bytes

  _v@\
|* / (0     /   \
   /;*      <
/0):\(1/# + /#+\
:  ; >~<   / _ /
#|v\/ 1)   \    /
(0*    \#:(1 \
\\#  \(0__0) /
 _  / (0\
/^^: 0):/
\#+ _#_
/0):^\
:  / /
#
(0 >#* _#_
\ _<
   \        /

Test it!

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 input
  • v reflects the IP down
  • / reflects the IP left
  • * at (1|1) pushes 1×1=1
  • | reflects the IP right
  • * pushes another 1
  • / reflects the IP up
  • v pops 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
  • (0 moves the first digit to stack 0
  • * at (5|2) pushes 5×2=10
  • (1 moves 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
    • (1 moves the top 10 to stack 1 again
    • \ reflects the IP down
    • / reflects the IP left
    • 0) pulls the previous result from stack 0
    • _ at (1|1) multiplies the previous result and 10
    • _ at (0|1) adds the next digit
    • (0 pushes 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.