Example #1
0
File: 027.py Project: neilpa/euler
def quad_primes(limit):
    res = []
    for a in xrange(-limit+1,limit):
        for b in [1] + primes.generate(limit):
            res.append((seq(a,b), (a, b), a*b))
            res.append((seq(a,-b), (a, -b), a*b))
    return max(res)
Example #2
0
File: 007.py Project: neilpa/euler
def solve():
    # Prime Number Theorem:
    # Roughly speaking, the prime number theorem states that if you randomly 
    # select a number nearby some large number N, the chance of it being prime 
    # is about 1 / ln(N), where ln(N) denotes the natural logarithm of N. For 
    # example, near N = 10,000, about one in nine numbers is prime, whereas 
    # near N = 1,000,000,000, only one in every 21 numbers is prime.
    return primes.generate(10000)[1001]
Example #3
0
def factor_count(n):
    """
    returns a dictionary of {factor: times_occuring} for the number n
    """
    ret = collections.defaultdict(int)
    for p in itertools.takewhile(lambda p: p<=n, primes.generate()):
        while not n % p:
            ret[p] += 1
            n = n // p
    return ret
Example #4
0
def run():
    print(sum(itertools.takewhile(lambda n:n<2e6, primes.generate())))
Example #5
0
def iter_factors(n):
    for p in itertools.takewhile(lambda p: p<=n, primes.generate()):
        while not n % p:
            yield p
            n = n // p
Example #6
0
#!/usr/bin/python

# https://projecteuler.net/problem=10
# Find the sum of all the primes below two million.

from primes import generate

sum = 0L
n = 2000000

for prime in generate():
    if prime < n:
        sum += prime
    else:
        print sum
        break
Example #7
0
import primes as prim

print prim.generate(10)

limit=100
count=0
yaks=''
for p in prim.P:
	yaks=yaks+ '''
#######################################
	'''
	yaks=yaks+ str(p)
	yaks=yaks+ '''
#######################################
	'''
	yaks=yaks+ str(prim.YaksicPrimes(p))
	count+=1
	if count>limit:
		f = open('YaksicPrimes.txt', 'w')
		f.write(yaks)
		break

Example #8
0
 def test_primes(self):
     for x in imap(eq, self.primes, generate()):
         self.assertTrue(x)
The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.

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 collections import deque
from primes import generate, check as isprime


def is_circular_prime(p):
    rotations = len(str(p))
    prime = deque(str(p))
    for x in range(rotations):
        prime_int = int("".join(prime))
        if not isprime(prime_int):
            return False
        prime.rotate(1)
    return True


primes = generate(1000000)
count = 0

for i, prime in enumerate(primes):
    if prime and is_circular_prime(i):
        count += 1

print(count)
Example #10
0
#!/usr/bin/python

# https://projecteuler.net/problem=7
# What is the 10,001st prime number?

from primes import generate

for n, prime in enumerate(generate()):
    if n == 10000:
        print prime
        break
Example #11
0
def nth_prime(n):
    for idx, p in enumerate(primes.generate(), start=1):
        if idx==n:
            return p