packages feed

kempe-0.2.0.9: lib/numbertheory.kmp

import "prelude/fn.kmp"

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

lcm : Int Int -- Int
    =: [ dup2 dip(dip(*)) 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 armabi k_gcd
%foreign armabi is_prime