uxn/projects/examples/exercises/primes.tal

40 lines
749 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
DUP2 ,is-prime JSR #00 EQU ,&skip JCN
2022-03-25 17:29:45 +00:00
( print ) DUP2 ,print/short JSR
( space ) #2018 DEO
2021-10-17 03:16:54 +00:00
&skip
INC2 NEQ2k ,&loop JCN
POP2 POP2
2022-03-25 17:29:45 +00:00
( halt ) #010f DEO
2021-10-17 03:16:54 +00:00
BRK
@is-prime ( number* -- flag )
2022-11-10 17:08:21 +00:00
DUP2 ,&t STR2
( range ) #01 SFT2 #0002 LTH2k ,&fail JCN
2021-10-17 03:16:54 +00:00
&loop
2022-11-10 17:08:21 +00:00
[ LIT2 &t $2 ] OVR2
( mod2 ) DIV2k MUL2 SUB2
ORA #00 EQU ,&fail JCN
2021-10-17 03:16:54 +00:00
INC2 GTH2k ,&loop JCN
2022-11-10 17:08:21 +00:00
POP2 POP2 #01
2021-10-17 03:16:54 +00:00
2022-03-10 17:37:11 +00:00
JMP2r
2022-11-10 17:08:21 +00:00
&fail POP2 POP2 #00 JMP2r
2021-10-17 03:16:54 +00:00
2022-03-10 17:37:11 +00:00
@print ( short* -- )
&short ( short* -- ) SWP ,&byte JSR
&byte ( byte -- ) DUP #04 SFT ,&char JSR
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