]> gitweb.factorcode.org Git - factor.git/commitdiff
Fix suboptimal prime number factoring
authorSamuel Tardieu <sam@rfc1149.net>
Mon, 2 Feb 2009 23:33:04 +0000 (00:33 +0100)
committerSamuel Tardieu <sam@rfc1149.net>
Mon, 2 Feb 2009 23:33:12 +0000 (00:33 +0100)
basis/math/primes/factors/factors-tests.factor
basis/math/primes/factors/factors.factor

index f247683c1c447c40195057dbd3b3798904e98d30..983de512169c40b244ba9a057c68bacbfdc40d7e 100644 (file)
@@ -6,3 +6,4 @@ USING: math.primes.factors tools.test ;
 { { 999983 1000003 } } [ 999969000187000867 unique-factors ] unit-test
 { 999967000236000612 } [ 999969000187000867 totient ] unit-test
 { 0 } [ 1 totient ] unit-test
+{ { 425612003 } } [ 425612003 factors ] unit-test
index 05d6b260106ac59f9bab6313161b49476a8ecf86..4c36fc0a8506a1a609e111a3ab979cf49457d794 100644 (file)
@@ -16,7 +16,11 @@ IN: math.primes.factors
 PRIVATE>
 
 : group-factors ( n -- seq )
-    [ 2 [ over 1 > ] [ write-factor next-prime ] [ ] while 2drop ] { } make ;
+    [
+        2
+        [ 2dup sq < ] [ write-factor next-prime ] [ ] until
+        drop dup 2 < [ drop ] [ 1 2array , ] if
+    ] { } make ;
 
 : unique-factors ( n -- seq ) group-factors [ first ] map ;