summaryrefslogtreecommitdiff
path: root/lib/prime.rb
diff options
context:
space:
mode:
authorMarcus Stollsteimer <[email protected]>2019-12-27 20:19:37 +0100
committerHiroshi SHIBATA <[email protected]>2020-03-06 20:55:22 +0900
commitbaaf6815704ef36160e45244b844b633ed51c3b4 ()
tree85d3266541a90d5fb7e6ba48cb0000df97bbe27a /lib/prime.rb
parente92fbaf6090fbc60081654cb36da47fc352000ce (diff)
Improve docs for Prime.{prime_division,int_from_prime_division} (#8)
Move explanation for the decomposition array from the Example section to the method description. Mention the term "multiplicity". Use examples that also demonstrate factors with multiplicity other than 1, and avoid factors/multiplicities with the same value. Also add the decomposition written as simple mathematical expression. This also fixes missing syntax highlighting for the code examples due to verbatim blocks that did not only include Ruby code.
-rw-r--r--lib/prime.rb47
1 files changed, 29 insertions, 18 deletions
@@ -174,17 +174,23 @@ class Prime
# Re-composes a prime factorization and returns the product.
#
# == Parameters
- # +pd+:: Array of pairs of integers. The each internal
- # pair consists of a prime number -- a prime factor --
- # and a natural number -- an exponent.
#
# == Example
- # For <tt>[[p_1, e_1], [p_2, e_2], ...., [p_n, e_n]]</tt>, it returns:
#
- # p_1**e_1 * p_2**e_2 * .... * p_n**e_n.
- #
- # Prime.int_from_prime_division([[2,2], [3,1]]) #=> 12
def int_from_prime_division(pd)
pd.inject(1){|value, (prime, index)|
value * prime**index
@@ -193,27 +199,32 @@ class Prime
# Returns the factorization of +value+.
#
# == Parameters
# +value+:: An arbitrary integer.
# +generator+:: Optional. A pseudo-prime generator.
# +generator+.succ must return the next
- # pseudo-prime number in the ascending
- # order. It must generate all prime numbers,
- # but may also generate non prime numbers too.
#
# === Exceptions
# +ZeroDivisionError+:: when +value+ is zero.
#
# == Example
- # For an arbitrary integer:
- #
- # n = p_1**e_1 * p_2**e_2 * .... * p_n**e_n,
- #
- # prime_division(n) returns:
- #
- # [[p_1, e_1], [p_2, e_2], ...., [p_n, e_n]].
#
- # Prime.prime_division(12) #=> [[2,2], [3,1]]
#
def prime_division(value, generator = Prime::Generator23.new)
raise ZeroDivisionError if value == 0