packages feed

kempe-0.1.0.0: lib/numbertheory.kmp

; tail recursive!
gcd : Int Int -- Int
    =: [ dup 0 =
         if( drop
           , dup dip(%) swap gcd )
       ]

over : a b -- a b a
     =: [ dip(dup) swap ]

dup2 : a b -- a b a b
     =: [ over over ]

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