예제 #1
0
def main(d):
    #solves defined problem for d = d

    desired = d - 1
    #will count number of fractions, initialized with number of fractions with numerator 1

    import find_primes_below
    #find_primes_below.main(x) returns a list of all primes below x

    primes_list = find_primes_below.main(d)
    #finds all primes below d

    import factor_given_primes
    #factor given primes.main(number,prime_list) returns list of prime factors of number

    for num in range(2, d):
        #want to search through numerators from 2 to d-1

        num_factors = list(set(factor_given_primes.main(num, primes_list)))
        #finds prime factors of numerator -- list/set part removes duplicates

        for denom in range(num + 1, d + 1):
            #want denominators from one greater than numerator to d

            index = 0
            #used to track index while searching through prime factors

            des_increase = 1
            #will flip to zero if denominator is divisible by any factors of numerator

            len_fact = len(num_factors)
            #finds number of factors of numerator (top index for divisibility search)

            while des_increase == 1 and index < len_fact:
                #continues until a divisor is found or all factors of numerator have been searched through

                if denom % num_factors[index] == 0:
                    #flips check number if prime factor of numerator evenly divides denominator

                    des_increase = 0

                index += 1
                #increments factor index by 1

            desired += des_increase
            #adds des_increase to count of fractions (1 if unique, 0 if not, i.e. denom and numerator share at least 1 prime factor)

    return desired
예제 #2
0
def main():
    #solves defined problem by searching through pairs of primes

    primes = find_primes_below.main(10**4)
    #arbitrarily chosen cap for primes based on problem cap of 10^7

    stored_num = [0, 0]
    #will store highest totient / n as well as the number n

    for i in primes:

        for j in primes:

            n = i * j
            #since we are searching for numbers with 2 prime factors

            if n < (10**7):
                #requirement for max n

                p_list = [i, j]
                tot_div_n = totient_over_n(p_list)
                #calculates totient/n using above function

                totient = tot_div_n * n
                #calculates the totient

                if totient in number_permutations.main(n):
                    #i.e. if φ(n) is a permutation of n

                    if tot_div_n > stored_num[0]:
                        #i.e. if higher than last previous highest totient/n found

                        stored_num = [tot_div_n, n]
                        #stores numbers to be compared to or for answer if best

                        #TEST
                        print('i=', i, 'j=', j, 'stored_num=', stored_num)
#TEST

    return stored_num
예제 #3
0
#644 = 2² × 7 × 23
#645 = 3 × 5 × 43
#646 = 2 × 17 × 19.

#Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers?


import find_primes_below	#find_primes_below.main(x) returns a list containing all prime numbers below x

import factor_given_primes	#factor_given_primes.main(number,primes) returns list containing all prime factors of number (including duplicates), or empty list if factoring fails

four_factor_numbers = []

search_end = 1000	#arbitrarily chosen end for search

primes_list = find_primes_below.main(search_end)

desired_set = []	#will hold the numbers we want to find


i = 1
while i < search_end ** 2 and desired_set==[]:		#arbitrarily chosen end for search

	factors_list = 	factor_given_primes.main(i, primes_list)	#gets list of prime factors

	if len(set(factors_list)) == 4:		#checks that number has exactly 4 prime factors

		four_factor_numbers.append(i)	#if it does, adds to list

		if len(four_factor_numbers) >= 4:
예제 #4
0
#The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another.

#There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.

#What 12-digit number do you form by concatenating the three terms in this sequence?




import find_primes_below
primes_list = find_primes_below.main(10000)	#gets list of all primes below 10000 (4 digits or less)


import number_permutations 			#number_permutations.main(x) returns a list of all permutations of x


matches_criteria=[]				#will hold all numbers meeting desired criteria


for i in primes_list:

	#NOT NECESSASRY AND MISSES SOME## if len("".join(set(str(i)))) == 4:	#checks that there are no duplicate digits in i

		permutations = number_permutations.main(i)	#gets permutations of i

		prime_permutations = []				#starts list of permutations that are prime

		for j in permutations:				#loop adds any prime permutations to list

			if j in primes_list:
예제 #5
0
#The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.

#There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

#How many circular primes are there below one million?

import find_primes_below

primes_list = find_primes_below.main(
    10**6)  #gets list of all primes below 10^6

import circular_rotations  #circular_rotations.main(x) returns a list of all permutations of x

circular_primes = []

for i in primes_list:

    rotations = circular_rotations.main(i)  #gets circular_rotations of i

    prime_permutations = 1  #to be used for a check later

    for j in rotations:  #checks if all of the permutations are in the list of primes

        if prime_permutations == 1:  #works to stop checking once one non-prime has been found

            if j not in primes_list:  #loop switches check to 0 if any non-prime permutation is found

                prime_permutations = 0

    if prime_permutations == 1:  #checks that all permutations are prime
예제 #6
0
def main(x):
#solves defined problem for first prime with x-prime value family

	prime_list = find_primes_below.main( 10 ** 6 )
	#finds all primes below (10^6) and puts them in a list

	prime_index = 25
	#keeps track of index for prime that is being tried
	#starts with 25 as that is the index of the first 3-digit prime
	#11412

	number_of_primes = len (prime_list)
	#finds number of primes less than 10^6 (to use as max index cap)

	while prime_index < ( number_of_primes - 1 ):
	#searches until desired result found or all primes exhausted

		prime = prime_list[ prime_index ]
		#pulls the prime from the prime list

		prime_str = str ( prime )
		#gets a string of the prime

		prime_length = len( prime_str )
		#finds length of prime

		check_repeats = []
		for q in range( 0, 10 ):
		#check if any digit repeats in prime, if it does then adds repeat to list

			if prime_str.count( str( q ) ) >= 2:

				check_repeats.append( q )

#TEST
		print('prime=',prime)
#TEST



		if check_repeats != []:
		#loop only executes if prime has at least one digit that repeats

			for i in check_repeats:
			#will run multiple times if there are multiple digits that repeat

				found_primes=[ prime_str ]
				#will store any primes found in digit replacements
				#resets list for each repeating digit

				for k in range( 0, 10 ):
				#try replacing digits with 0-9

					prime_l = list ( prime_str )
					#creates a list from prime_str to allow item assignment
					#resets for each k (trial digit replacement)

					for j in range( 0, prime_length ):
					#replacement digit index, runs through length of prime

						if int( prime_str[ j ] ) == i:
						#i.e. if (j-1)th digit of prime is the repeating digit i

							prime_l[ j ] = str( k )
							#replaces the repeating digit of prime_list with k

#TEST
#TEST							print('prime_l[',j,']=',str(k))
#TEST

					new_str = ''.join(prime_l)
					#converts back to prime_str to get correct formatting with digits replaced

#TEST
#TEST					print('new_str=',new_str)
#TEST

					if new_str != prime_str:
					#prevents original prime being repeatedly added to list

						if len( str( int( new_str ) ) ) == len( prime_str ):
						#prevents leading zeroes

							if int( new_str ) in prime_list:

								found_primes.append ( new_str )
								#adds any primes found in digit replacement to found_primes list

#TEST
#TEST				print(found_primes)
#TEST

				if len( found_primes ) == x:
				#i.e. if prime is part of an x-prime value family

					return found_primes

		prime_index += 1
		#increments prime_index

	return 0
예제 #7
0
def main(x):
#solves defined problem for first prime with x-prime value family

	prime_list = find_primes_below.main( 2 * 10 ** 6 )
	#finds all primes below (10^6) and puts them in a list

	prime_index = 25
	#keeps track of index for prime that is being tried
	#starts with 25 as that is the index of the first 3-digit prime
	#11412

	prime = prime_list[ prime_index ]
	#pulls the prime from the prime list

	prime_str = str ( prime )
	#gets a string of the prime

	number_of_primes = len (prime_list)
	#finds number of primes less than 10^6 (to use as max index cap)

	while prime_index < ( number_of_primes - 1 ):
	#searches until desired result found or all primes exhausted

		prime_length = len( prime_str )
		#finds length of prime

		check_repeats = 0
		for q in range( 0, 10 ):
		#check if any digit repeats in prime, if it does switches check to 1

			if prime_str.count( str( q ) ) >= 2:

				check_repeats = 1

		if check_repeats == 1:
		#loop only executes if prime has at least one digit that repeats

			for i in range( 0, prime_length - 1 ):
			#first replacement digit index

				for j in range(i+1, prime_length ):
				#second replacement digit index

					prime_str = str ( prime )
					#resets prime string for each i,j combination

					if int( prime_str[i] ) == int( prime_str[j] ):
					#only executes if digits match in original prime

#TEST
#						print('i=',i,'j=',j)
#TEST

						found_primes=[]
						#will store any primes found in digit replacements
						#resets list for each i,j combination

						for k in range( 0, 10 ):
						#try replacing digits with 0-9


							prime_l = list ( prime_str )
							#creates a list from prime_str to allow item assignment

							prime_l[i] = str( k )
							prime_l[j] = str( k )
							#replaces the 2 digits i,j of prime_list with k

							prime_str = ''.join(prime_l)
							#converts back to prime_str to get correct formatting with digits replaced

	#TEST
	#TEST						print(prime_str)
	#TEST

	#TEST
	#TEST						print('i=',i,'j=',j,'k=',k)
	#TEST

							if i != 0 or k != 0:
							#prevents leading zeroes

								if int( prime_str ) in prime_list:

									found_primes.append ( prime_str )
									#adds any primes found in digit replacement to found_primes list

	#TEST
						print(found_primes)
	#TEST

						if len( found_primes ) == x:
						#i.e. if prime is part of an x-prime value family

							return found_primes

		prime_index += 1
		prime = prime_list[ prime_index ]
		prime_str = str ( prime )
		#increments prime_index and gets a string of the next prime in prime_list

#TEST
		print('prime=',prime)
#TEST

		check_repeats = 0
		for q in range( 0, 10 ):
		#check if any digit repeats in prime, if it does switches check to 1

			if prime_str.count( str( q ) ) >= 2:

				check_repeats = 1


	return 0