uxn/projects/examples/exercises/primes.tal

37 lines
691 B
Tal
Raw Normal View History

2022-03-25 13:29:45 -04:00
( Primes:
An integer greater than one is called a prime number
2021-10-16 23:16:54 -04:00
if its only positive divisors are one and itself. )
2022-03-25 13:29:45 -04:00
|0100 ( -> ) @reset
2021-10-16 23:16:54 -04:00
#0000 INC2k
2021-10-16 23:16:54 -04:00
&loop
2023-02-27 13:41:35 -05:00
DUP2 not-prime ?&skip
DUP2 print/short #2018 DEO
2021-10-16 23:16:54 -04:00
&skip
2023-02-27 13:41:35 -05:00
INC2 NEQ2k ?&loop
2021-10-16 23:16:54 -04:00
POP2 POP2
2022-03-25 13:29:45 -04:00
( halt ) #010f DEO
2023-02-27 13:41:35 -05:00
2021-10-16 23:16:54 -04:00
BRK
2023-02-27 13:41:35 -05:00
@not-prime ( number* -- flag )
2021-10-16 23:16:54 -04:00
2022-11-10 12:08:21 -05:00
DUP2 ,&t STR2
2023-02-27 13:41:35 -05:00
( range ) #01 SFT2 #0002 LTH2k ?&fail
2021-10-16 23:16:54 -04:00
&loop
2023-02-27 13:41:35 -05: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-16 23:16:54 -04:00
2022-03-10 12:37:11 -05:00
JMP2r
2021-10-16 23:16:54 -04:00
2022-03-10 12:37:11 -05:00
@print ( short* -- )
2023-02-27 13:41:35 -05:00
&short ( short* -- ) SWP print/byte
&byte ( byte -- ) DUP #04 SFT print/char
2022-03-25 13:29:45 -04:00
&char ( char -- ) #0f AND DUP #09 GTH #27 MUL ADD #30 ADD #18 DEO
2022-03-10 12:37:11 -05:00
JMP2r