Skip to main content
deleted 219 characters in body
Source Link
Kevin Cruijssen
  • 136.3k
  • 14
  • 155
  • 394

#Whitespace, 145 137137 134 bytes

[S S S N _Push_0][S N S _Duplicate][T N T T _Read_STDIN_as_integer_in_heap_0][T T T _Retrieve][S S S T S N _Push_2][T S S T _Subtract][N T T T T N _Jump_to_Label_FALSE_if_negative][S S S T N _Push_1][S N S _Duplicate_1][S N S _Duplicate_1][N S S N _Create_Label_LOOP][S N N _Discard_top_stack][S S S T N _Push_1][T S S S _Add][S N S _Duplicate][S N S _Duplicate][S S S N _Push_0][T T T _Retrieve_heap_at_0][T S S T _subtract][N T S T N _Jump_to_Label_TRUE_if_0][S S S N _Push_0][T T T _Retrieve_heap_0][S N T _Swap_top_two][T S T T _Modulo][S N S _Duplicate][N T S T T N _Jump_to_Label_FALSE_if_0][N S N N _Jump_to_Label_LOOP][N S S T N _Create_Label_TRUE][S S S T N _Push_1][T N S T _Output_1][N N N _Exit][N S S T T N _Create_Label_FALSE][S S S N _Push_0][T N S T _Output_0] 
Command Explanation Stack  Heap  STDIN  STDOUT SSSN Push 0 [0]  {} SNS Duplicate (0) [0,0]  {} TNTT Read STDIN as number [0]  {0:5}  5 TTT Retrieve heap at 0 [5]  {0:5} SSSTSN Push 2 [5,2]  {0:5} TSST Subtract (5-2) [3]  {0:5} NTTTTN Jump to Label_FALSE if neg. []  {0:5} SSSTN Push 1 [1]  {0:5} SNS Duplicate (1) [1,1]  {0:5} SNS Duplicate (1) [1,1,1] {0:5} NSSN Create Label_LOOP [1,1,1] {0:5} SNN Discard top [1,1][1] {0:5} SSSTN Push 1 [1,1,1] {0:5} TSSS Add (1+1) [1,2][2] {0:5} SNS Duplicate (2) [1,2[2,2] {0:5} SNS Duplicate (2) [1,2[2,2,2] {0:5} SSSN Push 0 [1,2[2,2,2,0] {0:5} TTT Retrieve heap at 0 [1,2[2,2,2,5] {0:5} TSST Subtract (2-5) [1,2[2,2,-3] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,2[2,2] {0:5} SSSN Push 0 [1,2[2,2,0] {0:5} TTT Retrieve heap at 0 [1,2[2,2,5] {0:5} SNT Swap top two (2,5] -> 5,2]) [1,2[2,5,2] {0:5} TSTT Modulo (5%2) [1,2[2,1] {0:5} SNS Duplicate (1) [1,2[2,1,1] {0:5} NTSTTN Jump to Label_FALSE if 0 [1,2[2,1] {0:5} NSNN Jump to Label_LOOP [1,2[2,1] {0:5} SNN Discard top [1,2][2] {0:5} SSSTN Push 1 [1,2[2,1] {0:5} TSSS Add (2+1) [1,3][3] {0:5} SNS Duplicate (3) [1,3[3,3] {0:5} SNS Duplicate (3) [1,3[3,3,3] {0:5} SSSN Push 0 [1,3[3,3,3,0] {0:5} TTT Retrieve heap at 0 [1,3[3,3,3,5] {0:5} TSST Subtract (3-5) [1,3[3,3,-2] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,3[3,3] {0:5} SSSN Push 0 [1,3[3,3,0] {0:5} TTT Retrieve heap at 0 [1,3[3,3,5] {0:5} SNT Swap top two (3,5] -> 5,3]) [1,3[3,5,3] {0:5} TSTT Modulo (5%3) [1,3[3,2] {0:5} SNS Duplicate (2) [1,3[3,2,2] {0:5} NTSTTN Jump to Label_FALSE if 0 [1,3[3,2] {0:5} NSNN Jump to Label_LOOP [1,3[3,2] {0:5} SNN Discard top [1,3][3] {0:5} SSSTN Push 1 [1,3[3,1] {0:5} TSSS Add (3+1) [1,4][4] {0:5} SNS Duplicate (4) [1,4[4,4] {0:5} SNS Duplicate (4) [1,4[4,4,4] {0:5} SSSN Push 0 [1,4[4,4,4,0] {0:5} TTT Retrieve heap at 0 [1,4[4,4,4,5] {0:5} TSST Subtract (4-5) [1,4[4,4,-1] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,4[4,4] {0:5} SSSN Push 0 [1,4[4,4,0] {0:5} TTT Retrieve heap at 0 [1,4[4,4,5] {0:5} SNT Swap top two (4,5] -> 5,4]) [1,4[4,5,4] {0:5} TSTT Modulo (5%4) [1,4[4,1] {0:5} SNS Duplicate (1) [1,4[4,1,1] {0:5} NTSTTN Jump to Label_FALSE if 0 [1,4[4,1] {0:5} NSNN Jump to Label_LOOP [1,4[4,1] {0:5} SNN Discard top [1,4][4] {0:5} SSSTN Push 1 [1,4[4,1] {0:5} TSSS Add (4+1) [1,5][5] {0:5} SNS Duplicate (5) [1,5[5,5] {0:5} SNS Duplicate (5) [1,5[5,5,5] {0:5} SSSN Push 0 [1,5[5,5,5,0] {0:5} TTT Retrieve heap at 0 [1,5[5,5,5,5] {0:5} TSST Subtract (5-5) [1,5[5,5,0] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,5[5,5] {0:5} NSSTN Create Label_TRUE [1,5[5,5] {0:5} SSSTN Push 1 [1,5[5,5,1] {0:5} TNST Output top as number [1,5[5,5] {0:5}  1 NNN Stop program [1,5[5,5] {0:5} 

#Whitespace, 145 137 bytes

[S S S N _Push_0][S N S _Duplicate][T N T T _Read_STDIN_as_integer_in_heap_0][T T T _Retrieve][S S S T S N _Push_2][T S S T _Subtract][N T T T T N _Jump_to_Label_FALSE_if_negative][S S S T N _Push_1][S N S _Duplicate_1][S N S _Duplicate_1][N S S N _Create_Label_LOOP][S N N _Discard_top_stack][S S S T N _Push_1][T S S S _Add][S N S _Duplicate][S N S _Duplicate][S S S N _Push_0][T T T _Retrieve_heap_at_0][T S S T _subtract][N T S T N _Jump_to_Label_TRUE_if_0][S S S N _Push_0][T T T _Retrieve_heap_0][S N T _Swap_top_two][T S T T _Modulo][S N S _Duplicate][N T S T T N _Jump_to_Label_FALSE_if_0][N S N N _Jump_to_Label_LOOP][N S S T N _Create_Label_TRUE][S S S T N _Push_1][T N S T _Output_1][N N N _Exit][N S S T T N _Create_Label_FALSE][S S S N _Push_0][T N S T _Output_0] 
Command Explanation Stack  Heap STDIN STDOUT SSSN Push 0 [0]  {} SNS Duplicate (0) [0,0]  {} TNTT Read STDIN as number [0]  {0:5} 5 TTT Retrieve heap at 0 [5]  {0:5} SSSTSN Push 2 [5,2]  {0:5} TSST Subtract (5-2) [3]  {0:5} NTTTTN Jump to Label_FALSE if neg. []  {0:5} SSSTN Push 1 [1]  {0:5} SNS Duplicate (1) [1,1]  {0:5} SNS Duplicate (1) [1,1,1] {0:5} NSSN Create Label_LOOP [1,1,1] {0:5} SNN Discard top [1,1] {0:5} SSSTN Push 1 [1,1,1] {0:5} TSSS Add (1+1) [1,2] {0:5} SNS Duplicate (2) [1,2,2] {0:5} SNS Duplicate (2) [1,2,2,2] {0:5} SSSN Push 0 [1,2,2,2,0] {0:5} TTT Retrieve heap at 0 [1,2,2,2,5] {0:5} TSST Subtract (2-5) [1,2,2,-3] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,2,2] {0:5} SSSN Push 0 [1,2,2,0] {0:5} TTT Retrieve heap at 0 [1,2,2,5] {0:5} SNT Swap top two (2,5] -> 5,2]) [1,2,5,2] {0:5} TSTT Modulo (5%2) [1,2,1] {0:5} SNS Duplicate (1) [1,2,1,1] {0:5} NTSTTN Jump to Label_FALSE if 0 [1,2,1] {0:5} NSNN Jump to Label_LOOP [1,2,1] {0:5} SNN Discard top [1,2] {0:5} SSSTN Push 1 [1,2,1] {0:5} TSSS Add (2+1) [1,3] {0:5} SNS Duplicate (3) [1,3,3] {0:5} SNS Duplicate (3) [1,3,3,3] {0:5} SSSN Push 0 [1,3,3,3,0] {0:5} TTT Retrieve heap at 0 [1,3,3,3,5] {0:5} TSST Subtract (3-5) [1,3,3,-2] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,3,3] {0:5} SSSN Push 0 [1,3,3,0] {0:5} TTT Retrieve heap at 0 [1,3,3,5] {0:5} SNT Swap top two (3,5] -> 5,3]) [1,3,5,3] {0:5} TSTT Modulo (5%3) [1,3,2] {0:5} SNS Duplicate (2) [1,3,2,2] {0:5} NTSTTN Jump to Label_FALSE if 0 [1,3,2] {0:5} NSNN Jump to Label_LOOP [1,3,2] {0:5} SNN Discard top [1,3] {0:5} SSSTN Push 1 [1,3,1] {0:5} TSSS Add (3+1) [1,4] {0:5} SNS Duplicate (4) [1,4,4] {0:5} SNS Duplicate (4) [1,4,4,4] {0:5} SSSN Push 0 [1,4,4,4,0] {0:5} TTT Retrieve heap at 0 [1,4,4,4,5] {0:5} TSST Subtract (4-5) [1,4,4,-1] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,4,4] {0:5} SSSN Push 0 [1,4,4,0] {0:5} TTT Retrieve heap at 0 [1,4,4,5] {0:5} SNT Swap top two (4,5] -> 5,4]) [1,4,5,4] {0:5} TSTT Modulo (5%4) [1,4,1] {0:5} SNS Duplicate (1) [1,4,1,1] {0:5} NTSTTN Jump to Label_FALSE if 0 [1,4,1] {0:5} NSNN Jump to Label_LOOP [1,4,1] {0:5} SNN Discard top [1,4] {0:5} SSSTN Push 1 [1,4,1] {0:5} TSSS Add (4+1) [1,5] {0:5} SNS Duplicate (5) [1,5,5] {0:5} SNS Duplicate (5) [1,5,5,5] {0:5} SSSN Push 0 [1,5,5,5,0] {0:5} TTT Retrieve heap at 0 [1,5,5,5,5] {0:5} TSST Subtract (5-5) [1,5,5,0] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,5,5] {0:5} NSSTN Create Label_TRUE [1,5,5] {0:5} SSSTN Push 1 [1,5,5,1] {0:5} TNST Output top as number [1,5,5] {0:5} 1 NNN Stop program [1,5,5] {0:5} 

#Whitespace, 145 137 134 bytes

[S S S N _Push_0][S N S _Duplicate][T N T T _Read_STDIN_as_integer_in_heap_0][T T T _Retrieve][S S S T S N _Push_2][T S S T _Subtract][N T T T T N _Jump_to_Label_FALSE_if_negative][S S S T N _Push_1][S N S _Duplicate_1][N S S N _Create_Label_LOOP][S N N _Discard_top_stack][S S S T N _Push_1][T S S S _Add][S N S _Duplicate][S N S _Duplicate][S S S N _Push_0][T T T _Retrieve_heap_at_0][T S S T _subtract][N T S T N _Jump_to_Label_TRUE_if_0][S S S N _Push_0][T T T _Retrieve_heap_0][S N T _Swap_top_two][T S T T _Modulo][S N S _Duplicate][N T S T T N _Jump_to_Label_FALSE_if_0][N S N N _Jump_to_Label_LOOP][N S S T N _Create_Label_TRUE][S S S T N _Push_1][T N S T _Output_1][N N N _Exit][N S S T T N _Create_Label_FALSE][S S S N _Push_0][T N S T _Output_0] 
Command Explanation Stack Heap  STDIN  STDOUT SSSN Push 0 [0] {} SNS Duplicate (0) [0,0] {} TNTT Read STDIN as number [0] {0:5}  5 TTT Retrieve heap at 0 [5] {0:5} SSSTSN Push 2 [5,2] {0:5} TSST Subtract (5-2) [3] {0:5} NTTTTN Jump to Label_FALSE if neg. [] {0:5} SSSTN Push 1 [1] {0:5} SNS Duplicate (1) [1,1] {0:5} NSSN Create Label_LOOP [1,1] {0:5} SNN Discard top [1] {0:5} SSSTN Push 1 [1,1] {0:5} TSSS Add (1+1) [2] {0:5} SNS Duplicate (2) [2,2] {0:5} SNS Duplicate (2) [2,2,2] {0:5} SSSN Push 0 [2,2,2,0] {0:5} TTT Retrieve heap at 0 [2,2,2,5] {0:5} TSST Subtract (2-5) [2,2,-3] {0:5} NTSTN Jump to LABEL_TRUE if 0 [2,2] {0:5} SSSN Push 0 [2,2,0] {0:5} TTT Retrieve heap at 0 [2,2,5] {0:5} SNT Swap top two (2,5] -> 5,2]) [2,5,2] {0:5} TSTT Modulo (5%2) [2,1] {0:5} SNS Duplicate (1) [2,1,1] {0:5} NTSTTN Jump to Label_FALSE if 0 [2,1] {0:5} NSNN Jump to Label_LOOP [2,1] {0:5} SNN Discard top [2] {0:5} SSSTN Push 1 [2,1] {0:5} TSSS Add (2+1) [3] {0:5} SNS Duplicate (3) [3,3] {0:5} SNS Duplicate (3) [3,3,3] {0:5} SSSN Push 0 [3,3,3,0] {0:5} TTT Retrieve heap at 0 [3,3,3,5] {0:5} TSST Subtract (3-5) [3,3,-2] {0:5} NTSTN Jump to LABEL_TRUE if 0 [3,3] {0:5} SSSN Push 0 [3,3,0] {0:5} TTT Retrieve heap at 0 [3,3,5] {0:5} SNT Swap top two (3,5] -> 5,3]) [3,5,3] {0:5} TSTT Modulo (5%3) [3,2] {0:5} SNS Duplicate (2) [3,2,2] {0:5} NTSTTN Jump to Label_FALSE if 0 [3,2] {0:5} NSNN Jump to Label_LOOP [3,2] {0:5} SNN Discard top [3] {0:5} SSSTN Push 1 [3,1] {0:5} TSSS Add (3+1) [4] {0:5} SNS Duplicate (4) [4,4] {0:5} SNS Duplicate (4) [4,4,4] {0:5} SSSN Push 0 [4,4,4,0] {0:5} TTT Retrieve heap at 0 [4,4,4,5] {0:5} TSST Subtract (4-5) [4,4,-1] {0:5} NTSTN Jump to LABEL_TRUE if 0 [4,4] {0:5} SSSN Push 0 [4,4,0] {0:5} TTT Retrieve heap at 0 [4,4,5] {0:5} SNT Swap top two (4,5] -> 5,4]) [4,5,4] {0:5} TSTT Modulo (5%4) [4,1] {0:5} SNS Duplicate (1) [4,1,1] {0:5} NTSTTN Jump to Label_FALSE if 0 [4,1] {0:5} NSNN Jump to Label_LOOP [4,1] {0:5} SNN Discard top [4] {0:5} SSSTN Push 1 [4,1] {0:5} TSSS Add (4+1) [5] {0:5} SNS Duplicate (5) [5,5] {0:5} SNS Duplicate (5) [5,5,5] {0:5} SSSN Push 0 [5,5,5,0] {0:5} TTT Retrieve heap at 0 [5,5,5,5] {0:5} TSST Subtract (5-5) [5,5,0] {0:5} NTSTN Jump to LABEL_TRUE if 0 [5,5] {0:5} NSSTN Create Label_TRUE [5,5] {0:5} SSSTN Push 1 [5,5,1] {0:5} TNST Output top as number [5,5] {0:5}  1 NNN Stop program [5,5] {0:5} 
deleted 181 characters in body
Source Link
Kevin Cruijssen
  • 136.3k
  • 14
  • 155
  • 394

#Whitespace, 145145 137 bytes

[S S S N _Push_0][S N S _Duplicate][T N T T _Read_STDIN_as_integer_in_heap_0][T T T _Retrieve][S S S T S N _Push_2][T S S T _Subtract][S N S _Duplicate][N_Subtract][N T T T T N _Jump_to_Label_FALSE_if_negative][N T S T N _Jump_to_Label_TRUE_if_0][S_Jump_to_Label_FALSE_if_negative][S S S T N _Push_1][S N S _Duplicate_1][S N S _Duplicate_1][N S S N _Create_Label_LOOP][S N N _Discard_top_stack][S S S T N _Push_1][T S S S _Add][S N S _Duplicate][S N S _Duplicate][S S S N _Push_0][T T T _Retrieve_heap_at_0][T S S T _subtract][N T S T N _Jump_to_Label_TRUE_if_0][S S S N _Push_0][T T T _Retrieve_heap_0][S N T _Swap_top_two][T S T T _Modulo][S N S _Duplicate][N T S T T N _Jump_to_Label_FALSE_if_0][N S N N _Jump_to_Label_LOOP][N S S T N _Create_Label_TRUE][S S S T N _Push_1][T N S T _Output_1][N N N _Exit][N S S T T N _Create_Label_FALSE][S S S N _Push_0][T N S T _Output_0] 

Try it online.Try it online (with raw spaces, tabs, and new-lines only).

  1. Read input
  2. Is it less than 2: Return 0
  3. Is it 2: Return 1
  4. Integer i=1
  5. Start loop
      1. Increase i by 1
      1. Increase i by 1
      1. If the input and i are equal: Return 1 (last iteration of the loop)
      1. If the input and i are equal: Return 1 (last iteration of the loop)
    • 8a7a) Is the input modulo i 0: Return 0
    • 8b7b) Else: Go back to step 5
Command Explanation Stack Heap STDIN STDOUT SSSN Push 0 [0] {} SNS Duplicate (0) [0,0] {} TNTT Read STDIN as number [0] {0:5} 5 TTT Retrieve heap at 0 [5] {0:5} SSSTSN Push 2 [5,2] {0:5} TSST Subtract (5-2) [3] {0:5} SNS Duplicate (3) [3,3] {0:5} NTTTTN Jump to Label_FALSE if neg. [3] {0:5} NTSTN Jump to Label_TRUE if 0  [] {0:5} SSSTN Push 1 [1] {0:5} SNS Duplicate (1) [1,1] {0:5} SNS Duplicate (1) [1,1,1] {0:5} NSSN Create Label_LOOP [1,1,1] {0:5} SNN Discard top [1,1] {0:5} SSSTN Push 1 [1,1,1] {0:5} TSSS Add (1+1) [1,2] {0:5} SNS Duplicate (2) [1,2,2] {0:5} SNS Duplicate (2) [1,2,2,2] {0:5} SSSN Push 0 [1,2,2,2,0] {0:5} TTT Retrieve heap at 0 [1,2,2,2,5] {0:5} TSST Subtract (2-5) [1,2,2,-3] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,2,2] {0:5} SSSN Push 0 [1,2,2,0] {0:5} TTT Retrieve heap at 0 [1,2,2,5] {0:5} SNT Swap top two (2,5] -> 5,2]) [1,2,5,2] {0:5} TSTT Modulo (5%2) [1,2,1] {0:5} SNS Duplicate (1) [1,2,1,1] {0:5} NTSTTN Jump to Label_FALSE if 0 [1,2,1] {0:5} NSNN Jump to Label_LOOP [1,2,1] {0:5} SNN Discard top [1,2] {0:5} SSSTN Push 1 [1,2,1] {0:5} TSSS Add (2+1) [1,3] {0:5} SNS Duplicate (3) [1,3,3] {0:5} SNS Duplicate (3) [1,3,3,3] {0:5} SSSN Push 0 [1,3,3,3,0] {0:5} TTT Retrieve heap at 0 [1,3,3,3,5] {0:5} TSST Subtract (3-5) [1,3,3,-2] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,3,3] {0:5} SSSN Push 0 [1,3,3,0] {0:5} TTT Retrieve heap at 0 [1,3,3,5] {0:5} SNT Swap top two (3,5] -> 5,3]) [1,3,5,3] {0:5} TSTT Modulo (5%3) [1,3,2] {0:5} SNS Duplicate (2) [1,3,2,2] {0:5} NTSTTN Jump to Label_FALSE if 0 [1,3,2] {0:5} NSNN Jump to Label_LOOP [1,3,2] {0:5} SNN Discard top [1,3] {0:5} SSSTN Push 1 [1,3,1] {0:5} TSSS Add (3+1) [1,4] {0:5} SNS Duplicate (4) [1,4,4] {0:5} SNS Duplicate (4) [1,4,4,4] {0:5} SSSN Push 0 [1,4,4,4,0] {0:5} TTT Retrieve heap at 0 [1,4,4,4,5] {0:5} TSST Subtract (4-5) [1,4,4,-1] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,4,4] {0:5} SSSN Push 0 [1,4,4,0] {0:5} TTT Retrieve heap at 0 [1,4,4,5] {0:5} SNT Swap top two (4,5] -> 5,4]) [1,4,5,4] {0:5} TSTT Modulo (5%4) [1,4,1] {0:5} SNS Duplicate (1) [1,4,1,1] {0:5} NTSTTN Jump to Label_FALSE if 0 [1,4,1] {0:5} NSNN Jump to Label_LOOP [1,4,1] {0:5} SNN Discard top [1,4] {0:5} SSSTN Push 1 [1,4,1] {0:5} TSSS Add (4+1) [1,5] {0:5} SNS Duplicate (5) [1,5,5] {0:5} SNS Duplicate (5) [1,5,5,5] {0:5} SSSN Push 0 [1,5,5,5,0] {0:5} TTT Retrieve heap at 0 [1,5,5,5,5] {0:5} TSST Subtract (5-5) [1,5,5,0] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,5,5] {0:5} NSSTN Create Label_TRUE [1,5,5] {0:5} SSSTN Push 1 [1,5,5,1] {0:5} TNST Output top as number [1,5,5] {0:5} 1 NNN Exit Stop program [1,5,5] {0:5} 1 

#Whitespace, 145 bytes

[S S S N _Push_0][S N S _Duplicate][T N T T _Read_STDIN_as_integer_in_heap_0][T T T _Retrieve][S S S T S N _Push_2][T S S T _Subtract][S N S _Duplicate][N T T T T N _Jump_to_Label_FALSE_if_negative][N T S T N _Jump_to_Label_TRUE_if_0][S S S T N _Push_1][S N S _Duplicate_1][S N S _Duplicate_1][N S S N _Create_Label_LOOP][S N N _Discard_top_stack][S S S T N _Push_1][T S S S _Add][S N S _Duplicate][S N S _Duplicate][S S S N _Push_0][T T T _Retrieve_heap_at_0][T S S T _subtract][N T S T N _Jump_to_Label_TRUE_if_0][S S S N _Push_0][T T T _Retrieve_heap_0][S N T _Swap_top_two][T S T T _Modulo][S N S _Duplicate][N T S T T N _Jump_to_Label_FALSE_if_0][N S N N _Jump_to_Label_LOOP][N S S T N _Create_Label_TRUE][S S S T N _Push_1][T N S T _Output_1][N N N _Exit][N S S T T N _Create_Label_FALSE][S S S N _Push_0][T N S T _Output_0] 

Try it online.

  1. Read input
  2. Is it less than 2: Return 0
  3. Is it 2: Return 1
  4. Integer i=1
  5. Start loop
      1. Increase i by 1
      1. If the input and i are equal: Return 1 (last iteration of the loop)
    • 8a) Is the input modulo i 0: Return 0
    • 8b) Else: Go back to step 5
Command Explanation Stack Heap STDIN STDOUT SSSN Push 0 [0] {} SNS Duplicate (0) [0,0] {} TNTT Read STDIN as number [0] {0:5} 5 TTT Retrieve heap at 0 [5] {0:5} SSSTSN Push 2 [5,2] {0:5} TSST Subtract (5-2) [3] {0:5} SNS Duplicate (3) [3,3] {0:5} NTTTTN Jump to Label_FALSE if neg. [3] {0:5} NTSTN Jump to Label_TRUE if 0  [] {0:5} SSSTN Push 1 [1] {0:5} SNS Duplicate (1) [1,1] {0:5} SNS Duplicate (1) [1,1,1] {0:5} NSSN Create Label_LOOP [1,1,1] {0:5} SNN Discard top [1,1] {0:5} SSSTN Push 1 [1,1,1] {0:5} TSSS Add (1+1) [1,2] {0:5} SNS Duplicate (2) [1,2,2] {0:5} SNS Duplicate (2) [1,2,2,2] {0:5} SSSN Push 0 [1,2,2,2,0] {0:5} TTT Retrieve heap at 0 [1,2,2,2,5] {0:5} TSST Subtract (2-5) [1,2,2,-3] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,2,2] {0:5} SSSN Push 0 [1,2,2,0] {0:5} TTT Retrieve heap at 0 [1,2,2,5] {0:5} SNT Swap top two (2,5] -> 5,2]) [1,2,5,2] {0:5} TSTT Modulo (5%2) [1,2,1] {0:5} SNS Duplicate (1) [1,2,1,1] {0:5} NTSTTN Jump to Label_FALSE if 0 [1,2,1] {0:5} NSNN Jump to Label_LOOP [1,2,1] {0:5} SNN Discard top [1,2] {0:5} SSSTN Push 1 [1,2,1] {0:5} TSSS Add (2+1) [1,3] {0:5} SNS Duplicate (3) [1,3,3] {0:5} SNS Duplicate (3) [1,3,3,3] {0:5} SSSN Push 0 [1,3,3,3,0] {0:5} TTT Retrieve heap at 0 [1,3,3,3,5] {0:5} TSST Subtract (3-5) [1,3,3,-2] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,3,3] {0:5} SSSN Push 0 [1,3,3,0] {0:5} TTT Retrieve heap at 0 [1,3,3,5] {0:5} SNT Swap top two (3,5] -> 5,3]) [1,3,5,3] {0:5} TSTT Modulo (5%3) [1,3,2] {0:5} SNS Duplicate (2) [1,3,2,2] {0:5} NTSTTN Jump to Label_FALSE if 0 [1,3,2] {0:5} NSNN Jump to Label_LOOP [1,3,2] {0:5} SNN Discard top [1,3] {0:5} SSSTN Push 1 [1,3,1] {0:5} TSSS Add (3+1) [1,4] {0:5} SNS Duplicate (4) [1,4,4] {0:5} SNS Duplicate (4) [1,4,4,4] {0:5} SSSN Push 0 [1,4,4,4,0] {0:5} TTT Retrieve heap at 0 [1,4,4,4,5] {0:5} TSST Subtract (4-5) [1,4,4,-1] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,4,4] {0:5} SSSN Push 0 [1,4,4,0] {0:5} TTT Retrieve heap at 0 [1,4,4,5] {0:5} SNT Swap top two (4,5] -> 5,4]) [1,4,5,4] {0:5} TSTT Modulo (5%4) [1,4,1] {0:5} SNS Duplicate (1) [1,4,1,1] {0:5} NTSTTN Jump to Label_FALSE if 0 [1,4,1] {0:5} NSNN Jump to Label_LOOP [1,4,1] {0:5} SNN Discard top [1,4] {0:5} SSSTN Push 1 [1,4,1] {0:5} TSSS Add (4+1) [1,5] {0:5} SNS Duplicate (5) [1,5,5] {0:5} SNS Duplicate (5) [1,5,5,5] {0:5} SSSN Push 0 [1,5,5,5,0] {0:5} TTT Retrieve heap at 0 [1,5,5,5,5] {0:5} TSST Subtract (5-5) [1,5,5,0] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,5,5] {0:5} NSSTN Create Label_TRUE [1,5,5] {0:5} SSSTN Push 1 [1,5,5,1] {0:5} TNST Output top as number [1,5,5] {0:5} 1 NNN Exit  [1,5,5] {0:5} 1 

#Whitespace, 145 137 bytes

[S S S N _Push_0][S N S _Duplicate][T N T T _Read_STDIN_as_integer_in_heap_0][T T T _Retrieve][S S S T S N _Push_2][T S S T _Subtract][N T T T T N _Jump_to_Label_FALSE_if_negative][S S S T N _Push_1][S N S _Duplicate_1][S N S _Duplicate_1][N S S N _Create_Label_LOOP][S N N _Discard_top_stack][S S S T N _Push_1][T S S S _Add][S N S _Duplicate][S N S _Duplicate][S S S N _Push_0][T T T _Retrieve_heap_at_0][T S S T _subtract][N T S T N _Jump_to_Label_TRUE_if_0][S S S N _Push_0][T T T _Retrieve_heap_0][S N T _Swap_top_two][T S T T _Modulo][S N S _Duplicate][N T S T T N _Jump_to_Label_FALSE_if_0][N S N N _Jump_to_Label_LOOP][N S S T N _Create_Label_TRUE][S S S T N _Push_1][T N S T _Output_1][N N N _Exit][N S S T T N _Create_Label_FALSE][S S S N _Push_0][T N S T _Output_0] 

Try it online (with raw spaces, tabs, and new-lines only).

  1. Read input
  2. Is it less than 2: Return 0
  3. Integer i=1
  4. Start loop
      1. Increase i by 1
      1. If the input and i are equal: Return 1 (last iteration of the loop)
    • 7a) Is the input modulo i 0: Return 0
    • 7b) Else: Go back to step 5
Command Explanation Stack Heap STDIN STDOUT SSSN Push 0 [0] {} SNS Duplicate (0) [0,0] {} TNTT Read STDIN as number [0] {0:5} 5 TTT Retrieve heap at 0 [5] {0:5} SSSTSN Push 2 [5,2] {0:5} TSST Subtract (5-2) [3] {0:5} NTTTTN Jump to Label_FALSE if neg. [] {0:5} SSSTN Push 1 [1] {0:5} SNS Duplicate (1) [1,1] {0:5} SNS Duplicate (1) [1,1,1] {0:5} NSSN Create Label_LOOP [1,1,1] {0:5} SNN Discard top [1,1] {0:5} SSSTN Push 1 [1,1,1] {0:5} TSSS Add (1+1) [1,2] {0:5} SNS Duplicate (2) [1,2,2] {0:5} SNS Duplicate (2) [1,2,2,2] {0:5} SSSN Push 0 [1,2,2,2,0] {0:5} TTT Retrieve heap at 0 [1,2,2,2,5] {0:5} TSST Subtract (2-5) [1,2,2,-3] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,2,2] {0:5} SSSN Push 0 [1,2,2,0] {0:5} TTT Retrieve heap at 0 [1,2,2,5] {0:5} SNT Swap top two (2,5] -> 5,2]) [1,2,5,2] {0:5} TSTT Modulo (5%2) [1,2,1] {0:5} SNS Duplicate (1) [1,2,1,1] {0:5} NTSTTN Jump to Label_FALSE if 0 [1,2,1] {0:5} NSNN Jump to Label_LOOP [1,2,1] {0:5} SNN Discard top [1,2] {0:5} SSSTN Push 1 [1,2,1] {0:5} TSSS Add (2+1) [1,3] {0:5} SNS Duplicate (3) [1,3,3] {0:5} SNS Duplicate (3) [1,3,3,3] {0:5} SSSN Push 0 [1,3,3,3,0] {0:5} TTT Retrieve heap at 0 [1,3,3,3,5] {0:5} TSST Subtract (3-5) [1,3,3,-2] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,3,3] {0:5} SSSN Push 0 [1,3,3,0] {0:5} TTT Retrieve heap at 0 [1,3,3,5] {0:5} SNT Swap top two (3,5] -> 5,3]) [1,3,5,3] {0:5} TSTT Modulo (5%3) [1,3,2] {0:5} SNS Duplicate (2) [1,3,2,2] {0:5} NTSTTN Jump to Label_FALSE if 0 [1,3,2] {0:5} NSNN Jump to Label_LOOP [1,3,2] {0:5} SNN Discard top [1,3] {0:5} SSSTN Push 1 [1,3,1] {0:5} TSSS Add (3+1) [1,4] {0:5} SNS Duplicate (4) [1,4,4] {0:5} SNS Duplicate (4) [1,4,4,4] {0:5} SSSN Push 0 [1,4,4,4,0] {0:5} TTT Retrieve heap at 0 [1,4,4,4,5] {0:5} TSST Subtract (4-5) [1,4,4,-1] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,4,4] {0:5} SSSN Push 0 [1,4,4,0] {0:5} TTT Retrieve heap at 0 [1,4,4,5] {0:5} SNT Swap top two (4,5] -> 5,4]) [1,4,5,4] {0:5} TSTT Modulo (5%4) [1,4,1] {0:5} SNS Duplicate (1) [1,4,1,1] {0:5} NTSTTN Jump to Label_FALSE if 0 [1,4,1] {0:5} NSNN Jump to Label_LOOP [1,4,1] {0:5} SNN Discard top [1,4] {0:5} SSSTN Push 1 [1,4,1] {0:5} TSSS Add (4+1) [1,5] {0:5} SNS Duplicate (5) [1,5,5] {0:5} SNS Duplicate (5) [1,5,5,5] {0:5} SSSN Push 0 [1,5,5,5,0] {0:5} TTT Retrieve heap at 0 [1,5,5,5,5] {0:5} TSST Subtract (5-5) [1,5,5,0] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,5,5] {0:5} NSSTN Create Label_TRUE [1,5,5] {0:5} SSSTN Push 1 [1,5,5,1] {0:5} TNST Output top as number [1,5,5] {0:5} 1 NNN Stop program [1,5,5] {0:5} 
Fixed code and shortened exit
Source Link
Kevin Cruijssen
  • 136.3k
  • 14
  • 155
  • 394

#Whitespace, 143145 bytes

[S S S N _Push_0][S N S _Duplicate][T N T T _Read_STDIN_as_integer_in_heap_0][T T T _Retrieve][S S S T S N _Push_2][T S S T _Subtract][S N S _Duplicate][N T T T T N _Jump_to_Label_FALSE_if_negative][N T S T N _Jump_to_Label_TRUE_if_0][S S S T N _Push_1][S N S _Duplicate_1][S N S _Duplicate_1][N S S N _Create_Label_LOOP][S N N _Discard_top_stack][S S S T N _Push_1][T S S S _Add][S N S _Duplicate][S N S _Duplicate][S S S N _Push_0][T T T _Retrieve_heap_at_0][T S S T _subtract][N T S T N _Jump_to_Label_TRUE_if_0][S S S N _Push_0][T T T _Retrieve_heap_0][S N T _Swap_top_two][T S T T _Modulo][S N S _Duplicate][N T S T T N _Jump_to_Label_FALSE_if_0][N S N N _Jump_to_Label_LOOP][N S S T N _Create_Label_TRUE][S S S T N _Push_1][T N S T _Output_1][N S N S N _Jump_to_Label_EXIT_WITH_ERROR][N_Exit][N S S T T N _Create_Label_FALSE][S S S N _Push_0][T N S T _Output_0] 

Try it online (open the file prime_checker.ws at the left). For some reason it doesn't work on TIO (I think this has to do with differently behaving Labels and Jumps between the two online compilers).Try it online.

  1. Read input
  2. Is it less than 2: Return 0
  3. Is it 2: Return 1
  4. Integer i=1
  5. Start loop
      1. Increase i by 1
      1. IsIf the input minusand i 0are equal: Return 1 (last iteration of the loop)
    • 8a) Is the input modulo i 0: Return 0
    • 8b) Else: Go back to step 5
Command Explanation Stack Heap STDIN STDOUT STDERR SSN SSSN Push 0 [0] {} SNS Duplicate (0) [0,0] {} TNTT Read STDIN as number [0] {0:5} 5 TTT Retrieve heap at 0 [5] {0:5} SSSTSN Push 2 [5,2] {0:5} TSST Subtract (5-2) [3] {0:5} SNS Duplicate (3) [3,3] {0:5} NTTTTN Jump to Label_FALSE if neg. [3] {0:5} NTSTN Jump to Label_TRUE if 0 [] {0:5} SSSTN Push 1 [1] {0:5} SNS Duplicate (1) [1,1] {0:5} SNS Duplicate (1) [1,1,1] {0:5} NSSN Create Label_LOOP [1,1,1] {0:5} SNN Discard top [1,1] {0:5} SSSTN Push 1 [1,1,1] {0:5} TSSS Add (1+1) [1,2] {0:5} SNS Duplicate (2) [1,2,2] {0:5} SNS Duplicate (2) [1,2,2,2] {0:5} SSN SSSN Push 0 [1,2,2,2,0] {0:5} TTT Retrieve heap at 0 [1,2,2,2,5] {0:5} TSST Subtract (2-5) [1,2,2,-3] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,2,2] {0:5} SSN SSSN Push 0 [1,2,2,0] {0:5} TTT Retrieve heap at 0 [1,2,2,5] {0:5} SNT Swap top two (2,5] -> 5,2]) [1,2,5,2] {0:5} TSTT Modulo (5%2) [1,2,1] {0:5} SNS Duplicate (1) [1,2,1,1] {0:5} NTSTTN Jump to Label_FALSE if 0 [1,2,1] {0:5} NSNN Jump to Label_LOOP [1,2,1] {0:5} SNN Discard top [1,2] {0:5} SSSTN Push 1 [1,2,1] {0:5} TSSS Add (2+1) [1,3] {0:5} SNS Duplicate (3) [1,3,3] {0:5} SNS Duplicate (3) [1,3,3,3] {0:5} SSN SSSN Push 0 [1,3,3,3,0] {0:5} TTT Retrieve heap at 0 [1,3,3,3,5] {0:5} TSST Subtract (3-5) [1,3,3,-2] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,3,3] {0:5} SSN SSSN Push 0 [1,3,3,0] {0:5} TTT Retrieve heap at 0 [1,3,3,5] {0:5} SNT Swap top two (3,5] -> 5,3]) [1,3,5,3] {0:5} TSTT Modulo (5%3) [1,3,2] {0:5} SNS Duplicate (2) [1,3,2,2] {0:5} NTSTTN Jump to Label_FALSE if 0 [1,3,2] {0:5} NSNN Jump to Label_LOOP [1,3,2] {0:5} SNN Discard top [1,3] {0:5} SSSTN Push 1 [1,3,1] {0:5} TSSS Add (3+1) [1,4] {0:5} SNS Duplicate (4) [1,4,4] {0:5} SNS Duplicate (4) [1,4,4,4] {0:5} SSN SSSN Push 0 [1,4,4,4,0] {0:5} TTT Retrieve heap at 0 [1,4,4,4,5] {0:5} TSST Subtract (4-5) [1,4,4,-1] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,4,4] {0:5} SSN SSSN Push 0 [1,4,4,0] {0:5} TTT Retrieve heap at 0 [1,4,4,5] {0:5} SNT Swap top two (4,5] -> 5,4]) [1,4,5,4] {0:5} TSTT Modulo (5%4) [1,4,1] {0:5} SNS Duplicate (1) [1,4,1,1] {0:5} NTSTTN Jump to Label_FALSE if 0 [1,4,1] {0:5} NSNN Jump to Label_LOOP [1,4,1] {0:5} SNN Discard top [1,4] {0:5} SSSTN Push 1 [1,4,1] {0:5} TSSS Add (4+1) [1,5] {0:5} SNS Duplicate (5) [1,5,5] {0:5} SNS Duplicate (5) [1,5,5,5] {0:5} SSN SSSN Push 0 [1,5,5,5,0] {0:5} TTT Retrieve heap at 0 [1,5,5,5,5] {0:5} TSST Subtract (5-5) [1,5,5,0] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,5,5] {0:5} NSSTN Create Label_TRUE [1,5,5] {0:5} SSSTN Push 1 [1,5,5,1] {0:5} TNST Output top as number [1,5,5] {0:5} 1 NSNSNNNN Jump to LABEL_EXITExit  [1,5,5] {0:5} 1 error 

#Whitespace, 143 bytes

[S S N _Push_0][S N S _Duplicate][T N T T _Read_STDIN_as_integer_in_heap_0][T T T _Retrieve][S S S T S N _Push_2][T S S T _Subtract][S N S _Duplicate][N T T T T N _Jump_to_Label_FALSE_if_negative][N T S T N _Jump_to_Label_TRUE_if_0][S S S T N _Push_1][S N S _Duplicate_1][S N S _Duplicate_1][N S S N _Create_Label_LOOP][S N N _Discard_top_stack][S S S T N _Push_1][T S S S _Add][S N S _Duplicate][S N S _Duplicate][S S N _Push_0][T T T _Retrieve_heap_at_0][T S S T _subtract][N T S T N _Jump_to_Label_TRUE_if_0][S S N _Push_0][T T T _Retrieve_heap_0][S N T _Swap_top_two][T S T T _Modulo][S N S _Duplicate][N T S T T N _Jump_to_Label_FALSE_if_0][N S N N _Jump_to_Label_LOOP][N S S T N _Create_Label_TRUE][S S S T N _Push_1][T N S T _Output_1][N S N S N _Jump_to_Label_EXIT_WITH_ERROR][N S S T T N _Create_Label_FALSE][S S N _Push_0][T N S T _Output_0] 

Try it online (open the file prime_checker.ws at the left). For some reason it doesn't work on TIO (I think this has to do with differently behaving Labels and Jumps between the two online compilers).

  1. Read input
  2. Is it less than 2: Return 0
  3. Is it 2: Return 1
  4. Integer i=1
  5. Start loop
      1. Increase i by 1
      1. Is the input minus i 0: Return 1 (last iteration of the loop)
    • 8a) Is the input modulo i 0: Return 0
    • 8b) Else: Go back to step 5
Command Explanation Stack Heap STDIN STDOUT STDERR SSN  Push 0 [0] {} SNS Duplicate (0) [0,0] {} TNTT Read STDIN as number [0] {0:5} 5 TTT Retrieve heap at 0 [5] {0:5} SSSTSN Push 2 [5,2] {0:5} TSST Subtract (5-2) [3] {0:5} SNS Duplicate (3) [3,3] {0:5} NTTTTN Jump to Label_FALSE if neg. [3] {0:5} NTSTN Jump to Label_TRUE if 0 [] {0:5} SSSTN Push 1 [1] {0:5} SNS Duplicate (1) [1,1] {0:5} SNS Duplicate (1) [1,1,1] {0:5} NSSN Create Label_LOOP [1,1,1] {0:5} SNN Discard top [1,1] {0:5} SSSTN Push 1 [1,1,1] {0:5} TSSS Add (1+1) [1,2] {0:5} SNS Duplicate (2) [1,2,2] {0:5} SNS Duplicate (2) [1,2,2,2] {0:5} SSN  Push 0 [1,2,2,2,0] {0:5} TTT Retrieve heap at 0 [1,2,2,2,5] {0:5} TSST Subtract (2-5) [1,2,2,-3] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,2,2] {0:5} SSN  Push 0 [1,2,2,0] {0:5} TTT Retrieve heap at 0 [1,2,2,5] {0:5} SNT Swap top two (2,5] -> 5,2]) [1,2,5,2] {0:5} TSTT Modulo (5%2) [1,2,1] {0:5} SNS Duplicate (1) [1,2,1,1] {0:5} NTSTTN Jump to Label_FALSE if 0 [1,2,1] {0:5} NSNN Jump to Label_LOOP [1,2,1] {0:5} SNN Discard top [1,2] {0:5} SSSTN Push 1 [1,2,1] {0:5} TSSS Add (2+1) [1,3] {0:5} SNS Duplicate (3) [1,3,3] {0:5} SNS Duplicate (3) [1,3,3,3] {0:5} SSN  Push 0 [1,3,3,3,0] {0:5} TTT Retrieve heap at 0 [1,3,3,3,5] {0:5} TSST Subtract (3-5) [1,3,3,-2] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,3,3] {0:5} SSN  Push 0 [1,3,3,0] {0:5} TTT Retrieve heap at 0 [1,3,3,5] {0:5} SNT Swap top two (3,5] -> 5,3]) [1,3,5,3] {0:5} TSTT Modulo (5%3) [1,3,2] {0:5} SNS Duplicate (2) [1,3,2,2] {0:5} NTSTTN Jump to Label_FALSE if 0 [1,3,2] {0:5} NSNN Jump to Label_LOOP [1,3,2] {0:5} SNN Discard top [1,3] {0:5} SSSTN Push 1 [1,3,1] {0:5} TSSS Add (3+1) [1,4] {0:5} SNS Duplicate (4) [1,4,4] {0:5} SNS Duplicate (4) [1,4,4,4] {0:5} SSN  Push 0 [1,4,4,4,0] {0:5} TTT Retrieve heap at 0 [1,4,4,4,5] {0:5} TSST Subtract (4-5) [1,4,4,-1] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,4,4] {0:5} SSN  Push 0 [1,4,4,0] {0:5} TTT Retrieve heap at 0 [1,4,4,5] {0:5} SNT Swap top two (4,5] -> 5,4]) [1,4,5,4] {0:5} TSTT Modulo (5%4) [1,4,1] {0:5} SNS Duplicate (1) [1,4,1,1] {0:5} NTSTTN Jump to Label_FALSE if 0 [1,4,1] {0:5} NSNN Jump to Label_LOOP [1,4,1] {0:5} SNN Discard top [1,4] {0:5} SSSTN Push 1 [1,4,1] {0:5} TSSS Add (4+1) [1,5] {0:5} SNS Duplicate (5) [1,5,5] {0:5} SNS Duplicate (5) [1,5,5,5] {0:5} SSN  Push 0 [1,5,5,5,0] {0:5} TTT Retrieve heap at 0 [1,5,5,5,5] {0:5} TSST Subtract (5-5) [1,5,5,0] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,5,5] {0:5} NSSTN Create Label_TRUE [1,5,5] {0:5} SSSTN Push 1 [1,5,5,1] {0:5} TNST Output top as number [1,5,5] {0:5} 1 NSNSN Jump to LABEL_EXIT [1,5,5] {0:5} 1 error 

#Whitespace, 145 bytes

[S S S N _Push_0][S N S _Duplicate][T N T T _Read_STDIN_as_integer_in_heap_0][T T T _Retrieve][S S S T S N _Push_2][T S S T _Subtract][S N S _Duplicate][N T T T T N _Jump_to_Label_FALSE_if_negative][N T S T N _Jump_to_Label_TRUE_if_0][S S S T N _Push_1][S N S _Duplicate_1][S N S _Duplicate_1][N S S N _Create_Label_LOOP][S N N _Discard_top_stack][S S S T N _Push_1][T S S S _Add][S N S _Duplicate][S N S _Duplicate][S S S N _Push_0][T T T _Retrieve_heap_at_0][T S S T _subtract][N T S T N _Jump_to_Label_TRUE_if_0][S S S N _Push_0][T T T _Retrieve_heap_0][S N T _Swap_top_two][T S T T _Modulo][S N S _Duplicate][N T S T T N _Jump_to_Label_FALSE_if_0][N S N N _Jump_to_Label_LOOP][N S S T N _Create_Label_TRUE][S S S T N _Push_1][T N S T _Output_1][N N N _Exit][N S S T T N _Create_Label_FALSE][S S S N _Push_0][T N S T _Output_0] 

Try it online.

  1. Read input
  2. Is it less than 2: Return 0
  3. Is it 2: Return 1
  4. Integer i=1
  5. Start loop
      1. Increase i by 1
      1. If the input and i are equal: Return 1 (last iteration of the loop)
    • 8a) Is the input modulo i 0: Return 0
    • 8b) Else: Go back to step 5
Command Explanation Stack Heap STDIN STDOUT SSSN Push 0 [0] {} SNS Duplicate (0) [0,0] {} TNTT Read STDIN as number [0] {0:5} 5 TTT Retrieve heap at 0 [5] {0:5} SSSTSN Push 2 [5,2] {0:5} TSST Subtract (5-2) [3] {0:5} SNS Duplicate (3) [3,3] {0:5} NTTTTN Jump to Label_FALSE if neg. [3] {0:5} NTSTN Jump to Label_TRUE if 0 [] {0:5} SSSTN Push 1 [1] {0:5} SNS Duplicate (1) [1,1] {0:5} SNS Duplicate (1) [1,1,1] {0:5} NSSN Create Label_LOOP [1,1,1] {0:5} SNN Discard top [1,1] {0:5} SSSTN Push 1 [1,1,1] {0:5} TSSS Add (1+1) [1,2] {0:5} SNS Duplicate (2) [1,2,2] {0:5} SNS Duplicate (2) [1,2,2,2] {0:5} SSSN Push 0 [1,2,2,2,0] {0:5} TTT Retrieve heap at 0 [1,2,2,2,5] {0:5} TSST Subtract (2-5) [1,2,2,-3] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,2,2] {0:5} SSSN Push 0 [1,2,2,0] {0:5} TTT Retrieve heap at 0 [1,2,2,5] {0:5} SNT Swap top two (2,5] -> 5,2]) [1,2,5,2] {0:5} TSTT Modulo (5%2) [1,2,1] {0:5} SNS Duplicate (1) [1,2,1,1] {0:5} NTSTTN Jump to Label_FALSE if 0 [1,2,1] {0:5} NSNN Jump to Label_LOOP [1,2,1] {0:5} SNN Discard top [1,2] {0:5} SSSTN Push 1 [1,2,1] {0:5} TSSS Add (2+1) [1,3] {0:5} SNS Duplicate (3) [1,3,3] {0:5} SNS Duplicate (3) [1,3,3,3] {0:5} SSSN Push 0 [1,3,3,3,0] {0:5} TTT Retrieve heap at 0 [1,3,3,3,5] {0:5} TSST Subtract (3-5) [1,3,3,-2] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,3,3] {0:5} SSSN Push 0 [1,3,3,0] {0:5} TTT Retrieve heap at 0 [1,3,3,5] {0:5} SNT Swap top two (3,5] -> 5,3]) [1,3,5,3] {0:5} TSTT Modulo (5%3) [1,3,2] {0:5} SNS Duplicate (2) [1,3,2,2] {0:5} NTSTTN Jump to Label_FALSE if 0 [1,3,2] {0:5} NSNN Jump to Label_LOOP [1,3,2] {0:5} SNN Discard top [1,3] {0:5} SSSTN Push 1 [1,3,1] {0:5} TSSS Add (3+1) [1,4] {0:5} SNS Duplicate (4) [1,4,4] {0:5} SNS Duplicate (4) [1,4,4,4] {0:5} SSSN Push 0 [1,4,4,4,0] {0:5} TTT Retrieve heap at 0 [1,4,4,4,5] {0:5} TSST Subtract (4-5) [1,4,4,-1] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,4,4] {0:5} SSSN Push 0 [1,4,4,0] {0:5} TTT Retrieve heap at 0 [1,4,4,5] {0:5} SNT Swap top two (4,5] -> 5,4]) [1,4,5,4] {0:5} TSTT Modulo (5%4) [1,4,1] {0:5} SNS Duplicate (1) [1,4,1,1] {0:5} NTSTTN Jump to Label_FALSE if 0 [1,4,1] {0:5} NSNN Jump to Label_LOOP [1,4,1] {0:5} SNN Discard top [1,4] {0:5} SSSTN Push 1 [1,4,1] {0:5} TSSS Add (4+1) [1,5] {0:5} SNS Duplicate (5) [1,5,5] {0:5} SNS Duplicate (5) [1,5,5,5] {0:5} SSSN Push 0 [1,5,5,5,0] {0:5} TTT Retrieve heap at 0 [1,5,5,5,5] {0:5} TSST Subtract (5-5) [1,5,5,0] {0:5} NTSTN Jump to LABEL_TRUE if 0 [1,5,5] {0:5} NSSTN Create Label_TRUE [1,5,5] {0:5} SSSTN Push 1 [1,5,5,1] {0:5} TNST Output top as number [1,5,5] {0:5} 1 NNN Exit  [1,5,5] {0:5} 1 
Source Link
Kevin Cruijssen
  • 136.3k
  • 14
  • 155
  • 394
Loading