Add divisor? math function
parent
d220288356
commit
c698a83a12
|
@ -13,7 +13,8 @@ ARTICLE: "integer-functions" "Integer functions"
|
||||||
"Tests:"
|
"Tests:"
|
||||||
{ $subsection power-of-2? }
|
{ $subsection power-of-2? }
|
||||||
{ $subsection even? }
|
{ $subsection even? }
|
||||||
{ $subsection odd? } ;
|
{ $subsection odd? }
|
||||||
|
{ $subsection divisor? } ;
|
||||||
|
|
||||||
ARTICLE: "arithmetic-functions" "Arithmetic functions"
|
ARTICLE: "arithmetic-functions" "Arithmetic functions"
|
||||||
"Computing additive and multiplicative inverses:"
|
"Computing additive and multiplicative inverses:"
|
||||||
|
@ -269,6 +270,11 @@ HELP: gcd
|
||||||
{ $description "Computes the positive greatest common divisor " { $snippet "d" } " of " { $snippet "x" } " and " { $snippet "y" } ", and another value " { $snippet "a" } " satisfying:" { $code "a*y = d mod x" } }
|
{ $description "Computes the positive greatest common divisor " { $snippet "d" } " of " { $snippet "x" } " and " { $snippet "y" } ", and another value " { $snippet "a" } " satisfying:" { $code "a*y = d mod x" } }
|
||||||
{ $notes "If " { $snippet "d" } " is 1, then " { $snippet "a" } " is the inverse of " { $snippet "y" } " modulo " { $snippet "x" } "." } ;
|
{ $notes "If " { $snippet "d" } " is 1, then " { $snippet "a" } " is the inverse of " { $snippet "y" } " modulo " { $snippet "x" } "." } ;
|
||||||
|
|
||||||
|
HELP: divisor?
|
||||||
|
{ $values { "x" integer } { "y" integer } { "?" "a boolean" } }
|
||||||
|
{ $description "Tests if " { $snippet "y" } " is a divisor of " { $snippet "x" } ". This is the same thing as saying " { $snippet "x" } " is divisible by " { $snippet "y" } "." }
|
||||||
|
{ $notes "Returns t for both negative and positive divisors, as well as trivial and non-trivial divisors." } ;
|
||||||
|
|
||||||
HELP: mod-inv
|
HELP: mod-inv
|
||||||
{ $values { "x" integer } { "n" integer } { "y" integer } }
|
{ $values { "x" integer } { "n" integer } { "y" integer } }
|
||||||
{ $description "Outputs an integer " { $snippet "y" } " such that " { $snippet "xy = 1 (mod n)" } "." }
|
{ $description "Outputs an integer " { $snippet "y" } " such that " { $snippet "xy = 1 (mod n)" } "." }
|
||||||
|
|
|
@ -102,6 +102,12 @@ IN: math.functions.tests
|
||||||
[ t ] [ 123 124 verify-gcd ] unit-test
|
[ t ] [ 123 124 verify-gcd ] unit-test
|
||||||
[ t ] [ 50 120 verify-gcd ] unit-test
|
[ t ] [ 50 120 verify-gcd ] unit-test
|
||||||
|
|
||||||
|
[ t ] [ 0 42 divisor? ] unit-test
|
||||||
|
[ t ] [ 42 7 divisor? ] unit-test
|
||||||
|
[ t ] [ 42 -7 divisor? ] unit-test
|
||||||
|
[ t ] [ 42 42 divisor? ] unit-test
|
||||||
|
[ f ] [ 42 16 divisor? ] unit-test
|
||||||
|
|
||||||
[ 3 ] [ 5 7 mod-inv ] unit-test
|
[ 3 ] [ 5 7 mod-inv ] unit-test
|
||||||
[ 78572682077 ] [ 234829342 342389423843 mod-inv ] unit-test
|
[ 78572682077 ] [ 234829342 342389423843 mod-inv ] unit-test
|
||||||
|
|
||||||
|
|
|
@ -111,6 +111,9 @@ PRIVATE>
|
||||||
: lcm ( a b -- c )
|
: lcm ( a b -- c )
|
||||||
[ * ] 2keep gcd nip /i ; foldable
|
[ * ] 2keep gcd nip /i ; foldable
|
||||||
|
|
||||||
|
: divisor? ( x y -- ? )
|
||||||
|
mod 0 = ;
|
||||||
|
|
||||||
: mod-inv ( x n -- y )
|
: mod-inv ( x n -- y )
|
||||||
[ nip ] [ gcd 1 = ] 2bi
|
[ nip ] [ gcd 1 = ] 2bi
|
||||||
[ dup 0 < [ + ] [ nip ] if ]
|
[ dup 0 < [ + ] [ nip ] if ]
|
||||||
|
|
Loading…
Reference in New Issue