kempe-0.1.1.0: lib/numbertheory.kmp
import "prelude/fn.kmp"
; tail recursive!
gcd : Int Int -- Int
=: [ dup 0 =
if( drop
, dup dip(%) swap gcd )
]
square : Int -- Int
=: [ dup * ]
divides : Int Int -- Bool
=: [ % 0 = ]
; also tail recursive!
;
; kinda sus in that squaring will be integer overflow tho
is_prime_step : Int Int -- Bool
=: [ dup2 divides
if( drop drop False
, dup2 square <
if( 1 + is_prime_step
, drop drop True
)
)
]
is_prime : Int -- Bool
=: [ 2 is_prime_step ]
k_gcd : Int Int -- Int
=: [ gcd ]
%foreign cabi k_gcd
%foreign cabi is_prime