Example #1
0
def primeGen(limit):
    """Generates prime numbers until user tells it to stop"""
    
    # Can't iterate a generator, so make a list!
    prime_list = [] 

    for prime in prime_sieve(limit): 
        prime_list.append(prime)


    print("Prime # %d is : %d" %(1 , prime_list[0]))
    for p in range(len(prime_list)):
        if p > 1: 
            print("Do you want to print another prime? Y/N") 
            answer = raw_input('>').lower()

            if answer == 'y':
                print("Prime # %d is : %d" %(p + 1, prime_list[p]))

            elif answer == 'n': 
               print('Thanks for using the generator!')
               break
Example #2
0
File: 3.py Project: Primer42/Euler
'''
Created on Oct 18, 2012

@author: Will
'''
from prime import prime_sieve

if __name__ == '__main__':
    n = 600851475143
    ps = prime_sieve(int(n ** .5) + 1)
    print "sieve done - got %d primes" % len(ps)
    ps.remove(1)
    ps.sort(reverse=True)
    for p in ps:
        if n % p == 0:
            print p
            break
Example #3
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
Problem 41

We shall say that an n-digit number is pandigital if it makes use of all
the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital
and is also prime.

What is the largest n-digit pandigital prime that exists?
'''

from prime import prime_sieve

# Determine if a number is pandigital
def is_pandigital(num):
    num = str(num)
    digits = len(num)

    for n in xrange(1, digits + 1):
        if str(n) not in num:
            return False

    return True


primes = prime_sieve(10 ** 7)
print max([n for n in primes[::-1] if is_pandigital(n)])
Example #4
0
from prime import prime_sieve
max = 0
pair = [0, 0]
primes = prime_sieve(110000)

for a in range(-999, 1000):
    for b in range(-999, 1000):
        n = 0
        while abs(n**2 + a * n + b) in primes:
            n += 1
        if (n > max):
            max = n
            p = [a, b]

print(p[0] * p[1])
Example #5
0
File: 10.py Project: Primer42/Euler
'''
Created on Oct 19, 2012

@author: Will
'''
from prime import prime_sieve

if __name__ == '__main__':
    p = prime_sieve(2000000)
    print "Done with sieve"
    print sum(p)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
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 prime import prime_sieve

primeList = prime_sieve(2000000)
print sum(primeList)
     1: 1
     3: 1,3
     6: 1,2,3,6
    10: 1,2,5,10
    15: 1,3,5,15
    21: 1,3,7,21
    28: 1,2,4,7,14,28

We can see that 28 is the first triangle number to have over five divisors.

What is the value of the first triangle number to have over five hundred
divisors?
'''

from prime import prime_sieve, prime_factorization
from operator import mul

primes = prime_sieve(int(500 ** .5) + 1)

n = 0
while True:
    n += 1
    triangle = n * (n + 1) / 2

    factors = prime_factorization(triangle, primes)
    if factors:
        divisors = reduce(mul, (c + 1 for (factor, c) in factors))
        if divisors > 500:
            print triangle
            break
     1: 1
     3: 1,3
     6: 1,2,3,6
    10: 1,2,5,10
    15: 1,3,5,15
    21: 1,3,7,21
    28: 1,2,4,7,14,28

We can see that 28 is the first triangle number to have over five divisors.

What is the value of the first triangle number to have over five hundred
divisors?
'''

from prime import prime_sieve, prime_factorization
from operator import mul

primes = prime_sieve(int(500**.5) + 1)

n = 0
while True:
    n += 1
    triangle = n * (n + 1) / 2

    factors = prime_factorization(triangle, primes)
    if factors:
        divisors = reduce(mul, (c + 1 for (factor, c) in factors))
        if divisors > 500:
            print triangle
            break
#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
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 prime import prime_sieve

primeList = prime_sieve(2000000)
print sum(primeList)