uxn/projects/examples/exercises/primes.tal

37 lines
691 B
Tal
Raw Normal View History

2022-03-25 17:29:45 +00:00
( Primes:
An integer greater than one is called a prime number
2021-10-17 03:16:54 +00:00
if its only positive divisors are one and itself. )
2022-03-25 17:29:45 +00:00
|0100 ( -> ) @reset
2021-10-17 03:16:54 +00:00
#0000 INC2k
2021-10-17 03:16:54 +00:00
&loop
2023-02-27 18:41:35 +00:00
DUP2 not-prime ?&skip
DUP2 print/short #2018 DEO
2021-10-17 03:16:54 +00:00
&skip
2023-02-27 18:41:35 +00:00
INC2 NEQ2k ?&loop
2021-10-17 03:16:54 +00:00
POP2 POP2
2022-03-25 17:29:45 +00:00
( halt ) #010f DEO
2023-02-27 18:41:35 +00:00
2021-10-17 03:16:54 +00:00
BRK
2023-02-27 18:41:35 +00:00
@not-prime ( number* -- flag )
2021-10-17 03:16:54 +00:00
2022-11-10 17:08:21 +00:00
DUP2 ,&t STR2
2023-02-27 18:41:35 +00:00
( range ) #01 SFT2 #0002 LTH2k ?&fail
2021-10-17 03:16:54 +00:00
&loop
2023-02-27 18:41:35 +00:00
[ LIT2 &t $2 ] OVR2 ( mod2 ) DIV2k MUL2 SUB2 ORA ?&continue
&fail POP2 POP2 #01 JMP2r &continue
INC2 GTH2k ?&loop
POP2 POP2 #00
2021-10-17 03:16:54 +00:00
2022-03-10 17:37:11 +00:00
JMP2r
2021-10-17 03:16:54 +00:00
2022-03-10 17:37:11 +00:00
@print ( short* -- )
2023-02-27 18:41:35 +00:00
&short ( short* -- ) SWP print/byte
&byte ( byte -- ) DUP #04 SFT print/char
2022-03-25 17:29:45 +00:00
&char ( char -- ) #0f AND DUP #09 GTH #27 MUL ADD #30 ADD #18 DEO
2022-03-10 17:37:11 +00:00
JMP2r