Rename count-factors to group-factors and update docs
							parent
							
								
									75774188ea
								
							
						
					
					
						commit
						8bc631f5ed
					
				| 
						 | 
				
			
			@ -1,20 +1,23 @@
 | 
			
		|||
USING: help.markup help.syntax ;
 | 
			
		||||
USING: help.markup help.syntax math sequences ;
 | 
			
		||||
IN: math.primes.factors
 | 
			
		||||
 | 
			
		||||
{ factors count-factors unique-factors } related-words
 | 
			
		||||
{ factors group-factors unique-factors } related-words
 | 
			
		||||
 | 
			
		||||
HELP: factors
 | 
			
		||||
{ $values { "n" "a positive integer" } { "seq" "a sequence" } }
 | 
			
		||||
{ $description { "Factorize an integer and return an ordered list of factors, possibly repeated." } } ;
 | 
			
		||||
{ $values { "n" "a positive integer" } { "seq" sequence } }
 | 
			
		||||
{ $description { "Return an ordered list of a number's prime factors, possibly repeated." } }
 | 
			
		||||
{ $examples { $example "300 factors ." "{ 2 2 3 5 5 }" } } ;
 | 
			
		||||
 | 
			
		||||
HELP: count-factors
 | 
			
		||||
{ $values { "n" "a positive integer" } { "seq" "a sequence" } }
 | 
			
		||||
{ $description { "Return a sequence of pairs representing each factor in the number and its corresponding power." } } ;
 | 
			
		||||
HELP: group-factors
 | 
			
		||||
{ $values { "n" "a positive integer" } { "seq" sequence } }
 | 
			
		||||
{ $description { "Return a sequence of pairs representing each prime factor in the number and its corresponding power (multiplicity)." } }
 | 
			
		||||
{ $examples { $example "300 group-factors ." "{ { 2 2 } { 3 1 } { 5 2 } }" } } ;
 | 
			
		||||
 | 
			
		||||
HELP: unique-factors
 | 
			
		||||
{ $values { "n" "a positive integer" } { "seq" "a sequence" } }
 | 
			
		||||
{ $description { "Return an ordered list of unique prime factors." } } ;
 | 
			
		||||
{ $values { "n" "a positive integer" } { "seq" sequence } }
 | 
			
		||||
{ $description { "Return an ordered list of a number's unique prime factors." } }
 | 
			
		||||
{ $examples { $example "300 unique-factors ." "{ 2 3 5 }" } } ;
 | 
			
		||||
 | 
			
		||||
HELP: totient
 | 
			
		||||
{ $values { "n" "a positive integer" } { "t" "an integer" } }
 | 
			
		||||
{ $description { "Return the number of integers between 1 and " { $snippet "n-1" } " relatively prime to " { $snippet "n" } "." } } ;
 | 
			
		||||
{ $values { "n" "a positive integer" } { "t" integer } }
 | 
			
		||||
{ $description { "Return the number of integers between 1 and " { $snippet "n-1" } " that are relatively prime to " { $snippet "n" } "." } } ;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,6 @@
 | 
			
		|||
USING: math.primes.factors tools.test ;
 | 
			
		||||
 | 
			
		||||
{ { 999983 999983 1000003 } } [ 999969000187000867 factors ] unit-test
 | 
			
		||||
{ { { 999983 2 } { 1000003 1 } } } [ 999969000187000867 count-factors ] unit-test
 | 
			
		||||
{ { { 999983 2 } { 1000003 1 } } } [ 999969000187000867 group-factors ] unit-test
 | 
			
		||||
{ { 999983 1000003 } } [ 999969000187000867 unique-factors ] unit-test
 | 
			
		||||
{ 999967000236000612 } [ 999969000187000867 totient ] unit-test
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -27,7 +27,7 @@ PRIVATE>
 | 
			
		|||
: factors ( n -- seq )
 | 
			
		||||
    [ (factor) ] (decompose) ; foldable
 | 
			
		||||
 | 
			
		||||
: count-factors ( n -- seq )
 | 
			
		||||
: group-factors ( n -- seq )
 | 
			
		||||
    [ (count) ] (decompose) ; foldable
 | 
			
		||||
 | 
			
		||||
: unique-factors ( n -- seq )
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -88,7 +88,7 @@ PRIVATE>
 | 
			
		|||
 | 
			
		||||
! The divisor function, counts the number of divisors
 | 
			
		||||
: tau ( m -- n )
 | 
			
		||||
    count-factors flip second 1 [ 1+ * ] reduce ;
 | 
			
		||||
    group-factors flip second 1 [ 1+ * ] reduce ;
 | 
			
		||||
 | 
			
		||||
! Optimized brute-force, is often faster than prime factorization
 | 
			
		||||
: tau* ( m -- n )
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue