next_prime = 1 while next_prime < 99999: logging.debug(f"good_tups currently {good_tups}") next_prime = getNextOddPrime(next_prime) logging.info(f"next_prime = {next_prime}") insert_idx = len(good_tups) logging.debug(f"appending next_prime {next_prime} to good_tups") good_tups.append((next_prime, )) for i in range(insert_idx): # case 1: good_tups[i] is len 1 if len(good_tups[i]) == 1: if (isPrime(int(str(good_tups[i][0]) + str(next_prime))) and isPrime(int(str(next_prime) + str(good_tups[i][0])))): new_tup = (good_tups[i][0], next_prime) logging.debug(f"appending case 1 new tup {new_tup}") good_tups.append(new_tup) # case 2: good tups[i] is len >1 elif 1 < len(good_tups[i]) < 5: logging.debug(f"in case 2; {good_tups[i]} + {next_prime}") is_good = True for n in good_tups[i]: if (n, next_prime) not in good_tups[insert_idx:]: is_good = False break if is_good == True: new_tup = good_tups[i] + ((next_prime, ))
#!/usr/bin/python3 import time tStart = time.time() import stevepe largestPan = 0 for n in range(3, 987654321 + 1, 2): if stevepe.isIntPandigital(n): if stevepe.isPrime(n): if n > largestPan: largestPan = n print(__file__ + ": answer: {!s}".format(largestPan)) print("Run time: " + '{:.20f}'.format(time.time() - tStart) + " sec")
MAX_PRIME = 1000000 p = tuple(stevepe.getPrimesBelow(MAX_PRIME)) # strategy: define max and min window size; # for each window size i, slide a window across the set of primes. # Once we find a sequence of len i, quit, because we're looking # for a long one. WINDOW_LEN_MAX = 50000 WINDOW_LEN_MIN = 1 for window_len in range(WINDOW_LEN_MAX, WINDOW_LEN_MIN-1, -1): print("window len = {!s}".format(window_len)) i = 0 thisSum = sum(p[i:(i+window_len)]) #print("calculating sum over indices {!s}...{!s} inclusive".format(i, i+window_len-1)) while (i+window_len) < len(p) and thisSum < MAX_PRIME: if stevepe.isPrime(thisSum): # hooray! Found a long prime sum. Print and exit. print("{}: answer: {!s} ({!s} [{!s}] + ... + {!s} [{!s}])".format(__file__, thisSum, p[i], i, p[i+window_len-1], i+window_len-1)) print("Run time: " + '{:.20f}'.format(time.time() - tStart) + " sec") exit() # slide window if (i+window_len) < len(p): #print("calculating sum over {!s} ... {!s} inclusive".format(i+1, i+window_len)) thisSum = thisSum - p[i] + p[i+window_len] i += 1
ret = set() i = 1 for thisPow in range(math.floor(math.log(n,10))): temp = n % (10**(thisPow+1)) ret.add(temp) #print("left truncations = " + str(ret)) return ret import stevepe n = 9 count = 0 allSum = 0 while count < 11: n += 2 possiblyPrime = True if not stevepe.isPrime(n): possiblyPrime = False next for possPrime in truncateFromRight(n): if not stevepe.isPrime(possPrime): possiblyPrime = False break for possPrime in truncateFromLeft(n): if not stevepe.isPrime(possPrime): possiblyPrime = False break if possiblyPrime: print("Found a truncatable prime! = " + str(n)) count += 1 allSum += n
if args.log: loglevel = getattr(logging, args.log.upper()) else: loglevel = logging.WARN logging.basicConfig(level=loglevel) import time tStart = time.time() side_len = 1 primes_count = 0 total_count = 1 last_digit = 1 while True: side_len += 2 for i in range(4): last_digit += (side_len - 1) if isPrime(last_digit): primes_count += 1 total_count += 4 logging.debug(f"{primes_count} / {total_count}") if primes_count / total_count < .1: break print("{}: answer: {!s}".format(__file__, side_len)) print("Run time: " + '{:.20f}'.format(time.time() - tStart) + " sec")
def countConsecQuadPrimes(a, b): n = 0 while True: if not isPrime((n**2) + (a * n) + b): return n n += 1
# ignore 2, since 2 cat'ed at the end of any number will be composite. next_prime = 1 for i in range(4): next_prime = getNextOddPrime(next_prime) primes_list_strs.append(str(next_prime)) next_prime = getNextOddPrime(next_prime) while next_prime < 100000: logging.debug(f"next prime = {next_prime}") primes_list_strs.append(str(next_prime)) next_prime = getNextOddPrime(next_prime) for combo_seed in combinations(primes_list_strs, 4): combo_five = combo_seed + (str(next_prime), ) found_it = True for pair in combinations(combo_five, 2): if not isPrime(int(pair[0] + pair[1])): found_it = False break if not isPrime(int(pair[1] + pair[0])): found_it = False break if found_it: logging.debug(f"found candidate; combo_five = {combo_five}") combo_sum = 0 for item in combo_five: combo_sum += int(item) if combo_sum < lowest_sum or lowest_sum == None: logging.debug(f"found low sum candidate = {combo_sum}") lowest_sum = combo_sum print("{}: answer: {!s}".format(__file__, lowest_sum))
def getShiftsSet(n): ret = set() nList = [] while n > 0: nList.append(n % 10) n = n // 10 lenNList = len(nList) for i in range(lenNList): newNum = 0 for j in range(lenNList): newNum *= 10 newNum += nList[(i + j) % lenNList] ret.add(newNum) return ret count = 0 for n in range(2, MAX_TO_TRY + 1): allPrimes = True for num in getShiftsSet(n): if not stevepe.isPrime(num): allPrimes = False break if allPrimes: count += 1 print(__file__ + ": answer: " + str(count)) print("Run time: " + '{:.20f}'.format(time.time() - tStart) + " sec")
#!/usr/bin/python3 import time tStart = time.time() import stevepe for a in range(1001, 10000, 2): if not stevepe.isPrime(a): continue for step in range(2, (10001-a//2)+1, 2): b = a + step c = a + step + step perms = stevepe.getIntPermutations(a) if b not in perms or c not in perms: continue if not stevepe.isPrime(b) or not stevepe.isPrime(c): continue print("{}: possible answer: {!s}, {!s}, {!s}".format(__file__, a, b, c)) print("Run time: " + '{:.20f}'.format(time.time() - tStart) + " sec")
# swap in each of the 10 integers for i in range(10): # don't want to swap 0 in the first position. if i == 0 and 0 in thisComboIndices: continue thisPrimeSwapAsList = thisPrimeAsList.copy() for idx in thisComboIndices: thisPrimeSwapAsList[idx] = str(i) # turn list back into an int thisPrimeSwapAsStr = ''.join(str(c) for c in thisPrimeSwapAsList) logging.debug(" this swap = {}".format(thisPrimeSwapAsStr)) if stevepe.isPrime(int(thisPrimeSwapAsStr)): resultingPrimesList.append(int(thisPrimeSwapAsStr)) logging.debug(" resulting primes count = {!s}".format(len(resultingPrimesList))) if len(resultingPrimesList) == FAMILY_LEN: # YAY! We found what we're looking for. print("\n\n{}: answer: {!s}".format(__file__, min(resultingPrimesList))) print("Run time: " + '{:.20f}'.format(time.time() - tStart) + " sec") sys.exit() # iterate to next prime thisPrimeIdx += 1 if thisPrimeIdx < len(primes): thisPrime = primes[thisPrimeIdx] thisPrimeAsList = list(str(thisPrime))