permutations[prime_sorted_digits].append(prime) else: permutations[prime_sorted_digits] = [prime] # Keep only those with at least three primes permutations = { sorted_digits: primes for sorted_digits, primes in permutations.items() if len(primes) >= 3 } # Get arithmetic sequences of length 3 progressives = [] for primes in permutations.values(): # Pick all prime triplets for combo in combinations(primes, 3): #Check for progression if combo[2] - combo[1] == combo[1] - combo[0]: progressives.append(combo) #There should be two sequences assert len(progressives) == 2 # Remove known sequence progressives.remove((1487, 4817, 8147)) the_seq = progressives[0] return ''.join(str(prime) for prime in the_seq) if __name__ == '__main__': start.time_functions(p49)
#dn_todo = [1,10] dn_list = [] n = 0 num = 0 while len(dn_todo): num += 1 pre_n = n n += len(str(num)) #Can optimise this while len(dn_todo) and pre_n <= dn_todo[0] and dn_todo[0] <= n: n_wanted = dn_todo[0] del dn_todo[0] #We note: #Current num is the number leading to the n boundary #1234 # * ^ # n=102 dn_list.append(str(num)[-(n - n_wanted) - 1]) p = 1 for x in [int(s) for s in dn_list]: p *= x return p if __name__ == "__main__": import utility.start as start start.time_functions(p40)
''' https://projecteuler.net/problem=41 Assuming n can be 9 maximum. n cannot be 9 because sum of 1, ..., 9 is 45 and thus divisible by 9. n cannot be 8 because sum of 1, ..., 8 is 36 and thus divisible by 9. Thus I am assuming n is 7. ''' from utility import start, primes2, generic def p41(): MAX_NUM = 7654321 prime_list = primes2.PrimeList() prime_list.sieve(MAX_NUM) max_prime_pandigital = 0 for prime in prime_list.get_primes_in(generic.grange(MAX_NUM, -1)): if ''.join(sorted(str(prime))) == '1234567': max_prime_pandigital = prime break return max_prime_pandigital if __name__ == '__main__': start.time_functions(p41)
def concatenated_product(k, n): single_products = [k * i for i in range(1, n + 1)] return int(''.join([str(product) for product in single_products])) def is_pandigital(number): return sorted(str(number)) == ['1', '2', '3', '4', '5', '6', '7', '8', '9'] def p38(): # k = 1 cases: largest_pandigital = concatenated_product(1, 9) checks = [(5, 9, 5), (25, 33, 4), (100, 333, 3), (5000, 9999, 2)] for check in checks: k_start = check[0] k_end = check[1] n = check[2] for k in range(k_start, k_end + 1): possible = concatenated_product(k, n) if possible > largest_pandigital and is_pandigital(possible): largest_pandigital = possible return largest_pandigital if __name__ == '__main__': start.time_functions(p38)
''' import itertools from utility import start def p43(): def check_three(number, start, div_by): return ((100 * number[start] + 10 * number[start + 1] + number[start + 2]) % div_by == 0) found = [] base = '0123456789' for pd in itertools.permutations(base): #Filler just so d_1 corresponds to 1st digit pd = ['filler'] + [int(digit) for digit in pd] if pd[4] in (0, 2, 4, 6, 8): #Could optimise ordering of ifs: if sum(pd[3:6]) % 3 == 0: if pd[6] in (0, 5): if check_three(pd, 5, 7): if check_three(pd, 6, 11): if check_three(pd, 7, 13): if check_three(pd, 8, 17): found.append(pd[1:]) return sum(int(''.join(str(digit) for digit in snum)) for snum in found) if __name__ == '__main__': start.time_functions(p43)
odd = 2 + 2*square ==> prime != 2 1, 3, 5, 7 are not composite; Problem checked up to 33. Work up from 35, 37... and subtract 2*square and see if any is prime. square starts from 1, because odd must be a composite. square inclusively ends at math.floor(math.sqrt((odd - 3) / 2)) ''' import math from utility import start, generic, primes2 def p46(): primes = primes2.PrimeList() for odd in generic.grange(35, 2): if primes.is_prime(odd): continue for psquare in range(1, math.floor(math.sqrt((odd - 3) / 2)) + 1): square = 2 * psquare**2 if primes.is_prime(odd - square): break else: return odd if __name__ == '__main__': start.time_functions(p46)
""" import fractions from utility import start def p33(): the_fracs = [] for denominator in range (11, 100): c = denominator // 10 d = denominator % 10 if d == 0: continue for numerator in range(11, denominator): a = numerator // 10 b = numerator % 10 if b == 0: continue frac = numerator/denominator if (a == c and frac == b/d) or (a == d and frac == b/c) or\ (b==c and frac == a/d) or (b == d and frac == a/c): the_fracs.append(fractions.Fraction(numerator, denominator)) final_frac = fractions.Fraction(1) for frac in the_fracs: final_frac *= frac return final_frac.denominator if __name__ == '__main__': start.time_functions(p33)
new_prime = int(str_prime[i:]) if not pl.is_prime(new_prime): break else: truncated_primes.append(new_prime) else: return truncated_primes return None def p37a(): real_answer = [] pg = pl.get_primes(10) prime = 2 while prime < 10**6: prime = next(pg) truns = get_truncates(prime) if truns: real_answer.append(prime) if len(real_answer) == 11: break return sum(real_answer) if __name__ == '__main__': import utility.start as start start.time_functions(p37a)
max_l = current_l return the_p def p39b(): py_triples = {} for p in range(12, 1001, 2): max_a = m.ceil((p / 3) - 1) for a in range(3, max_a + 1): b = p * (2 * a - p) / (2 * (a - p)) #Must be whole number #print(p, a, b, p-a-b) if m.floor(b) == m.ceil(b): #We have a triple if p not in py_triples: py_triples[p] = [] py_triples[p].append((a, b)) the_p = 0 max_l = 0 for perimeter, solns in py_triples.items(): current_l = len(solns) if current_l > max_l: the_p = perimeter max_l = current_l return the_p if __name__ == '__main__': import utility.start as start start.time_functions(p39b)
from utility import start, integers def get_working_size(n, r): """Return all k s.t. nCk > X, given smallest r such that nCr > X""" return n - 2 * r + 1 def p53(): num_values = 0 # First ones that work from question n = 23 r = 10 while n <= 100: # We under-shoot the r value by 1: while integers.choose(n, r) >= 10**6: r -= 1 # The last r that worked is actually r + 1: num_values += get_working_size(n, r + 1) n += 1 return num_values if __name__ == '__main__': start.time_functions(p53)
break cur_answer = maths.floor(numerator[current_num_i] / denom) carry = numerator[current_num_i] % denom try: numerator[current_num_i+1] = carry * 10 +\ numerator[current_num_i+1] except IndexError: numerator.append(carry*10) answer.append(cur_answer) working_number_list.append(numerator[current_num_i]) #print(list(set(working_number_list)), working_number_list) repeating_section_info.append((denom, len(repeat_section), repeat_section)) #Analyse repeating_section_info max_length = (0, 0, 0) for denom, length, section in repeating_section_info: print(denom, length, section) if length > max_length[1]: max_length = (denom, length, section) return max_length[0] if __name__ == '__main__': import utility.start as start start.time_functions(p26)
LOL (Micket): "I think i few people missed the fact that 1*x didn't have to contain the same numbers (althought it did happen to have)" """ def p52(): #ALL THESE IFs COULD HAVE BEEN REPLACED WITH A LOOP!! for x in range(142857, 10000000): x2 = 2 * x if sorted(list(str(x))) == sorted(list(str(x2))): #return x x3 = 3 * x if sorted(list(str(x))) == sorted(list(str(x3))): #return x x4 = 4 * x if sorted(list(str(x))) == sorted(list(str(x4))): #return x x5 = 5 * x if sorted(list(str(x))) == sorted(list(str(x5))): #return x x6 = 6 * x if sorted(list(str(x))) == sorted(list(str(x6))): return x if __name__ == '__main__': import utility.start as start start.time_functions(p52)
import itertools import utility.start as start def p32(): valid_products = set() for k in (1, 2): base = '123456789' for m1 in itertools.permutations(base, k): remaining = base for l in m1: remaining = remaining.replace(l, '') m1 = int(''.join(m1)) for m2 in itertools.permutations(remaining, 5-k): remaining2 = remaining for l in m2: remaining2 = remaining2.replace(l, '') m2 = int(''.join(m2)) p = m1 * m2 if sorted(str(p)) == list(remaining2): valid_products.add(p) return sum(valid_products) if __name__ == '__main__': start.time_functions(p32)
remaining_value = value - first_coin if remaining_value < 0: break if remaining_value != 0: new_max_denom = min(max_denom, remaining_value, first_coin) for new_max_denom in [c for c in ctable if c <= new_max_denom]: new_possibles = form(remaining_value, new_max_denom) for new_possible in new_possibles: addto = [first_coin] addto.extend(new_possible) addto.sort(reverse=True) possibles.append(addto) else: possibles.append([first_coin]) possibles = remove_dups(possibles) return possibles def p31(): possibilities = form(200, 200) #for p in possibilities: # print(p) print(form.cache_info()) return len(possibilities) if __name__ == '__main__': start.time_functions(p31)
t += 1 t_num = integers.get_triangle_number(t) if t_num == p_num: return (t_num, t, p37a, h) elif t_num > p_num: break elif p_num > h_num: break for n, t_num in enumerate(integers.get_triangle_numbers()): x = 166 #x=165 is the first one p_num = get_pentagonal_number(x) while p_num < t_num: x += 1 p_num = get_pentagonal_number(x) if p_num == t_num: y = 144 #y=143 is the first one h_num = get_hexagonal_number(y) while h_num < t_num: y += 1 h_num = get_hexagonal_number(y) if h_num == t_num: return (t_num, n + 1, x, y) #n starts from 0 if __name__ == '__main__': start.time_functions(p45)
except IndexError: consider_nexts.append((pair[0], pair[1])) diffs_list.append(diff) add_next_gen() while True: smallest_diff = min(diffs_list) index_smallest = diffs_list.index(smallest_diff) pair = consider_nexts[index_smallest] # If smallest_diff is in last generator, add in next generator if index_smallest == len(diffs_list) - 1: add_next_gen() update(index_smallest) yield pair[0], pair[1], smallest_diff def p44(): pentagonals = generic.Sliceable(integers.get_pentagonals(), generic.Sliceable.less_than_last) for p1, p2, sub_dif in get_smallest_diffs(pentagonals): if sub_dif in pentagonals and p1 + p2 in pentagonals: return sub_dif if __name__ == '__main__': start.time_functions(p44)
elif is_three_kind(): return (4, is_three_kind()) elif is_two_pairs(): return [3] + is_two_pairs() def is_player1_winner(hand1, hand2): '''Return if Player 1 wins (player 1 is hand1)''' s1, s2 = (score_hand(hand1), score_hand(hand2)) for i in range(len(s1)): if s1[i] > s2[i]: return True elif s1[i] < s2[i]: return False raise Exception("They are all equal!") def p54(): player1_hands, player2_hands = parse_datafile() player1_wins = [ is_player1_winner(player1_hands[i], player2_hands[i]) for i in range(len(player1_hands)) ] return player1_wins.count(True) if __name__ == '__main__': start.time_functions(p54)
"j": 10, "k": 11, "l": 12, "m": 13, "n": 14, "o": 15, "p": 16, "q": 17, "r": 18, "s": 19, "t": 20, "u": 21, "v": 22, "w": 23, "x": 24, "y": 25, "z": 26 } value = sum(letter_values[char] for char in word.lower()) return value word_sums = [letter_sum(word) for word in words] num_triangles = sum(1 for word_sum in word_sums if integers.is_triangle_number(word_sum)) return num_triangles if __name__ == '__main__': start.time_functions(p42)
''' https://projecteuler.net/problem=47 ''' import math from utility import start, generic, primes2 def p47(): pass if __name__ == '__main__': start.time_functions(p47)