Exemplo n.º 1
0
def list_prime_factors(number):
    current = number
    primes = []
    factors = []
    prime_generator = p003.primes(number+1)

    def get_next_number(num):
        if num in primes:
            factors.append(num)
            return num

        for p in primes:
            if num % p == 0:
                factors.append(p)
                num /= p
                return get_next_number(num)

        return num

    while current not in primes:
        try:
            primes.append(prime_generator.next())
        except StopIteration:
            break

        if current % primes[-1] == 0:
            current = get_next_number(current)
    
    assert(number == reduce(lambda x, y: x * y, factors))

    return sorted(factors)
Exemplo n.º 2
0
def get_nth_prime(n):
	prime_generator = primes()

	for i in xrange(n):
		prime = prime_generator.next()
	
	return prime
Exemplo n.º 3
0
def first_n_to_n_prime_factors(n):
    start_value = reduce(mul, islice(primes(),0,n))

    factors = FactorList(10**(n+1))

    for seq in izip(*[ count(start_value+i) for i in xrange(n) ]):
        if all( len(set(factors.factor(num))) == n for num in seq ):
            return seq
Exemplo n.º 4
0
# Problem 7: https://projecteuler.net/problem=7
# By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, 
#  we can see that the 6th prime is 13.
# What is the 10 001st prime number?
from p003 import primes
from math import log
n_th = 10001

def pi(x):
    '''
    Estimate the number os primes under x.
    Source: https://primes.utm.edu/howmany.html
    '''
    return int(x/(log(x)-1))


N = 120000
print('Numbers os primes under {} is {}.'.format(N, pi(N)))
myprimes = primes(N)

print('The {} prime is {}.'.format(n_th, myprimes[n_th-1]))
Exemplo n.º 5
0
def solve():
  n = 10001
  return next(islice(primes(), n-1, n))
Exemplo n.º 6
0
# Problem 10: https://projecteuler.net/problem=10
# The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
# Find the sum of all the primes below two million.

from p003 import primes

myprimes = primes(2000000)


print(sum(myprimes))
Exemplo n.º 7
0
# Problem 5: https://projecteuler.net/problem=5
# 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?
from numpy import array, where, in1d
from p003 import primes
from functools import reduce

numbers = array(range(2,20+1))
myprimes = primes(20)
factors = []

for prime in myprimes:
    had_division = True
    while had_division:
        remainders = numbers%prime
        ix = in1d(remainders, 0)
        had_division = ix.sum() > 0
        if had_division:
            factors.append(prime)
            numbers[where(ix)] = numbers[where(ix)]/prime
print(reduce(lambda x,y: x*y, factors))