示例#1
0
def test(p):
    for i in xrange(len(p) - 1):
        for j in xrange(i + 1, len(p)):
            s1 = int(str(p[i]) + str(p[j]))
            s2 = int(str(p[j]) + str(p[i]))
            if not (is_prime(s1) and is_prime(s2)): return False
    return True
示例#2
0
def truncate_number(number):
    if len(str(number)) >= 2:
        right_num = number
        len_rnum = len(str(right_num)) - 1
        left_num = number
        len_lnum = len(str(left_num))
        primes_list = []

        while len_rnum > 0:

            right_num = str(right_num)[0:len_rnum]
            right_num = int(right_num)
            rnum_prime = common_functions.is_prime(right_num)

            if rnum_prime == True:
                #print("R:",str(right_num)[0:len_rnum], "is Prime")
                primes_list.append(right_num)
            #else:
            #print("R:",str(right_num)[0:len_rnum], "is NOT Prime")

            len_rnum -= 1

        for y in range(1, len_lnum):
            left_num = int(left_num)
            new_l_num = str(left_num)[y:len_lnum]
            new_l_num = int(new_l_num)
            lnum_prime = common_functions.is_prime(new_l_num)

            if lnum_prime == True:
                #print("L:", str(left_num)[y:len_lnum], "is Prime")
                primes_list.append(new_l_num)
            #else:
            #print("L:", str(left_num)[y:len_lnum], "is NOT Prime")

        if len(str(number)) == 2:
            if len(primes_list) == 2:
                #print(primes_list)
                truncatable_primes.append(number)
        elif len(str(number)) == 3:
            if len(primes_list) == 4:
                #print(primes_list)
                truncatable_primes.append(number)
        elif len(str(number)) == 4:
            if len(primes_list) == 6:
                #print(primes_list)
                truncatable_primes.append(number)
        elif len(str(number)) == 6:
            if len(primes_list) == 10:
                #print(primes_list)
                truncatable_primes.append(number)
示例#3
0
def replace_digit(n):
    'Returns True if number is part of an 8 prime value family'
    string = str(n)
    length = len(string)
    digits = map(int, string)
    unique = list(set(map(int, string)))
    for d in unique:
        count = 10
        if not is_prime(n): count -= 1
        for i in xrange(10):
            if i != d:
                new_digits = [
                    str(i) if ele == d else str(ele) for ele in digits
                ]
                new = int(''.join(new_digits))
                # disregard 0's in the beginning that shorten length
                if new == 0 or math.floor(math.log10(new)) + 1 < length:
                    count -= 1
                else:
                    if not is_prime(new): count -= 1
            if count < 8: break
        if count == 8: return True
    return False
示例#4
0
def num_of_circles(circles):
    x = 8
    current_num = 2
    corners = 2
    diagonals = [1]

    for i in range(circles):
        for z in range(current_num,current_num + x):
            if z == (current_num + x - 1):
                #print("UpperRight:",z-(corners*3))
                #print("UpperLeft:",z-(corners*2))
                #print("LowerLeft:",z-corners)
                #print("LowerRight:",z)

                diagonals.append(z-(corners*3))
                diagonals.append(z-(corners*2))
                diagonals.append(z-corners)
                diagonals.append(z)

        corners += 2
        current_num += x
        x += 8

    num_of_primes_in_diagonals = 0
    for diagonal in diagonals:
        if common_functions.is_prime(diagonal) == True:
            num_of_primes_in_diagonals += 1

    print("\nNumber of Primes:",num_of_primes_in_diagonals)
    print("Number of Diagonals:",len(diagonals))
    percentage = num_of_primes_in_diagonals / len(diagonals) * 100
    percentage = round(percentage, 10)

    if num_of_primes_in_diagonals / len(diagonals) >= 0.1:
        print("\nNumber of Primes is greater than 10%")
        print(percentage,"%")
    elif num_of_primes_in_diagonals / len(diagonals) < 0.1:
        print("\nNumber of Primes is less than 10%")
        print(percentage,"%")

        print("\nLowerLeft:",z-corners)
        print("LowerRight:",z)
        print("Side Length:",(circles*2)+1)
示例#5
0
def goldbachs_other_conjecture() -> int:
    primes = [2]
    squares = [2]
    num_to_square = 1
    num = 1

    while True:
        num += 2

        # if num is prime number ---> add it to the primes list and continue
        if is_prime(num):
            primes.append(num)
            continue

        # if the last squares < num ---> increase num_to_square & add (2 * num_to_square^2) to squares
        if squares[len(squares) - 1] < num:
            num_to_square += 1
            squares.append(2 * num_to_square ** 2)

        # initiate primes_index to last item of primes & squares_index to 0
        primes_index = len(primes) - 1
        squares_index = 0

        while True:
            # run on squares until end or equation >= num
            while squares_index < len(squares) and primes[primes_index] + squares[squares_index] < num:
                squares_index += 1

            # break the loop if found that the condition is not fulfilled
            if primes[primes_index] + squares[squares_index] == num:
                break

            # start again with primes_index - 1
            primes_index -= 1
            squares_index = 0

            # if not found primes_index - num found!
            if primes_index == 0:
                return num
示例#6
0
        prime_len = len(str_prime)
        i = 0

        # Generate the circular numbers
        int_primes_list = []
        while i < prime_len:

            int_primes = int(str_prime)
            int_primes_list.append(int_primes)
            str_prime = str_prime[1:] + str_prime[0]
            i += 1

        #print(int_primes_list)

        # Determine if all circular numbers are primes
        are_all_circ_prime = all(
            common_functions.is_prime(pr) is True for pr in int_primes_list)

        if are_all_circ_prime is True:
            for prime_val in int_primes_list:
                if prime_val not in circular_primes:
                    circular_primes.append(prime_val)

num_of_cir_primes = len(circular_primes)
print(circular_primes)
print('Number of Circular Primes:')
print(num_of_cir_primes)

print('Duration: ')
print(datetime.now() - startTime)
示例#7
0
                           39 18  5  4  3 12 29
                           40 19  6  1  2 11 28
                           41 20  7  8  9 10 27
                           42 21 22 23 24 25 26
                           43 44 45 46 47 48 49

It is interesting to note that the odd squares lie along the bottom right
diagonal, but what is more interesting is that 8 out of the 13 numbers
lying along both diagonals are prime; that is, a ratio of 8/13 62%.

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%?
"""

from common_functions import is_prime

is_count = 8
not_count = 5
side, inc = 7, 8
n = 49

while float(is_count) / (is_count + not_count) > 0.1:
    for i in xrange(4):
        n += inc
        if is_prime(n): is_count += 1
        else: not_count += 1
    side += 2
    inc += 2
print side
示例#8
0
# Project Euler - Problem #41 - Pandigital Prime - SOLVED - 62s

import common_functions
from datetime import datetime
startTime = datetime.now()

x = ''
pandigital_primes = []

for i in range(7000000 ,10000000):
    prime = common_functions.is_prime(i)

    if prime == True:
        x = str(i)
        chars = list(x)
        max_char = max(chars)

        if max_char == "2" and len(chars) == 2:
            if "1" in chars:
                pandigital_primes.append(i)
        elif max_char == "3" and len(chars) == 3:
            if "1" in chars and "2" in chars:
                pandigital_primes.append(i)
        elif max_char == "4" and len(chars) == 4:
            if "1" in chars and "2" in chars and "3" in chars:
                pandigital_primes.append(i)
        elif max_char == "5" and len(chars) == 5:
            if "1" in chars and "2" in chars and "3" in chars and "4" in chars:
                pandigital_primes.append(i)
        elif max_char == "6" and len(chars) == 6:
            if "1" in chars and "2" in chars and "3" in chars and "4" in chars and "5" in chars:
示例#9
0
# Project Euler - Problem #49 - Prime Permutations - Solved 0.02s

import common_functions
from datetime import datetime
startTime = datetime.now()

for x in range(1023, 9997):
    a = common_functions.is_prime(x)

    if a == True:

        for y in range(3330, 3331):
            second_sequence_num = x + y
            b = common_functions.is_prime(second_sequence_num)

            if b == True:
                third_sequence_num = second_sequence_num + y
                c = common_functions.is_prime(third_sequence_num)

                if c == True:
                    first_sequence_str = str(x)
                    second_sequence_str = str(second_sequence_num)
                    third_sequence_str = str(third_sequence_num)

                    first_sequence_set = set(first_sequence_str)
                    second_sequence_set = set(second_sequence_str)
                    third_sequence_set = set(third_sequence_str)

                    final_str = first_sequence_str + second_sequence_str + third_sequence_str
                    final_str_set = set(final_str)
示例#10
0
This is the longest sum of consecutive primes that adds to a prime below
one-hundred.

The longest sum of consecutive primes below one-thousand that adds to a
prime, contains 21 terms, and is equal to 953.

Which prime, below one-million, can be written as the sum of the most
consecutive primes?
"""

from common_functions import prime_list
from common_functions import is_prime

max = 10**6
primes = prime_list(2, max)
sums = [0]

sum, idx = 0, 0
while (sum < max):
	sum += primes[idx]
	sums.append(sum)
	idx += 1

max_prime = 0
for i in range(idx):
	for j in range(i+1, idx):
		diff = sums[j] - sums[i]
		if is_prime(diff) and diff > max_prime: 
			max_prime = diff

print max_prime
示例#11
0
    else:
        prime_sum += prime
        prime_sum_list.append(prime_sum)

prime_dict = {prime_list[i]:prime_sum_list[i] for i in range(0,prime_list_len)}
'''

running_dict = {}

i = 0
while i < prime_list_len:
    x = 1
    running_sum = prime_list[i]
    while x < prime_list_len:
        if x > i:
            running_sum += prime_list[x]
            running_sum_prime = common_functions.is_prime(running_sum)

            if running_sum_prime == True:
                if running_sum < 1000000:
                    difference_len = (x+1)-i
                    running_dict[difference_len] = running_sum 

        x += 1
    i += 1

print(max(running_dict.items()))


print('\nDuration: ')
print(datetime.now() - startTime)
示例#12
0
def character_replacement(list_i):
    for num_of_chars in range(1, len(list_i)):
        print("\nNUM OF CHARS:", num_of_chars)
        #print()
        
        # Replace 1 Character
        if num_of_chars == 1:
            print("Replace 1 Character")
            
            # START OF ORIGINAL
            for x in range(0,len(list_i)):
                val_primes_list = []
                #print("NUM:",i)
                #print("Char Count:",x)

                for num in digits:
                    list_i[x] = str(num)
                    updated_str = "".join(list_i)
                    #print("List_I:", updated_str)
                    updated_num = int(updated_str)

                    if updated_num > 10 and updated_num < 100000:
                        if common_functions.is_prime(updated_num) == True:
                            val_primes_list.append(int(updated_str))
                
                if i not in prime_digits:
                    prime_digits[i] = val_primes_list
                    #print(prime_digits)
                elif i in prime_digits:
                    if len(val_primes_list) >= len(prime_digits[i]):
                        #print(val_primes_list)
                        #print(prime_digits[i])
                        prime_digits[i] = val_primes_list
                        #print(prime_digits)

                list_i = list(str_i)
            # END OF ORIGINAL

        # Replace 2+ characters 
        elif num_of_chars == 2:
            print("Replace 2 Characters")

            for x in range(0,len(list_i)):
                val_primes_list = []
                print("NUM:",i)
                print("Char Count:",x)
                print("LEN LIST I:", len(list_i))

                for num in digits:
                    #print("X:",x)
                    
                    # First Number
                    if x == 0:
                        for num_vals in range(1,len(list_i)-1):
                            list_i[x] = str(num)
                            list_i[x+num_vals] = str(num)
                            updated_str = "".join(list_i)
                            print("List_I:", updated_str)
                            updated_num = int(updated_str)
                            list_i = list(str_i)

                    elif x > 0 and x < len(list_i)-1:
                        num_difference = (len(list_i)-1) - 1
                        for num_vals in range(1,num_difference):

                            if x == 1: 
                                list_i[x] = str(num)
                                list_i[x+num_vals] = str(num)
                            list_i[x] = str(num)
                            list_i[x+1] = str(num)
                            updated_str = "".join(list_i)
                            print("List_I:", updated_str)
                            updated_num = int(updated_str)
                            list_i = list(str_i)
                        
                    # Last Number
                    elif x == len(list_i)-1:
                        for num_vals in range(2,len(list_i)):
                            list_i[x] = str(num)
                            list_i[x-num_vals] = str(num)
                            updated_str = "".join(list_i)
                            print("List_I:", updated_str)
                            updated_num = int(updated_str)
                            list_i = list(str_i)

                    if updated_num > 100 and updated_num < 100000:
                        if common_functions.is_prime(updated_num) == True:
                            val_primes_list.append(int(updated_str))

                if i not in prime_digits:
                    prime_digits[i] = val_primes_list
                    print(prime_digits)
                elif i in prime_digits:
                    if len(val_primes_list) >= len(prime_digits[i]):
                        print(val_primes_list)
                        print(prime_digits[i])
                        prime_digits[i] = val_primes_list
                        print(prime_digits)

                list_i = list(str_i)


        # START WORKING HERE - EVERYTHING ABOVE WORKS
        elif num_of_chars == 3:
            print("Replace 3 Characters")
            for x in range(0,len(list_i)-1):
                val_primes_list = []
                print("NUM:",i)
                print("Char Count:",x)
                print("LEN LIST I:", len(list_i))

                for num in digits:
                    #print("X:",x)
                    
                    if x == 0:
                        for num_vals in range(1,len(list_i)-2):
                            list_i[x] = str(num)
                            list_i[x+num_vals] = str(num)
                            list_i[x+num_vals+1] = str(num)
                            updated_str = "".join(list_i)
                            print("List_I:", updated_str)
                            updated_num = int(updated_str)
                            list_i = list(str_i)

                    elif x > 0 and x < len(list_i)-2:
                        num_difference = (len(list_i)-1) - 1
                        for num_vals in range(1,num_difference):

                            if x == 1: 
                                list_i[x] = str(num)
                                list_i[x+num_vals] = str(num)
                                list_i[x+num_vals+1] = str(num)
                            else: 
                                list_i[x] = str(num)
                                list_i[x+1] = str(num)
                                list_i[x+2] = str(num)
                            updated_str = "".join(list_i)
                            print("List_I:", updated_str)
                            updated_num = int(updated_str)
                            list_i = list(str_i)
                        
                    # Last Number
                    elif x == len(list_i)-1:
                        for num_vals in range(2,len(list_i)):
                            list_i[x] = str(num)
                            list_i[x-num_vals] = str(num)
                            list_i[x-num_vals] = str(num)-2
                            updated_str = "".join(list_i)
                            print("List_I:", updated_str)
                            updated_num = int(updated_str)
                            list_i = list(str_i)

                    if updated_num > 1000 and updated_num < 100000:
                        if common_functions.is_prime(updated_num) == True:
                            val_primes_list.append(int(updated_str))

                if i not in prime_digits:
                    prime_digits[i] = val_primes_list
                    print(prime_digits)
                elif i in prime_digits:
                    if len(val_primes_list) >= len(prime_digits[i]):
                        print(val_primes_list)
                        print(prime_digits[i])
                        prime_digits[i] = val_primes_list
                        print(prime_digits)

                list_i = list(str_i)
示例#13
0



                        if num_of_chars == 5:
                            print("Replace 5 Characters")

                            if num_of_chars == 6:
                                print("Replace 6 Characters") '''
    

digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

prime_digits = {}
for i in range(56003,56004):
    str_i = str(i)
    
    if common_functions.is_prime(i) == True:
        list_i = list(str_i)
        character_replacement(list_i)

max_key = max(prime_digits, key=lambda x: len(set(prime_digits[x])))

print("\nMAX KEY")
print(prime_digits[max_key])
print("Number of Primes:", len(prime_digits[max_key]))

print('\nDuration: ')
print(datetime.now() - startTime)

示例#14
0
                #print(primes_list)
                truncatable_primes.append(number)
        elif len(str(number)) == 4:
            if len(primes_list) == 6:
                #print(primes_list)
                truncatable_primes.append(number)
        elif len(str(number)) == 6:
            if len(primes_list) == 10:
                #print(primes_list)
                truncatable_primes.append(number)

    #else:
    #print("Prime lengh == 1")


for i in range(22, 739398):
    prime_number = common_functions.is_prime(i)

    if prime_number == True:
        #print(i)
        truncate_number(i)
        #print()

print(truncatable_primes)

truncatable_sum = sum(truncatable_primes)
print('# of Values:', len(truncatable_primes))
print("Total Sum:", truncatable_sum)
print()
print('Duration: ')
print(datetime.now() - startTime)