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)" } "." }
|
||||||
|
|
|
@ -32,13 +32,13 @@ IN: math.functions.tests
|
||||||
|
|
||||||
[ 1.0 ] [ 0 cosh ] unit-test
|
[ 1.0 ] [ 0 cosh ] unit-test
|
||||||
[ 0.0 ] [ 1 acosh ] unit-test
|
[ 0.0 ] [ 1 acosh ] unit-test
|
||||||
|
|
||||||
[ 1.0 ] [ 0 cos ] unit-test
|
[ 1.0 ] [ 0 cos ] unit-test
|
||||||
[ 0.0 ] [ 1 acos ] unit-test
|
[ 0.0 ] [ 1 acos ] unit-test
|
||||||
|
|
||||||
[ 0.0 ] [ 0 sinh ] unit-test
|
[ 0.0 ] [ 0 sinh ] unit-test
|
||||||
[ 0.0 ] [ 0 asinh ] unit-test
|
[ 0.0 ] [ 0 asinh ] unit-test
|
||||||
|
|
||||||
[ 0.0 ] [ 0 sin ] unit-test
|
[ 0.0 ] [ 0 sin ] unit-test
|
||||||
[ 0.0 ] [ 0 asin ] unit-test
|
[ 0.0 ] [ 0 asin ] unit-test
|
||||||
|
|
||||||
|
@ -97,11 +97,17 @@ IN: math.functions.tests
|
||||||
|
|
||||||
: verify-gcd ( a b -- ? )
|
: verify-gcd ( a b -- ? )
|
||||||
2dup gcd
|
2dup gcd
|
||||||
[ rot * swap rem ] dip = ;
|
[ rot * swap rem ] dip = ;
|
||||||
|
|
||||||
[ 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
|
||||||
|
|
||||||
|
@ -150,4 +156,4 @@ IN: math.functions.tests
|
||||||
1067811677921310779
|
1067811677921310779
|
||||||
2135623355842621559
|
2135623355842621559
|
||||||
[ >bignum ] tri@ ^mod
|
[ >bignum ] tri@ ^mod
|
||||||
] unit-test
|
] 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 ]
|
||||||
|
@ -198,7 +201,7 @@ M: real sin fsin ;
|
||||||
|
|
||||||
GENERIC: sinh ( x -- y ) foldable
|
GENERIC: sinh ( x -- y ) foldable
|
||||||
|
|
||||||
M: complex sinh
|
M: complex sinh
|
||||||
>float-rect
|
>float-rect
|
||||||
[ [ fsinh ] [ fcos ] bi* * ]
|
[ [ fsinh ] [ fcos ] bi* * ]
|
||||||
[ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
|
[ [ fcosh ] [ fsin ] bi* * ] 2bi rect> ;
|
||||||
|
|
Loading…
Reference in New Issue