1 ! Copyright (C) 2007-2009 Samuel Tardieu.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: arrays kernel make math math.functions math.primes ;
4 IN: math.primes.brute-force
8 : count-factor ( n d -- n' c )
10 [ dupd /mod zero? ] curry [ nip [ 1 + ] dip ] while drop
13 : write-factor ( n d -- n' d' )
15 [ [ count-factor ] keep swap 2array , ] keep
16 ! If the remainder is a prime number, increase d so that
17 ! the caller stops looking for factors.
18 over prime? [ drop dup ] when
23 : brute-force-factors ( n -- seq )
26 [ 2dup sq < ] [ write-factor next-prime ] until
27 drop dup 2 < [ drop ] [ 1 2array , ] if