def main() -> None: print("Problem 21") with measure_execution_time("old"): result = all_amicable_numbers_under(LIMIT) with measure_execution_time("new"): divisor_sums = create_divisor_sums(30_000) result2 = sum(all_amicable_numbers_under_fast(LIMIT, divisor_sums)) assert result == result2 print(f"The sum of all the amicable numbers under {LIMIT} is {result}")
def main(): print("Problem 9") with measure_execution_time(): a, b, c = find_pythagorean_triplet_summing_up_to(1000) print( f"The Pythagorean triplet for which a + b + c = 1000 is {a}, {b}, {c}") print(f"The product for which is {a*b*c}")
def main() -> None: print("Problem 23") with measure_execution_time(): result = sum(integers_not_sum_abundant(LIMIT)) print( f"the sum of all the positive integers which cannot be written as the sum of two abundant numbers is {result}" )
def main(): print("Problem 26") with measure_execution_time(): result = value_with_longest_recurring_cycle(LIMIT) print( f"The value of d < {LIMIT} for which 1/d contains the longest recurring cycle in its decimal fraction part is {result}" )
def main(): n = 600_851_475_143 with measure_execution_time(): prime_factors = calc_prime_factors(n) print("Problem 3") print("the largest prime factor of the number {} = {}".format( n, max(prime_factors)))
def main(): print("Problem 25") with measure_execution_time(): result = fibonacci_index(NR_DIGITS) print( f"The index of the first term in the Fibonacci sequence to contain {NR_DIGITS} digits is {result}" )
def main(): print("Problem 13") with measure_execution_time(): numbers = [int(n) for n in NUMBERS_STRING.split('\n')] digits = str(sum(numbers))[:10] print( f"The first 10 digits of the sum of 100 50-digit numbers is {digits}")
def main(): limit = 4_000_000 with measure_execution_time(): even_fibonacci = (n for n in fibonacci_sequence(limit) if is_even(n)) result = sum(even_fibonacci) print("Problem 2") print(f"The sum of all even fibonacci numbers below {limit} = {result}")
def main() -> None: print("Problem 4") digits = 3 with measure_execution_time(): result = max(largest_palindrome(digits)) print(f"The largest palindrome of the product of two {digits}-digit \ numbers is {result}")
def main(): print("Problem 27") with measure_execution_time(): result = product_of_coefficients() print( f"The product of a and b for the quadric expression with maximum number of primes is {result}" )
def main(): print("Problem 28") with measure_execution_time(): result = sum_of_spiral_diagonals(LIMIT) print( f"The sum of the numbers on the diagonals of a {LIMIT} by {LIMIT} spiral is {result}" )
def main(): print("Problem 5") limit = 20 with measure_execution_time(): result = smallest_evenly_divisible_number(limit) print( f"The smallest positive number that is evenly divisible by all of the numbers from 1 to {limit} is {result}." )
def main() -> None: print("Problem 6") limit = 1_000 with measure_execution_time(): result = square_of_sum(limit) - sum_of_squares(limit) print( f'The difference between the sum of squares and the square of the sum for the first {limit} natural numbers is {result}' )
def main(): print('Problem 22') with measure_execution_time(): with open('../resources/p022_names.txt', 'r', encoding='ascii') as file: name_list = file.readline().replace('"', '').split(',') result = sum(name_scores(name_list)) print(f'The total of all name scores in the file is {result}')
def main() -> None: print("Problem 19") with measure_execution_time(): start_year = 1901 end_year = 2001 result = sundays_on_day_of_month(1, start_year, end_year) print( f"There are {result} Sundays on the first of the month between {start_year} and {end_year}" )
def main(): print("Problem 7") with measure_execution_time(): product, adjacent_digits = adjacent_digits_with_highest_product( LIMIT, [int(s) for s in BIG_NUMBER]) string = "".join(str(s) for s in adjacent_digits) print( f"The {LIMIT} adjacent digits with highest product is {string}, the product of which is {product}." )
def main(): print("Problem 12") n = 0 divisors = 0 with measure_execution_time(): while divisors <= LIMIT: n += 1 divisors = calc_divisors(triangle_number(n)) print(f"The first triangle number with over {LIMIT} divisors is {triangle_number(n)}")
def main(): print("Problem 24") with measure_execution_time(): nth_permutation_iterator = islice(permutations(range(10)), LIMIT - 1, LIMIT) nth_permutation = list(nth_permutation_iterator)[0] result = ''.join(str(d) for d in nth_permutation) print( f"The {LIMIT}th lexicographic permutation of the digits 0 to 9 is {result}" )
def main() -> None: print("Problem 17") with measure_execution_time(): words = [ three_digit_number_written_as_word(n) for n in range(1, LIMIT) ] cleaned_words = [clean_word(word) for word in words] result = sum(len(w) for w in cleaned_words) + len("onethousand") print( f"the number of letters used to write 1 to {LIMIT} in words is {result}" )
def main(): print("Problem 14") with measure_execution_time(): longest = 0 result = 0 chain_length_dict = dict() for n in range(1_000_000): chain = length_of_chain(n, chain_length_dict) if chain > longest: longest = chain result = n
def main() -> None: print("Problem 7") with measure_execution_time(): result = n_th_prime_number(LIMIT) print(f'The {LIMIT}st prime number is {result}')
def main() -> None: print("Problem 20") with measure_execution_time(): result = sum(int(char) for char in str(math.factorial(NUMBER))) print(f"The sum of digits in the number {NUMBER}! is {result}")
divider() problem_020.main() divider() problem_021.main() divider() problem_022.main() divider() problem_023.main() divider() problem_024.main() divider() problem_025.main() divider() problem_026.main() divider() problem_027.main() divider() problem_028.main() divider() problem_029.main() divider() problem_030.main() divider() problem_067.main() divider() if __name__ == '__main__': with measure_execution_time("Time for all problems together"): main()
def main(): print("Problem 11") with measure_execution_time(): result = greatest_product_adjacent_numbers(LIMIT) print(f"the greatest product of {LIMIT} adjacent numbers in the same direction in the 20×20 grid is {result}")
def main(): print("Problem 30") with measure_execution_time(): result = sum_numbers_that_are_sum_of_fifth_powers_of_their_digits() print(f"the sum of all the numbers that can be written as the sum of fifth powers of their digits is {result}")
def main() -> None: print("Problem 67") with measure_execution_time(): result = find_longest_path_triangle(get_triangle_from_file()) print(f"The maximum total from top to bottom of the triangle is {result}")
def main(): print("Problem 10") with measure_execution_time(): result = np.sum(sieve_of_eratosthenes(LIMIT)) print(f"The sum of all primes below {LIMIT:,} is {result:,}.")
def main(): print("Problem 16") with measure_execution_time(): result = sum([int(s) for s in str(2**1000)]) print(f"the sum of the digits of the number 2^1000 is {result}")
def main(): print("Problem 15") with measure_execution_time(): # we add 1 to size as a NxN grid has N+1xN+1 intersecetions. result = nr_routes_in_grid(SIZE + 1) print(f"the number of routes through a {SIZE}x{SIZE} grid = {result}")
def main(): print("Problem 29") with measure_execution_time(): result = nr_distinct_terms(LIMIT) print(f"The number of distinct terms with a and b =< {LIMIT} is {result}")