Exemple #1
0
def factorGenerator(n):
    p = prime_factors(n)
    factors = {}
    for p1 in p:
        try:
            factors[p1] += 1
        except KeyError:
            factors[p1] = 1
    return factors
Exemple #2
0
def smallest_multiple(values):
    decompositions = []
    for v in values:
        factors, powers = prime_factors(v)
        decompositions.append(pd.Series(powers, index=factors))
    decompositions = pd.concat(decompositions, axis=1, keys=values)
    smallest_multiple_decomposition = decompositions.max(axis=1)
    factors = pd.Series(smallest_multiple_decomposition.index)
    powers = smallest_multiple_decomposition.reset_index(drop=True)
    return int((factors**powers).prod())
Exemple #3
0
def is_circular_prime(n):
    ret = True
    s = str(n)
    s_cycle = s + s  # double string to generate circular number
    if len(s) > 1:
        for i in xrange(0, len(s)):
            tmp = s_cycle[i:i+len(s)]
            if len(prime_factors(int(tmp))) > 1:
                ret = False
                break
    return ret
Exemple #4
0
def n_divisors(number):
    '''
    Form of the sigma function for counting the number of divisors
    of a given number that relies on the prime factorization of the
    given number

    Source:
    https://en.wikipedia.org/wiki/Divisor_function#Formulas_at_prime_powers
    '''
    pf = prime_factors(number)
    return product((x + 1) for x in pf.values())
Exemple #5
0
def main():
    '''
    2520 is the smallest number that can be divided by each of the numbers from 1 to
    10 without any remainder.

    What is the smallest positive number that is evenly divisible by all of the
    numbers from 1 to 20?
    '''

    # Figure out the max power of each prime factor in 2..20
    factors = defaultdict(int)
    for pf in [prime_factors(i) for i in range(2, 21)]:
        for k, v in pf.items():
            factors[k] = max(factors[k], v)

    return product(k**v for k, v in factors.items())
Exemple #6
0
def is_prime(n):
    f, p = prime_factors(n)
    return len(f) == 1 and p[0] == 1
Exemple #7
0
There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

How many circular primes are there below one million?
'''
from problem3 import prime_factors


def is_circular_prime(n):
    ret = True
    s = str(n)
    s_cycle = s + s  # double string to generate circular number
    if len(s) > 1:
        for i in xrange(0, len(s)):
            tmp = s_cycle[i:i+len(s)]
            if len(prime_factors(int(tmp))) > 1:
                ret = False
                break
    return ret

N = 1000000
count = 0
for i in xrange(2, N+1):
    if len(prime_factors(i)) == 1:
        if is_circular_prime(i):
            count += 1
            print i, count

print '==result=='
print count
Exemple #8
0
 def test_prime_factors(self):
     self.assertEqual(problem3.prime_factors(2), [2])
     self.assertEqual(problem3.prime_factors(4), [2,2])
     self.assertEqual(problem3.prime_factors(6), [2,3])
     self.assertEqual(problem3.prime_factors(8), [2,2,2])
     self.assertEqual(problem3.prime_factors(13195), [5, 7, 13, 29])