Example #1
0
def prob41():
    p = primes.getPrimes()
    largest = 0
    for x in p:
        if isPandigital(x) and x > largest:
            largest = x
    return largest
Example #2
0
File: 615.py Project: dbork/euler
def solve_given_n(n):
    # it's 2^i * something by construction, where the something is composed of
    # 2s, 3/2s, 5/2s, etc. we also know these things can't go higher than
    # the k-th prime, which the prime number theorem says will be less than
    # about ln(n), so just to be safe we can do
    primes = filter(lambda x: x > 0, getPrimes(int(math.log(n, 2) * n)))
    #print primes
    tiles = [math.log(1.5, 2), 1] + map(lambda x: math.log(float(x) / 2, 2),
                                        primes[2:])
    #print tiles
    print len(tiles)

    # this is v generous but should be fine
    upper_bound = tiles[0] * n
    # now we binary search for n tilings lol
    guess = 0
    mod_guess = 0
    powers = range(-50, int(math.log(upper_bound, 2)) + 1)
    powers.reverse()
    for power in powers:
        print 'trying power ' + str(power)
        print 'with guess ' + str(guess)
        tilings = count_tilings_lazy(guess + 2**power, tiles, n * 50)
        print tilings
        if tilings < n:
            guess += 2**power

    print guess
    print(2**guess * 2**n)  #% 123454321
Example #3
0
def prime_factors(n):
    """returns all prime factors of n"""
    l = {}
    listOfPrimes = getPrimes(n)
    for p in listOfPrimes:
        while n % p == 0:
            n = n / p
            l[p] = l.get(p, 0) + 1
    return l
Example #4
0
File: 70.py Project: dbork/euler
def getTotients(n):
    #totients = map(float, range(n))
    totients = range(n + 1)
    primesList = filter(lambda x: x != 0, primes.getPrimes(n, "log"))

    for prime in primesList:
        for i in range(2, n / prime + 1):
            totients[prime * i] *= (prime - 1)
            totients[prime * i] /= prime

    return totients
Example #5
0
def prob26():
    max = 0
    d = 0
    primes = getPrimes()
    for n in primes:
        if n > 1000: break
        recur = decimalRecurrence(n)
        if recur > max:
            max = recur
            d = n

    print("Max Recurrence: %s - d = %s" % (max, d))
Example #6
0
def getTotients(n):
    totients = range(n + 1)
    primesList = filter(lambda x: x != 0, primes.getPrimes(n, "log"))

    for prime in primesList:
        # since in most useful applications this is what we want, i decree that
        # primes are not relatively prime to themselves
        totients[prime] -= 1

        for i in range(2, n / prime + 1):
            totients[prime * i] *= (prime - 1)
            totients[prime * i] /= prime

    return totients
Example #7
0
def getTruncatedTotients(n, k):
    totients = map(lambda x: x / k, range(n + 1))
    primesList = filter(lambda x: x != 0, primes.getPrimes(n, "log"))

    for prime in primesList:
        # we no longer need this, since p / p is way bigger then 1 / k
        #totients[prime] -= 1

        for i in range(2, n / prime + 1):
            # this is awkward because integer division means we can't
            # just multiply by (p-1)/p, so we'll exploit integer
            # division as follows: (though this doesn't work, see, eg, 12)
            totients[prime * i] -= totients[prime * i] / prime
            #totients[prime * i] *= (prime - 1)
            #totients[prime * i] /= prime

    return totients
Example #8
0
File: 128.py Project: dbork/euler
# situation.

# so 7 doesn't work and 2 does by inspection. the other differences are all
# one less than a multiple of 6. they aren't all covered, but, for ring i,
# the "internal" difference (upper corner minus the lover vertical pal)
# is 6i - 1 and the "external" difference (the other one) is 6(2i + 1) - 1,
# by more-or-less inspection. this is enough that we can be procedural

# so what tile in the sequence do we want?
limit = 2000

# how many primes do we need? since there's a valid PD(n) = 3 number in
# "most-ish" of the rings, we need at most the first 6 * 4 * limit primes or
# so, being more liberal than we probably need because we can
# update: it seems that wasn't liberal enough. oh well
primesList = p.getPrimes(600 * limit, "log")
sequence = [1]

# starting with ring 2
ring = 1
current = 2
# was the last difference prime?
lastDiff = 5

while len(sequence) <= limit:
    # wait, there's one more fact we need. differing from the next
    # vertical in the chain by a multiple of 6 does not guarantee that the
    # difference between its predecessor/successor is prime. this
    # difference also needs to be checked.

    # the first one we check is on the upper corner of ring i
Example #9
0
def main():

    max_number = 0

    if len(sys.argv) > 1:
        max_number = int(sys.argv[1])

    if max_number > 1:

        # We calculate the image dimensions depending on the number specified in the first argument
        salir = False
        comp = 1
        summ = 1
        wh = 0
        offset_x = 0
        offset_y = 0

        while salir == False:
            if(max_number >= comp and max_number < (comp+summ)):
                wh+=1
                salir = True
            else:
                wh+=1
                comp += summ
                summ += 2

        if wh % 2 == 0:
            offset_x = (wh/2)-1;
            offset_y = (wh/2);
        else:
            offset_x = (wh-1)/2;
            offset_y = offset_x;

        ini_x = offset_x
        ini_y = offset_y

        print("wh: %d" % wh)
        print("offset_x: %d" % offset_x)
        print("offset_y: %d" % offset_y)

        # We create a blank image
        img = Image.new('1', (wh,wh))
        pix = img.load()
        
        cont = 0
        times = 1
        cont_2_veces = 0
        dirr = 0
        lo_que_lleva = 0
        lo_que_resta = max_number
        delta = math.ceil(max_number/NUMBERS_LIMIT)
        num_of_primes = 0
        sqrt_all = math.floor(math.sqrt(max_number))
        primes = [0] * sqrt_all
        start = time.time()

        for i in range(delta):

            limit = NUMBERS_LIMIT            

            if lo_que_resta < NUMBERS_LIMIT:
                limit = lo_que_resta

            lo_que_lleva_end = lo_que_lleva + limit

            print("lo_que_lleva: "+str(lo_que_lleva))
            print("lo_que_lleva_end: "+str(lo_que_lleva_end))
            
            primeList = getPrimes(lo_que_lleva,lo_que_lleva_end,primes[:num_of_primes])

            limit_range = limit
            if lo_que_lleva == 0:
                limit_range -= 1

            for j in range(limit_range):

                if primeList[j] == True:
                    
                    pix[offset_x,offset_y] = 1
                    
                    if primes[num_of_primes-1] < sqrt_all:
                        primes[num_of_primes] = j+1+lo_que_lleva
                        num_of_primes += 1
                        
                else:
                    pix[offset_x,offset_y] = 0
                
                if cont == times:
                    if cont_2_veces == 0:
                        cont_2_veces += 1
                    else:
                        times += 1
                        cont_2_veces = 0
                    dirr = (dirr+1)%4
                    cont = 0
                    
                cont += 1

                if dirr == 0:
                    offset_x += 1
                elif dirr == 1:
                    offset_y -= 1
                elif dirr == 2:
                    offset_x -= 1
                else:
                    offset_y += 1

            lo_que_lleva = lo_que_lleva_end
            lo_que_resta -= limit
            primeList = []

        done = time.time()

        print("time: "+str(done-start))

        img.save("ulam.png")
Example #10
0
# kinda boring but straightforward DP i think
import sys
sys.path.append("..")
import math
import primes

primesList = primes.getPrimes(1000000, "log")


def isPrime(n):
    return primesList[n] != 0


filteredPrimesList = filter(isPrime, primesList)

# DP[i][j] should hold the number of ways to make i using primes
# no larger than the jth prime
DP = [[1], [0]]

n = 2
combSum = 0

while combSum <= 5000:
    i = 1
    # there's one way to make n out of primes no greater than 2 if
    # n is divisble by 2, otherwise there are no ways
    ways = [1 - n % 2]

    while filteredPrimesList[i] <= n:
        prime = filteredPrimesList[i]
        waysUsingPrime = 0
Example #11
0
# figure out which primes it could have arisen from. eg for 109673, the
# only possible divisions are 109 673 and 10967 3 by inspection, and
# in fact 10967 isn't prime anyways. this could be interpreted as an edge
# in a graph that has a 5-cycle iff there is a corresponding 5-set.

# but minimality's hard to test... amd also this seems brute-forcey, which
# makes me kinda worried it's not going to be enough. oh well, let's go
# ahead with it anyways and see what happens lol
import sys
sys.path.append("..")
import math
import primes

limit = 10000000
cliqueSize = 5
primesList = primes.getPrimes(limit, "log")
edges = {}
filteredPrimes = filter(lambda x: x != 0, primesList)

for prime in filteredPrimes[4:]:
    # heuristic progress-report clause
    if math.log(prime, 2) - int(math.log(prime, 2)) \
                                              < 1.0 / math.sqrt(prime):
        print "adding edges from " + str(prime)

    primeString = str(prime)
    #print "next prime: " + str(prime)

    for divider in range(1, len(primeString)):
        left = primeString[:divider]
        right = primeString[divider:]