]> gitweb.factorcode.org Git - factor.git/commitdiff
math.functions: when gcd is inlined, "gcd nip" is almost as good as "gcd*".
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 18 Oct 2011 17:30:39 +0000 (10:30 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 18 Oct 2011 17:30:39 +0000 (10:30 -0700)
basis/math/functions/functions-tests.factor
basis/math/functions/functions.factor
basis/math/primes/primes.factor
basis/math/ratios/ratios.factor
extra/crypto/rsa/rsa.factor

index 29bdb1e694f03f334bde5cf673995792dfe8a6f2..edd66f9b83534a638f23e16d93a0f0dc0417deb3 100644 (file)
@@ -117,49 +117,46 @@ CONSTANT: log10-factorial-1000 HEX: 1.40f3593ed6f8ep11
 [ t ] [ 10 atanh tanh 10 1.e-10 ~ ] unit-test
 [ t ] [ 0.5 atanh tanh 0.5 1.e-10 ~ ] unit-test
 
-: test-gcd ( x y -- z )
-    [ gcd nip ] [ gcd* ] 2bi [ assert= ] keep ;
-
-[ 100 ] [ 100 100 test-gcd ] unit-test
-[ 100 ] [ 1000 100 test-gcd ] unit-test
-[ 100 ] [ 100 1000 test-gcd ] unit-test
-[ 4 ] [ 132 64 test-gcd ] unit-test
-[ 4 ] [ -132 64 test-gcd ] unit-test
-[ 4 ] [ -132 -64 test-gcd ] unit-test
-[ 4 ] [ 132 -64 test-gcd ] unit-test
-[ 4 ] [ -132 -64 test-gcd ] unit-test
-
-[ 100 ] [ 100 >bignum 100 >bignum test-gcd ] unit-test
-[ 100 ] [ 1000 >bignum 100 >bignum test-gcd ] unit-test
-[ 100 ] [ 100 >bignum 1000 >bignum test-gcd ] unit-test
-[ 4 ] [ 132 >bignum 64 >bignum test-gcd ] unit-test
-[ 4 ] [ -132 >bignum 64 >bignum test-gcd ] unit-test
-[ 4 ] [ -132 >bignum -64 >bignum test-gcd ] unit-test
-[ 4 ] [ 132 >bignum -64 >bignum test-gcd ] unit-test
-[ 4 ] [ -132 >bignum -64 >bignum test-gcd ] unit-test
+[ 100 ] [ 100 100 gcd nip ] unit-test
+[ 100 ] [ 1000 100 gcd nip ] unit-test
+[ 100 ] [ 100 1000 gcd nip ] unit-test
+[ 4 ] [ 132 64 gcd nip ] unit-test
+[ 4 ] [ -132 64 gcd nip ] unit-test
+[ 4 ] [ -132 -64 gcd nip ] unit-test
+[ 4 ] [ 132 -64 gcd nip ] unit-test
+[ 4 ] [ -132 -64 gcd nip ] unit-test
+
+[ 100 ] [ 100 >bignum 100 >bignum gcd nip ] unit-test
+[ 100 ] [ 1000 >bignum 100 >bignum gcd nip ] unit-test
+[ 100 ] [ 100 >bignum 1000 >bignum gcd nip ] unit-test
+[ 4 ] [ 132 >bignum 64 >bignum gcd nip ] unit-test
+[ 4 ] [ -132 >bignum 64 >bignum gcd nip ] unit-test
+[ 4 ] [ -132 >bignum -64 >bignum gcd nip ] unit-test
+[ 4 ] [ 132 >bignum -64 >bignum gcd nip ] unit-test
+[ 4 ] [ -132 >bignum -64 >bignum gcd nip ] unit-test
 
 [ 6 ] [
     1326264299060955293181542400000006
     1591517158873146351817850880000000
-    test-gcd
+    gcd nip
 ] unit-test
 
 [ 11 ] [
     13262642990609552931815424
     159151715887314635181785
-    test-gcd
+    gcd nip
 ] unit-test
 
 [ 3 ] [
     13262642990609552931
     1591517158873146351
-    test-gcd
+    gcd nip
 ] unit-test
 
 [ 26525285981219 ] [
     132626429906095
     159151715887314
-    test-gcd
+    gcd nip
 ] unit-test
 
 
index 241261e3bf35692b45f7de5cd115420f05bf37f3..bd312406b60de7eed332522603a334a4b1a85f77 100644 (file)
@@ -96,9 +96,6 @@ M: complex exp >rect [ exp ] dip polar> ; inline
         swap [ /mod [ over * swapd - ] dip ] keep (gcd)
     ] if ; inline recursive
 
-: (gcd*) ( a b -- c )
-    [ [ mod ] keep swap (gcd*) ] unless-zero ; inline recursive
-
 PRIVATE>
 
 : ^ ( x y -- z )
@@ -112,16 +109,13 @@ PRIVATE>
 : nth-root ( n x -- y ) swap recip ^ ; inline
 
 : gcd ( x y -- a d )
-    [ 0 1 ] 2dip (gcd) dup 0 < [ neg ] when ; foldable
-
-: gcd* ( x y -- d )
-    (gcd*) dup 0 < [ neg ] when ; foldable
+    [ 0 1 ] 2dip (gcd) dup 0 < [ neg ] when ; foldable inline
 
 : lcm ( a b -- c )
-    [ * ] 2keep gcd* /i ; foldable
+    [ * ] 2keep gcd nip /i ; foldable
 
 : divisor? ( m n -- ? )
-    mod 0 = ;
+    mod 0 = ; inline
 
 ERROR: non-trivial-divisor n ;
 
index 2d9e5af3f4e56dfc47bca84e2f22b01731b6c8d8..6be21371352784383d1ad11f5a4b73872ef4a40e 100644 (file)
@@ -68,7 +68,7 @@ PRIVATE>
 
 : nprimes ( n -- seq ) [ 2 swap [ dup , next-prime ] times ] { } make nip ;
 
-: coprime? ( a b -- ? ) gcd* 1 = ; foldable
+: coprime? ( a b -- ? ) gcd nip 1 = ; foldable
 
 : random-prime ( numbits -- p )
     [ ] [ 2^ ] [ random-bits* next-prime ] tri
index ec5e197dec19b46b5bd441c210943c5d8fd2889e..dcb8e87e7c85ee1b874d783829e7e63a0806fd0d 100644 (file)
@@ -30,7 +30,7 @@ M: integer /
         division-by-zero
     ] [
         dup 0 < [ [ neg ] bi@ ] when
-        2dup gcd* [ /i ] curry bi@ fraction>
+        2dup gcd nip [ /i ] curry bi@ fraction>
     ] if-zero ;
 
 M: ratio hashcode*
index f79a879df1de144f219c0996dd613c9c8419e700..1e21ccce9732b5f4d11987e4385a4228f2d21fac 100644 (file)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2008 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: math.primes kernel math math.functions namespaces
-sequences accessors ;
+USING: math.primes kernel math math.functions math.primes
+namespaces sequences accessors ;
 IN: crypto.rsa
 
 ! The private key is the only secret.
@@ -27,7 +27,7 @@ CONSTANT: public-key 65537
     #! Loop until phi is not divisible by the public key.
     dup rsa-primes [ * ] 2keep
     [ 1 - ] bi@ *
-    dup public-key gcd* 1 = [
+    dup public-key coprime? [
         rot drop
     ] [
         2drop modulus-phi