uxn/projects/examples/exercises/primes.tal

43 lines
785 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
DUP2 ,is-prime JSR #00 EQU ,&skip JCN
2022-03-25 13:29:45 -04:00
( print ) DUP2 ,print/short JSR
( space ) #2018 DEO
2021-10-16 23:16:54 -04:00
&skip
INC2 NEQ2k ,&loop JCN
POP2 POP2
2022-03-25 13:29:45 -04:00
( halt ) #010f DEO
2021-10-16 23:16:54 -04:00
BRK
@is-prime ( number* -- flag )
2022-03-25 13:29:45 -04:00
DUP2 #0001 EQU2 ,&fail JCN
2021-10-16 23:16:54 -04:00
STH2k
( range ) #01 SFT2 #0002
&loop
2022-03-10 12:37:11 -05:00
STH2kr OVR2 ( mod2 ) [ DIV2k MUL2 SUB2 ] ORA ,&continue JCN
2022-03-25 13:29:45 -04:00
POP2 POP2
2022-03-10 12:37:11 -05:00
POP2r #00 JMP2r
2021-10-16 23:16:54 -04:00
&continue
INC2 GTH2k ,&loop JCN
2022-03-25 13:29:45 -04:00
POP2 POP2
2021-10-16 23:16:54 -04:00
POP2r #01
2022-03-10 12:37:11 -05:00
JMP2r
2022-03-25 13:29:45 -04:00
&fail POP2 #00 JMP2r
2021-10-16 23:16:54 -04:00
2022-03-10 12:37:11 -05:00
@print ( short* -- )
&short ( short* -- ) SWP ,&byte JSR
&byte ( byte -- ) DUP #04 SFT ,&char JSR
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