コード例 #1
0
ファイル: euler_49.py プロジェクト: abingham/project_euler
def run():
    primes = euler_util.GenFinder(euler_util.primes(9999))

    for i in range(1000, 10000 - (3330 * 2)):
        if not primes.contains(i):
            continue

        j = i + 3330
        if not primes.contains(j):
            continue

        k = j + 3330
        if not primes.contains(k):
            continue

        idigs = euler_util.digits(i)
        idigs.sort()

        jdigs = euler_util.digits(j)
        jdigs.sort()

        if not idigs == jdigs:
            continue

        kdigs = euler_util.digits(k)
        kdigs.sort()
        if not kdigs == idigs:
            continue

        match = [i, j, k]
        match.sort()
        print match
コード例 #2
0
def run(max_val):
    primes = list(eu.primes(max_val))
    mults = [0] * len(primes)
    counts = [0] * (max_val + 1)

    value = 0
    update_idx = 0
    while True:
        # calculate the current value
        # value = calc_value(primes, mults)

        if value <= max_val:
            counts[value] += 1
            mults[0] += 1
            value += 2
            continue

        # find first index that is not zero
        nzi = find_nonzero_multiplier(mults)
        if nzi == len(mults) - 1:
            break
        
        # zero it and everything before it out. Increment the next.
        for i in range(nzi + 1):
            mults[i] = 0
        mults[nzi + 1] += 1
        value = calc_value(primes, mults)

    for i,v in enumerate(counts):
        if v > 5000:
            print i
            break
コード例 #3
0
def run(tgt):
    primes = list(eu.primes(int(math.ceil(math.sqrt(tgt)) * 2)))
    print 'primes generated'

    print primes[-1]

    min_ratio = None
    min_n = 0

    for i in range(len(primes) - 1, -1, -1):
        for j in range(i, -1, -1):
            pi = primes[i]
            pj = primes[j]
            n = pi * pj

            if n > tgt:
                continue
            phi = (pi - 1) * (pj - 1)
            
            if not permutations(n, phi):
                continue

            ratio = n / float(phi)
            if min_ratio is None or ratio < min_ratio:
                min_ratio = ratio
                min_n = n

    print min_n
コード例 #4
0
ファイル: ex0043.py プロジェクト: abingham/project_euler
def run():
    primes = [p for p in euler_util.primes(17)]

    sum = 0
    for c in euler_util.combos(range(10)):
        if c[0] == 0:
            continue
        if curious(c, primes):
            # print c
            sum += euler_util.undigits(c)
コード例 #5
0
ファイル: ex0041.py プロジェクト: abingham/project_euler
def run():
    primes = [p for p in euler_util.primes(int(math.ceil(math.sqrt(987654321))))]

    max = 0
    toks = []
    for i in [1,2,3,4,5,6,7,8,9]:
        toks.append(i)
        for c in euler_util.combos(toks):
            x = euler_util.undigits(c)
            if is_prime(x, primes) and x > max:
                max = x
コード例 #6
0
ファイル: ex0069.py プロジェクト: abingham/project_euler
def run(n):
    primes = list(eu.primes(int(math.ceil(math.sqrt(n)))))

    max_quotient = 0
    max_n = None

    for i in range(2,n + 1):
        phi = eu.eulers_totient(i, primes)
        rslt = float(i) / phi

        if rslt > max_quotient:
            max_quotient = rslt
            max_n = i
コード例 #7
0
ファイル: ex0050.py プロジェクト: abingham/project_euler
def run():
    primes = [p for p in euler_util.primes(1000000)]
    # print "primes calculated"

    sums = [0] + euler_util.accumulate(primes,
                                       lambda x,y:x+y,
                                       0)
    # §print "sums calculated"

    max_dist = 0
    max = 0
    for i in range(1, len(sums)):
        if len(sums) - max_dist <= 0:
            break
        for j in range(i + max_dist + 1, len(sums)):
            diff = sums[j] - sums[i-1]
            if diff >= 1000000:
                break
            if euler_util.bsearch(primes, diff) != -1:
                max_dist = (j - i) + 1
                max = diff
コード例 #8
0
ファイル: ex0043.py プロジェクト: abingham/project_euler
def test():
    primes = [p for p in euler_util.primes(17)]
コード例 #9
0
import fractions

import euler_util as eu

primes = list(eu.primes(1000000))

def proper_fractions(den):
    return [num for num in xrange(1,den) if fractions.gcd(num,den) == 1]

def run(max):
    sum = 0
    for i in range(2, max + 1):
        sum += eu.eulers_totient(i)
    print sum

if __name__ == '__main__':
    run(1000000)
        
コード例 #10
0
import bisect, math
import euler_util

primes = [i for i in euler_util.primes(1000001)]

def rotate(n):
    d,m = divmod(n,10)
    return d + m * (10**int(math.log10(n)))

def is_prime(n):
    return euler_util.bsearch(primes, n) != -1

def circular(n):
    if not is_prime(n):
        return False
    for i in range(math.log10(n)):
        n = rotate(n)
        if not is_prime(n):
            return False
    return True

def run():
    results = set()
    for i in range(1000000):
        if circular(i):
            print i
            results.add(i)
        
    print len(results)

def test():
コード例 #11
0
def test():    
    primes = [p for p in euler_util.primes(17)]
    print curious(euler_util.digits(1406357289), primes)
コード例 #12
0
# 10001st prime

import euler_util

target_guess = 1000000
iter = euler_util.primes(target_guess)

for i in range(9999):
    iter.next()
print iter.next()
コード例 #13
0
from cPickle import dump,load
import sys

import euler_util as eu
import prime_reader

#reader = prime_reader.read_primes()
#primes = [reader.next() for i in range(10000000)]
primes = [p for p in eu.primes(100000000)]
print 'primes generated'

def prime_pair(x,y):
    '''determine if x,y are a prime_pair, i.e. xy is prime and yx is
    prime'''

    if eu.bsearch(primes, eu.num_cat(x,y)) == -1:
        return False
    if eu.bsearch(primes, eu.num_cat(y,x)) == -1:
        return False
    return True
    
class Solution(object):
    def __init__(self):
        self.__soln = None
        self.__sum = 0

    def __set_soln(self, s):
        self.__soln = s
        self.__sum = sum(s)
        
    def __nonzero__(self):
コード例 #14
0
ファイル: euler_10.py プロジェクト: abingham/project_euler
def gen():
    sum = 0
    for p in euler_util.primes(max_candidate):
        sum += p
    print sum