Add divisor? math function

db4
Aaron Schaefer 2009-04-01 19:29:19 -04:00
parent d220288356
commit c698a83a12
3 changed files with 22 additions and 7 deletions

View File

@ -13,7 +13,8 @@ ARTICLE: "integer-functions" "Integer functions"
"Tests:"
{ $subsection power-of-2? }
{ $subsection even? }
{ $subsection odd? } ;
{ $subsection odd? }
{ $subsection divisor? } ;
ARTICLE: "arithmetic-functions" "Arithmetic functions"
"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" } }
{ $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
{ $values { "x" integer } { "n" integer } { "y" integer } }
{ $description "Outputs an integer " { $snippet "y" } " such that " { $snippet "xy = 1 (mod n)" } "." }

View File

@ -32,13 +32,13 @@ IN: math.functions.tests
[ 1.0 ] [ 0 cosh ] unit-test
[ 0.0 ] [ 1 acosh ] unit-test
[ 1.0 ] [ 0 cos ] unit-test
[ 0.0 ] [ 1 acos ] unit-test
[ 0.0 ] [ 0 sinh ] unit-test
[ 0.0 ] [ 0 asinh ] unit-test
[ 0.0 ] [ 0 sin ] unit-test
[ 0.0 ] [ 0 asin ] unit-test
@ -97,11 +97,17 @@ IN: math.functions.tests
: verify-gcd ( a b -- ? )
2dup gcd
[ rot * swap rem ] dip = ;
[ rot * swap rem ] dip = ;
[ t ] [ 123 124 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
[ 78572682077 ] [ 234829342 342389423843 mod-inv ] unit-test
@ -150,4 +156,4 @@ IN: math.functions.tests
1067811677921310779
2135623355842621559
[ >bignum ] tri@ ^mod
] unit-test
] unit-test

View File

@ -111,6 +111,9 @@ PRIVATE>
: lcm ( a b -- c )
[ * ] 2keep gcd nip /i ; foldable
: divisor? ( x y -- ? )
mod 0 = ;
: mod-inv ( x n -- y )
[ nip ] [ gcd 1 = ] 2bi
[ dup 0 < [ + ] [ nip ] if ]
@ -198,7 +201,7 @@ M: real sin fsin ;
GENERIC: sinh ( x -- y ) foldable
M: complex sinh
M: complex sinh
>float-rect
[ [ fsinh ] [ fcos ] bi* * ]
[ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;