have a list of primes --> to see how many of those primes act as a divisor (and how often).
Here's some python for the algo(http://mathforum.org/library/drmath/view/55843.html) Just count the number of items in the list instead of returning them however.
Here's a Dr. Math that explains what exactly it is you need to do mathematically.
Essentially it boils down to if your number n = a^x * b^y * c^z (where a, b, and c are n's prime divisors and x, y, and z are the number of times that divisor is repeated) then the total count for all of the divisors is (x + 1) * (y + 1) * (z + 1).
Edit: BTW, to find a,b,c,etc you'll want to do what amounts to a greedy algo if I'm understanding this correctly. Start with your largest prime divisor and multiply it by itself until a further multiplication would exceed the number n. Then move to the next lowest factor and times the previous prime ^ number of times it was multiplied by the current prime and keep multiplying by the prime until the next will exceed n... etc. Keep track of the number of times you multiply the divisors together and apply those numbers into the formula above.
Twenty-four has 8 divisors, namely 1,2,3,4,6,8,12 and 24. I have to find a relationship between the integer, N, and the total number of its divisors, d(N). I have found that every prime number will have 2 divisors, every integer which is the square of a prime will have 3 divisors, every integer which is a cube of a prime number will have 4 divisors, etc. Building on this, I've also found that every number can be expressed uniquely as the product of primes and that there is a
relationship between the exponents of the primes and d(N). For example,
24 = 2^(3)*3^(1)
therefore, d(24) = (3 + 1)(1 + 1) = 8.
import operator
# A slightly efficient superset of primes.
def PrimesPlus():
yield 2
yield 3
i = 5
while True:
yield i
if i % 6 == 1:
i += 2
i += 2
# Returns a dict d with n = product p ^ d[p]
def GetPrimeDecomp(n):
d = {}
primes = PrimesPlus()
for p in primes:
while n % p == 0:
n /= p
d[p] = d.setdefault(p, 0) + 1
if n == 1:
return d
def NumberOfDivisors(n):
d = GetPrimeDecomp(n)
powers_plus = map(lambda x: x+1, d.values())
return reduce(operator.mul, powers_plus, 1)
An answer to your question depends greatly on the size of the integer. Methods for small numbers, e.g. less then 100 bit, and for numbers ~1000 bit (such as used in cryptography) are completely different.
- general overview: http://en.wikipedia.org/wiki/Divisor_function
- values for small
n
and some useful references: AOOOOO5: d(n) (also called tau(n) or sigma_0(n)), the number of divisors of n. - real-world example: factorization of integers
===> A very nice code (by J.F. Sebastian) for finding the prime numbers--> see 2nd reference link
To generate all prime numbers less than a limit Sieve of Eratosthenes (the page contains variants in 20 programming languages) is the oldest and the simplest solution.
In Python:
Note: a typical way, which is common to see, is to divide X by 2, 3, 4, 5, ..., square root of X and if they all can't be divided till we get to the square root, then it is the prime number.def iprimes_upto(limit):
is_prime = [True] * limit
for n in range(2, limit):
if is_prime[n]:
yield n
for i in range(n*n, limit, n): # start at ``n`` squared
is_prime[i] = False
Example:is_prime = [True] * limit
for n in range(2, limit):
if is_prime[n]:
yield n
for i in range(n*n, limit, n): # start at ``n`` squared
is_prime[i] = False
>>> list(iprimes_upto(15))
[2, 3, 5, 7, 11, 13]
♥ ¸¸.•*¨*•♫♪♪♫•*¨*•.¸¸♥
http://stackoverflow.com/questions/110344/algorithm-to-calculate-the-number-of-divisors-of-a-given-numberhttp://stackoverflow.com/questions/171765/what-is-the-best-way-to-get-all-the-divisors-of-a-number
No comments:
Post a Comment