Esempio n. 1
0
def euler50c(upperlimit):

# 1. Find primes under one million.
# 2. Test for each prime:
#   primes[i] + primes[i+1] +... primes[j] until the sume >= prime.
# if the sum == prime, end, if >, break and start again with primes[i+1].


    #create the list of primes
    primes = et.primesfrom2to(upperlimit)
    primeset = set(primes)
    primes_rev = primes[::-1]
    print("len(primes)\t",len(primes))
    print("primes[len(primes)/2]\t",primes[len(primes)/2])
    print("primes[len(primes)-1]\t",primes[len(primes)-1])
    max_prime = 2
    current_max = 0
    start_position = 0
    dict_c = {}

    for starting in range(0,len(primes)):
        temp_max, temp_max_list, initial,end = rec_sum(primes,upperlimit,start_position = starting,current_max = current_max)
        if temp_max_list > current_max:
            current_max = temp_max_list
            max_prime = temp_max
            dict_c[max_prime] = current_max
    print(max_prime,current_max)
Esempio n. 2
0
def euler50c(upperlimit):

# 1. Find primes under one million.
# 2. Test for each prime:
#   primes[i] + primes[i+1] +... primes[j] until the sume >= prime.
# if the sum == prime, end, if >, break and start again with primes[i+1].


    #create the list of primes
    primes = et.primesfrom2to(upperlimit)
    primeset = set(primes)
    primes_rev = primes[::-1]
    print("len(primes)\t",len(primes))
    print("primes[len(primes)/2]\t",primes[len(primes)/2])
    print("primes[len(primes)-1]\t",primes[len(primes)-1])
##    print("sum(primes)\t",sum(primes))
##    print("sum(primes[0:len(primes)/2])\t",sum(primes[0:len(primes)/2]))
    dict_c = {}
    #input("break")
    max_summs = 0
    current = []
    max_prime = 1
    value_max = primes[len(primes)-1]


    for i in range(0,len(primes)+1):
        for j in range(len(primes)+1,i,-1):
##            print(i,j)
            if (j-i) < max_summs:
##                print("breaking ",i,j)
                break
            
            
            else:
                temp_l = primes[i:j]
                temp_prime = sum(temp_l)
                if temp_prime > value_max:
##                    print("Breaking\t temp_prime", temp_prime, value_max)
                    pass
                else:
                    if temp_prime in primeset:
                        if len(temp_l)> max_summs:
                            max_summs = len(temp_l)
                            max_prime = temp_prime
                            try:
                                dict_c[temp_prime].append(temp_l.copy())
                            except:
                                dict_c[temp_prime] = temp_l.copy()


                
    print(max_summs)
##    print(current)
    print(max_prime)
    print(dict_c[max_prime])
Esempio n. 3
0
def euler50(upperlimit):
    primes = et.primesfrom2to(upperlimit)
    primes_rev = primes[::-1]
    print(len(primes))
    dict_c = {}

    ##    print(primes)
    for i in range(0, len(primes_rev)):
        prime = primes_rev[i]
        ##        if prime == 41:
        ##            print("start")
        dict_c[prime] = []
        for j in range(0, len(primes)):
            suma = 0
            start = j
            list_primes = []

            while suma < prime and primes[j] != prime:
                suma += primes[j]
                list_primes.append(primes[j])
                ##                print(suma,i,j,prime)
                if j >= len(primes) - i:
                    ##                    print("breaking", prime,j,i)
                    break
                if suma == prime:
                    ##                    print(start,j)
                    dict_c[prime].append(list_primes.copy())
                    ##                    print(prime)
                    break

                j += 1
##    print(dict_c)
    maxa = 0
    dict_max = dict()
    for i in dict_c.keys():
        try:
            for j in range(0, len(dict_c[i])):
                tmp_max = len(dict_c[i][j])
                if tmp_max >= maxa:
                    maxa = tmp_max
                    try:
                        m = dict_max[maxa]
                        addme = max(m, i)

                    except:
                        pass

                    dict_max[maxa] = i
        except:
            pass

##    print(dict_max)

    print(maxa)
    print(dict_max[maxa])
Esempio n. 4
0
def euler50(upperlimit):
    primes = et.primesfrom2to(upperlimit)
    primeset = set(primes)
    primes_rev = primes[::-1]
    print("len(primes)\t",len(primes))
    print("primes[len(primes)/2]\t",primes[len(primes)/2])
    print("primes[len(primes)-1]\t",primes[len(primes)-1])
    print("sum(primes)\t",sum(primes))
    print("sum(primes[0:len(primes)/2])\t",sum(primes[0:len(primes)/2]))
    dict_c = {}
    #input("break")
    max_summs = 0
    current = []
    max_prime = 1
    for i in range(0, len(primes_rev)):
        prime = primes_rev[i]
        set_tried =set()
##        dict_c[prime] = []
        for j in range(0,len(primes)):
            suma = 0
            start = j
            list_primes = []
            count = 0
                        
            while suma < prime and primes[j] != prime:
                
                count += 1
                suma += primes[j]
                if suma in set_tried:
                    break
                list_primes.append(primes[j])
                if suma in primeset:
                    set_tried.add(suma)
                if j >= len(primes) - i:
                    break
                if suma == prime:                    
                    if count > max_summs:
                        max_summs = count
                        current = list_primes.copy()
                        max_prime = prime
                        try:
                            dict_c[prime].append(list_primes.copy())
                        except:
                            dict_c[prime] = list_primes.copy()
                    break
                
                j += 1
                
    print(max_summs)
##    print(current)
    print(max_prime)
Esempio n. 5
0
def euler43():
    """Generates listofprimes < 18, evaluates permutations 0-9 for Euler43"""

    lp = et.primesfrom2to(18)
    print(lp)
    s = ""
    for x in range(0, 10):
        s += str(x)

    listes = []
    lv = []
    for n in itertools.permutations(s):
        sn = createseq(n)
        if evaluaten(sn, lp):
            listes.append(int(sn))

    print(sum(listes))
    print(listes[1])
    print(evaluate(str(listes[1]), lp))
Esempio n. 6
0
def euler49(end):
    primes = et.primesfrom2to(10000)
    primes = primes[::-1]
    print(len(primes))
    count = 0
    check = 0
    dict_c = {}
    checked = set()
    for prime in primes:
        if prime in checked:
            pass
        elif len(str(prime)) < 4:
            pass
        else:
            listn = [x for x in str(prime)]
            listn = set(getpermutationlist(listn))
            listn = [x for x in listn]
            listn = sorted(listn, reverse=True)
            ##            print(listn)
            for next_prime in listn:
                if len(str(next_prime)) < 4:
                    pass
                elif next_prime < prime and next_prime in primes:
                    ##                    checked.add(next_prime)

                    ##                    print(prime,next_prime)
                    a = prime - next_prime
                    b = next_prime - a
                    if len(str(b)) < 4:
                        pass
                    elif b in primes and b in listn:
                        dict_c[count] = [prime, next_prime, b, a]
                        count += 1
                    else:
                        pass
                else:
                    pass

        if check == len(primes) / 3:
            print(check, prime)
        check += 1
    print(dict_c)
Esempio n. 7
0
def alternate2(primes, val):
    '''Deduce the way the numbers increase as a function of the increased side. Apply that knowdledge to 
    infer the turning point'''
    #intialize
    diagonals = [1, 3, 5, 7, 9]
    dist = len(diagonals)
    pmax = max(primes)
    maxd = max(diagonals)
    ratio = 1
    pr_list = [1 if x in primes else 0 for x in diagonals]

    prs = sum(pr_list)
    n = 3

    #loop
    while ratio > val:
        n += 2

        if maxd < pmax:
            primes = primes
        else:
            primes = set(eulertools.primesfrom2to(100 * pmax))
            pmax = max(primes)

        for i in range(1, 5):
            value = maxd + i * (n - 1)
            #diagonals.append(value)
            if value in primes:
                prs += 1
        #ext_list = [maxd+(n-1), maxd+2*(n-1), maxd + 3*(n-1), maxd+4*(n-1)]
        #ext_pr = [1  if x in primes else 0 for x in ext_list]

        #diagonals.extend(ext_list)
        #pr_list.extend(ext_pr)

        maxd = maxd + 4 * (n - 1)
        dist += 4
        ratio = prs / dist

    return n, ratio, prs, maxd, dist, pmax  #, diagonals
Esempio n. 8
0
def euler49(end):
    primes = et.primesfrom2to(10000)
    print(len(primes))
    count = 0
    check = 0
    dict_c = {}
    for prime in primes:
        for next_prime in primes:
            if next_prime < prime:
                if set(str(next_prime)) == set(str(prime)):
                    a = prime - next_prime
                    b = next_prime - a
                    if b in primes:
                        if set(str(b)) == set(str(prime)):
                            dict_c[count] = [prime, next_prime, b, a]
                            count += 1
            else:
                break
        if check == len(primes) / 3:
            print(check, prime)
        check += 1
    print(dict_c)
Esempio n. 9
0
def euler47(n_divisors, n_consecutive):
    count = 0
    primes = et.primesfrom2to(1000)
    n = 3
    flag = True
    while flag == True:
        if n not in primes:
            np_opd = divisors(n, primes)
            primeset = set(np_opd)
            if len(primeset) == n_divisors:
                count += 1


##                print(n,np_opd,primeset)
            else:
                if count != 0:
                    count = 0
            if count == n_consecutive:
                print(n - count + 1, np_opd, primeset)
                flag = False
        else:
            count = 0
        n += 1
Esempio n. 10
0
def euler46():
    n = 3
    primes = et.primesfrom2to(1000000)
    flag = True
    while flag == True:
        if n not in primes:
            check = 0
            # small numbers only, I will increase primes only if needed.
            for i in primes:
                if i < n:
                    try:
                        result = n - i
                        ##                        print('testing... ' + str(n) + ' ' + str(i))
                        if int(np.sqrt(result / 2)) == np.sqrt(result / 2):
                            ##                            print(n,i)
                            check = 1

                            break
                    except:
                        print("error in try")
            if check == 0:
                print(n)
                flag = False
        n += 2
Esempio n. 11
0
def testme():
    number = 645
    npprimes = et.primesfrom2to(1000)
    np_opd = np.array([], dtype=np.int32)
    n, npp, np_opd = test_arr(number, npprimes, np_opd)
    print(np_opd)
Esempio n. 12
0
def euler50b(upperlimit):

# 1. Find primes under one million.
# 2. Test for each prime:
#   primes[i] + primes[i+1] +... primes[j] until the sume >= prime.
# if the sum == prime, end, if >, break and start again with primes[i+1].


    #create the list of primes
    primes = et.primesfrom2to(upperlimit)
    primeset = set(primes)
    primes_rev = primes[::-1]
    print("len(primes)\t",len(primes))
    print("primes[len(primes)/2]\t",primes[len(primes)/2])
    print("primes[len(primes)-1]\t",primes[len(primes)-1])


#[a,b,c,...,n-1,n]
# while t_sum < 10^6: increase t_sum with next prime.
# t_sum = a + b + c + ... + (n-1) + n
# once t_sum > 10^6, go back to t_sum < 10^6.
# if t_sum not prime, subtract:
# (a)
# if (t_sum-a) not prime, try: (t_sum-n)
# if that is not prime, try: t_sum - a - n, or t-sum-a-b, or t_sum - n - (n-1)
# etc. until the result is prime.
# repeat starting with "b", as long as the list of primes that
# sum up to <10^6 is longer than the list of primes that sum to
# the last prime.

    
    max_summs = 0
    max_prime = 1
    


##    print("sum(primes)\t",sum(primes))
##    print("sum(primes[0:len(primes)/2])\t",sum(primes[0:len(primes)/2]))
    dict_c = {}
    #input("break")
    max_summs = 0
    current = []
    max_prime = 1
      
    for i in range(0, len(primes_rev)):
        prime = primes_rev[i]
        set_tried =set()
##        dict_c[prime] = []
        for j in range(i+1,len(primes_rev)-1):
            diff = prime
            start = j
            list_primes = []
            count = 0
                        
            while diff > 0 and j <len(primes_rev):
                
                count += 1
                try:
                    diff -= primes_rev[j]
                except:
                    print(diff,i,j,len(primes_rev))
                if diff in set_tried:
                    break
                list_primes.append(primes_rev[j])
                if diff in primeset:
                    set_tried.add(diff)
##                if j >= len(primes) - i:
##                    break
                if diff == 0:                    
                    if count > max_summs:
                        max_summs = count
                        current = list_primes.copy()
                        max_prime = prime
                        try:
                            dict_c[prime].append(list_primes.copy())
                        except:
                            dict_c[prime] = list_primes.copy()
                    break
                
                j += 1
                
    print(max_summs)
##    print(current)
    print(max_prime)
    print(dict_c[max_prime])
Esempio n. 13
0
def euler50b(upperlimit):

# 1. Find primes under one million.
# 2. Test for each prime:
#   primes[i] + primes[i+1] +... primes[j] until the sume >= prime.
# if the sum == prime, end, if >, break and start again with primes[i+1].


    #create the list of primes
    primes = et.primesfrom2to(upperlimit)
    primeset = set(primes)
    primes_rev = primes[::-1]
    print("len(primes)\t",len(primes))
    print("primes[len(primes)/2]\t",primes[len(primes)/2])
    print("primes[len(primes)-1]\t",primes[len(primes)-1])
##    print("sum(primes)\t",sum(primes))
##    print("sum(primes[0:len(primes)/2])\t",sum(primes[0:len(primes)/2]))
    dict_c = {}
    #input("break")
    max_summs = 0
    current = []
    max_prime = 1
      
    for i in range(0, len(primes_rev)):
        prime = primes_rev[i]
        set_tried =set()
##        dict_c[prime] = []
        for j in range(i+1,len(primes_rev)-1):
            diff = prime
            start = j
            list_primes = []
            count = 0
                        
            while diff > 0 and j <len(primes_rev):
                
                count += 1
                try:
                    diff -= primes_rev[j]
                except:
                    print(diff,i,j,len(primes_rev))
                if diff in set_tried:
                    break
                list_primes.append(primes_rev[j])
                if diff in primeset:
                    set_tried.add(diff)
##                if j >= len(primes) - i:
##                    break
                if diff == 0:                    
                    if count > max_summs:
                        max_summs = count
                        current = list_primes.copy()
                        max_prime = prime
                        try:
                            dict_c[prime].append(list_primes.copy())
                        except:
                            dict_c[prime] = list_primes.copy()
                    break
                
                j += 1
                
    print(max_summs)
##    print(current)
    print(max_prime)
    print(dict_c[max_prime])
Esempio n. 14
0
# If one complete new layer is wrapped around the spiral above, a square spiral with side length 9 will be formed. If this process is continued, what is the side length of the square spiral for which the ratio of primes along both diagonals first falls below 10%?

# #Approach:
# ####1. Create matrix
# ####2. Fill matrix
# ####3. Check if diagonals are prime
# ####4. Calculate %

# In[1]:

import numpy as np
import eulertools

# In[2]:

primes = set(eulertools.primesfrom2to(999999999))

#100000000))#299999999))

# In[3]:


#@profile
def alternate2(primes, val):
    '''Deduce the way the numbers increase as a function of the increased side. Apply that knowdledge to 
    infer the turning point'''
    #intialize
    diagonals = [1, 3, 5, 7, 9]
    dist = len(diagonals)
    pmax = max(primes)
    maxd = max(diagonals)