sums[x+y] = 0 return sum(sums) def is_abundant(num): """ determines if a number is abundant, where the sum of its proper divisors exceeds its own value num (int): some number returns (bool): True if abundant, False otherwise """ return sum_of_proper_divisors(num) > num def sum_of_proper_divisors(num): """ determines the sum of the proper divisors of some number e.g. proper divisors of 284 are 1, 2, 4, 71, 142 ==> 220 num (int): number > 1 returns (int): sum """ assert num >= 1 if num == 2: return 1 else: return sum(get_divisors(num)[:-1]) if __name__ == "__main__": euler_test(23, problem23())
""" 10001st prime Problem 7 By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10,001st prime number? """ from euler import euler_test, prime # function prime() moved to euler module if __name__ == "__main__": euler_test(7, prime(10001))
""" Factorial digit sum Problem 20 n! means n x (n - 1) x ... x 3 x 2 x 1 For example, 10! = 10 x 9 x ... x 3 x 2 x 1 = 3628800, and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. Find the sum of the digits in the number 100! """ from euler import euler_test from math import factorial def factorial_digit_sum(num): """ sums the digits of the factorial of some number num (int): integer > 0 returns (int): solution """ return sum([int(char) for char in str(factorial(num)).replace("L", "")]) if __name__ == "__main__": euler_test(20, factorial_digit_sum(100))
What is the value of the first triangle number to have over five hundred divisors? from euler import euler_test, Stub, get_divisors """ from euler import euler_test, get_divisors def highly_divisible_triangular_number(min_divisors): """ finds a triangle number that number of divisors exceeds 'min_divisors' min_divisors (int): limit, inclusive returns (int): triangle number """ for tri_number in gen_triangular_number(): if len(get_divisors(tri_number)) > min_divisors: return tri_number def gen_triangular_number(): """ generates triangular numbers, e.g. 28 = 7+6+5+4+3+2+1 yields (int): triangular number """ i = 1 while True: yield sum(range(i+1)) i += 1 if __name__ == "__main__": euler_test(12, highly_divisible_triangular_number(500))
""" Power digit sum Problem 16 2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 2^1000? """ from euler import euler_test def power_digit_sum(exponent): """ finds the sum of all digits in 2^exponent """ return sum([int(char) for char in str(2**exponent)]) if __name__ == "__main__": euler_test(16, power_digit_sum(1000))
def subset_test ( ): #*****************************************************************************80 # ## SUBSET_TEST tests the SUBSET library. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 31 May 2015 # # Author: # # John Burkardt # from agm_values import agm_values_test from asm_enum import asm_enum_test from asm_triangle import asm_triangle_test from bell import bell_test from bell_values import bell_values_test from catalan import catalan_test from catalan_row_next import catalan_row_next_test from catalan_values import catalan_values_test from cfrac_to_rat import cfrac_to_rat_test from change_greedy import change_greedy_test from change_next import change_next_test from chinese_check import chinese_check_test from chinese_to_i4 import chinese_to_i4_test from comb_next import comb_next_test from comb_row_next import comb_row_next_test from comb_unrank import comb_unrank_test from comp_enum import comp_enum_test from comp_next import comp_next_test from comp_next_grlex import comp_next_grlex_test from comp_random_grlex import comp_random_grlex_test from comp_rank_grlex import comp_rank_grlex_test from comp_to_ksub import comp_to_ksub_test from comp_unrank_grlex import comp_unrank_grlex_test from compnz_enum import compnz_enum_test from compnz_next import compnz_next_test from compnz_to_ksub import compnz_to_ksub_test from compnz_random import compnz_random_test from congruence import congruence_test from count_pose_random import count_pose_random_test from derange_enum import derange_enum_test from derange_enum2 import derange_enum2_test from derange_enum3 import derange_enum3_test from digraph_arc_print import digraph_arc_print_test from dvec_add import dvec_add_test from dvec_complementx import dvec_complementx_test from dvec_mul import dvec_mul_test from dvec_print import dvec_print_test from dvec_sub import dvec_sub_test from dvec_to_i4 import dvec_to_i4_test from equiv_print import equiv_print_test from equiv_print2 import equiv_print2_test from equiv_random import equiv_random_test from euler import euler_test from frobenius_number_order2 import frobenius_number_order2_test from frobenius_number_order2_values import frobenius_number_order2_values_test from gamma_values import gamma_values_test from gamma_log_values import gamma_log_values_test from gray_rank2 import gray_rank2_test from gray_unrank2 import gray_unrank2_test from i4_choose import i4_choose_test from i4_factorial import i4_factorial_test from i4_factorial_values import i4_factorial_values_test from i4_fall import i4_fall_test from i4_gcd import i4_gcd_test from i4_huge import i4_huge_test from i4_log_10 import i4_log_10_test from i4_modp import i4_modp_test from i4_partition_conj import i4_partition_conj_test from i4_partition_count import i4_partition_count_test from i4_partition_count2 import i4_partition_count2_test from i4_partition_count_values import i4_partition_count_values_test from i4_partition_print import i4_partition_print_test from i4_rise import i4_rise_test from i4_sign import i4_sign_test from i4_sqrt import i4_sqrt_test from i4_sqrt_cf import i4_sqrt_cf_test from i4_to_chinese import i4_to_chinese_test from i4_to_dvec import i4_to_dvec_test from i4_to_i4poly import i4_to_i4poly_test from i4_to_van_der_corput import i4_to_van_der_corput_test from i4_uniform_ab import i4_uniform_ab_test from i4mat_mm import i4mat_mm_test from i4mat_print import i4mat_print_test from i4mat_print_some import i4mat_print_some_test from i4mat_u1_inverse import i4mat_u1_inverse_test from i4poly_add import i4poly_add_test from i4poly_degree import i4poly_degree_test from i4poly_dif import i4poly_dif_test from i4poly_mul import i4poly_mul_test from i4poly_print import i4poly_print_test from i4poly_to_i4 import i4poly_to_i4_test from i4vec_ascends import i4vec_ascends_test from i4vec_descends import i4vec_descends_test from i4vec_frac import i4vec_frac_test from i4vec_index import i4vec_index_test from i4vec_indicator0 import i4vec_indicator0_test from i4vec_indicator1 import i4vec_indicator1_test from i4vec_max_index_last import i4vec_max_index_last_test from i4vec_pairwise_prime import i4vec_pairwise_prime_test from i4vec_print import i4vec_print_test from i4vec_product import i4vec_product_test from i4vec_reverse import i4vec_reverse_test from i4vec_sort_bubble_a import i4vec_sort_bubble_a_test from i4vec_sort_heap_index_d import i4vec_sort_heap_index_d_test from i4vec_sum import i4vec_sum_test from i4vec_transpose_print import i4vec_transpose_print_test from i4vec_uniform_ab import i4vec_uniform_ab_test from index_box_next_2d import index_box_next_2d_test from index_box_next_3d import index_box_next_3d_test from inverse_mod_n import inverse_mod_n_test from involute_enum import involute_enum_test from josephus import josephus_test from ksub_random import ksub_random_test from ksub_random2 import ksub_random2_test from ksub_random3 import ksub_random3_test from ksub_random4 import ksub_random4_test from ksub_random5 import ksub_random5_test from ksub_rank import ksub_rank_test from ksub_to_comp import ksub_to_comp_test from ksub_to_compnz import ksub_to_compnz_test from ksub_unrank import ksub_unrank_test from lvec_next import lvec_next_test from moebius_values import moebius_values_test from monomial_count import monomial_count_test from monomial_counts import monomial_counts_test from multinomial_coef1 import multinomial_coef1_test from multinomial_coef2 import multinomial_coef2_test from multiperm_enum import multiperm_enum_test from nim_sum import nim_sum_test from padovan import padovan_test from pent_enum import pent_enum_test from perm_fixed_enum import perm_fixed_enum_test from perm_print import perm_print_test from perm0_check import perm0_check_test from perm0_lex_next import perm0_lex_next_test from perm0_random import perm0_random_test from perm0_random2 import perm0_random2_test from perm0_sign import perm0_sign_test from perm1_check import perm1_check_test from perrin import perrin_test from pord_check import pord_check_test from power_mod import power_mod_test from prime import prime_test from pythag_triple_next import pythag_triple_next_test from r8_agm import r8_agm_test from r8_choose import r8_choose_test from r8_fall import r8_fall_test from r8_fall_values import r8_fall_values_test from r8_gamma import r8_gamma_test from r8_rise import r8_rise_test from r8_rise_values import r8_rise_values_test from r8_to_dec import r8_to_dec_test from r8_uniform_01 import r8_uniform_01_test from r8_uniform_ab import r8_uniform_ab_test from r8mat_print import r8mat_print_test from r8mat_print_some import r8mat_print_some_test from r8poly_add import r8poly_add_test from r8poly_degree import r8poly_degree_test from r8poly_dif import r8poly_dif_test from r8poly_div import r8poly_div_test from r8poly_mul import r8poly_mul_test from r8poly_print import r8poly_print_test from r8vec_frac import r8vec_frac_test from r8vec_indicator1 import r8vec_indicator1_test from r8vec_mirror_next import r8vec_mirror_next_test from r8vec_print import r8vec_print_test from r8vec_uniform_01 import r8vec_uniform_01_test from r8vec_uniform_ab import r8vec_uniform_ab_test from rat_to_cfrac import rat_to_cfrac_test from regro_next import regro_next_test from schroeder import schroeder_test from subset_gray_rank import subset_gray_rank_test from subset_gray_unrank import subset_gray_unrank_test from subset_lex_next import subset_lex_next_test from subset_random import subset_random_test from subtriangle_next import subtriangle_next_test from thue_binary_next import thue_binary_next_test from thue_ternary_next import thue_ternary_next_test from timestamp import timestamp_test from tuple_next import tuple_next_test from tuple_next2 import tuple_next2_test from tuple_next_ge import tuple_next_ge_test from ubvec_add import ubvec_add_test from ubvec_print import ubvec_print_test from ubvec_to_ui4 import ubvec_to_ui4_test from ubvec_xor import ubvec_xor_test from ui4_to_ubvec import ui4_to_ubvec_test from vec_colex_next import vec_colex_next_test from vec_colex_next2 import vec_colex_next2_test from vec_colex_next3 import vec_colex_next3_test from vec_gray_next import vec_gray_next_test from vec_gray_rank import vec_gray_rank_test from vec_gray_unrank import vec_gray_unrank_test from vec_lex_next import vec_lex_next_test from vec_random import vec_random_test from vector_constrained_next import vector_constrained_next_test from vector_constrained_next2 import vector_constrained_next2_test from vector_constrained_next3 import vector_constrained_next3_test from vector_next import vector_next_test from ytb_enum import ytb_enum_test from ytb_next import ytb_next_test from ytb_print import ytb_print_test from ytb_random import ytb_random_test print '' print 'SUBSET_TEST' print ' Python version:' print ' Test the SUBSET library.' agm_values_test ( ) asm_enum_test ( ) asm_triangle_test ( ) bell_test ( ) bell_values_test ( ) catalan_test ( ) catalan_row_next_test ( ) catalan_values_test ( ) cfrac_to_rat_test ( ) change_greedy_test ( ) change_next_test ( ) chinese_check_test ( ) chinese_to_i4_test ( ) comb_next_test ( ) comb_row_next_test ( ) comb_unrank_test ( ) comp_enum_test ( ) comp_next_test ( ) comp_next_grlex_test ( ) comp_random_grlex_test ( ) comp_rank_grlex_test ( ) comp_to_ksub_test ( ) comp_unrank_grlex_test ( ) compnz_enum_test ( ) compnz_next_test ( ) compnz_random_test ( ) compnz_to_ksub_test ( ) congruence_test ( ) count_pose_random_test ( ) derange_enum_test ( ) derange_enum2_test ( ) derange_enum3_test ( ) digraph_arc_print_test ( ) dvec_add_test ( ) dvec_complementx_test ( ) dvec_mul_test ( ) dvec_print_test ( ) dvec_sub_test ( ) dvec_to_i4_test ( ) equiv_print_test ( ) equiv_print2_test ( ) equiv_random_test ( ) euler_test ( ) frobenius_number_order2_test ( ) frobenius_number_order2_values_test ( ) gamma_values_test ( ) gamma_log_values_test ( ) gray_rank2_test ( ) gray_unrank2_test ( ) i4_choose_test ( ) i4_factorial_test ( ) i4_factorial_values_test ( ) i4_fall_test ( ) i4_gcd_test ( ) i4_huge_test ( ) i4_log_10_test ( ) i4_modp_test ( ) i4_partition_conj_test ( ) i4_partition_count_test ( ) i4_partition_count2_test ( ) i4_partition_count_values_test ( ) i4_partition_print_test ( ) i4_rise_test ( ) i4_sign_test ( ) i4_sqrt_test ( ) i4_sqrt_cf_test ( ) i4_to_chinese_test ( ) i4_to_dvec_test ( ) i4_to_i4poly_test ( ) i4_to_van_der_corput_test ( ) i4_uniform_ab_test ( ) i4mat_mm_test i4mat_print_test ( ) i4mat_print_some_test ( ) i4mat_u1_inverse_test ( ) i4poly_add_test ( ) i4poly_degree_test ( ) i4poly_dif_test ( ) i4poly_mul_test ( ) i4poly_print_test ( ) i4poly_to_i4_test ( ) i4vec_ascends_test ( ) i4vec_descends_test ( ) i4vec_frac_test ( ) i4vec_index_test ( ) i4vec_indicator0_test ( ) i4vec_indicator1_test ( ) i4vec_max_index_last_test ( ) i4vec_pairwise_prime_test ( ) i4vec_print_test ( ) i4vec_product_test ( ) i4vec_reverse_test ( ) i4vec_sort_bubble_a_test ( ) i4vec_sort_heap_index_d_test ( ) i4vec_sum_test ( ) i4vec_transpose_print_test ( ) i4vec_uniform_ab_test ( ) index_box_next_2d_test ( ) index_box_next_3d_test ( ) inverse_mod_n_test ( ) involute_enum_test ( ) josephus_test ( ) ksub_random_test ( ) ksub_random2_test ( ) ksub_random3_test ( ) ksub_random4_test ( ) ksub_random5_test ( ) ksub_rank_test ( ) ksub_to_comp_test ( ) ksub_to_compnz_test ( ) ksub_unrank_test ( ) lvec_next_test ( ) moebius_values_test ( ) monomial_count_test ( ) monomial_counts_test ( ) multinomial_coef1_test ( ) multinomial_coef2_test ( ) multiperm_enum_test ( ) nim_sum_test ( ) padovan_test ( ) pent_enum_test ( ) perm_fixed_enum_test ( ) perm_print_test ( ) perm0_check_test ( ) perm0_lex_next_test ( ) perm0_random_test ( ) perm0_random2_test ( ) perm0_sign_test ( ) perm1_check_test ( ) perrin_test ( ) pord_check_test ( ) power_mod_test ( ) prime_test ( ) pythag_triple_next_test ( ) r8_agm_test ( ) r8_choose_test ( ) r8_fall_test ( ) r8_fall_values_test ( ) r8_gamma_test ( ) r8_rise_test ( ) r8_rise_values_test ( ) r8_to_dec_test ( ) r8_uniform_01_test ( ) r8_uniform_ab_test ( ) r8mat_print_test ( ) r8mat_print_some_test ( ) r8poly_add_test ( ) r8poly_degree_test ( ) r8poly_dif_test ( ) r8poly_div_test ( ) r8poly_mul_test ( ) r8poly_print_test ( ) r8vec_frac_test ( ) r8vec_indicator1_test ( ) r8vec_mirror_next_test ( ) r8vec_print_test ( ) r8vec_uniform_01_test ( ) r8vec_uniform_ab_test ( ) rat_to_cfrac_test ( ) regro_next_test ( ) schroeder_test ( ) subset_gray_rank_test ( ) subset_gray_unrank_test ( ) subset_lex_next_test ( ) subset_random_test ( ) subtriangle_next_test ( ) thue_binary_next_test ( ) thue_ternary_next_test ( ) tuple_next_test ( ) tuple_next2_test ( ) tuple_next_ge_test ( ) timestamp_test ( ) tuple_next_test ( ) tuple_next2_test ( ) ubvec_add_test ( ) ubvec_print_test ( ) ubvec_to_ui4_test ( ) ubvec_xor_test ( ) ui4_to_ubvec_test ( ) vec_colex_next_test ( ) vec_colex_next2_test ( ) vec_colex_next3_test ( ) vec_gray_next_test ( ) vec_gray_rank_test ( ) vec_gray_unrank_test ( ) vec_lex_next_test ( ) vec_random_test ( ) vector_constrained_next_test ( ) vector_constrained_next2_test ( ) vector_constrained_next3_test ( ) vector_next_test ( ) ytb_enum_test ( ) ytb_next_test ( ) ytb_print_test ( ) ytb_random_test ( ) # # Terminate. # print '' print 'SUBSET_TEST:' print ' Normal end of execution.' print '' return
""" Summation of primes Problem 10 The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. """ from euler import euler_test, Stub, gen_primes def summation_of_primes(limit): """ adds all the primes up to some limit limit (int): limit, exclusive returns (int): result """ stub = Stub() stub.start() result = 0 for prime in gen_primes(limit): result += prime return result if __name__ == "__main__": euler_test(10, summation_of_primes(2*10**6))
""" Multiples of 3 and 5 Problem 1 If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. """ from euler import euler_test def multiples_of_3_and_5(limit): """ finds the sum of multiples of 3 and 5 up to some limit limit (int): limit, exclusive returns (int): sum of all multiples """ multiples = [] for num in range(limit): if (num % 3 == 0) or (num % 5 == 0): multiples.append(num) result = sum(multiples) return result if __name__ == "__main__": euler_test(1, multiples_of_3_and_5(1000))
def subset_test(): #*****************************************************************************80 # ## SUBSET_TEST tests the SUBSET library. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 31 May 2015 # # Author: # # John Burkardt # from agm_values import agm_values_test from asm_enum import asm_enum_test from asm_triangle import asm_triangle_test from bell import bell_test from bell_values import bell_values_test from catalan import catalan_test from catalan_row_next import catalan_row_next_test from catalan_values import catalan_values_test from cfrac_to_rat import cfrac_to_rat_test from change_greedy import change_greedy_test from change_next import change_next_test from chinese_check import chinese_check_test from chinese_to_i4 import chinese_to_i4_test from comb_next import comb_next_test from comb_row_next import comb_row_next_test from comb_unrank import comb_unrank_test from comp_enum import comp_enum_test from comp_next import comp_next_test from comp_next_grlex import comp_next_grlex_test from comp_random_grlex import comp_random_grlex_test from comp_rank_grlex import comp_rank_grlex_test from comp_to_ksub import comp_to_ksub_test from comp_unrank_grlex import comp_unrank_grlex_test from compnz_enum import compnz_enum_test from compnz_next import compnz_next_test from compnz_to_ksub import compnz_to_ksub_test from compnz_random import compnz_random_test from congruence import congruence_test from count_pose_random import count_pose_random_test from derange_enum import derange_enum_test from derange_enum2 import derange_enum2_test from derange_enum3 import derange_enum3_test from digraph_arc_print import digraph_arc_print_test from dvec_add import dvec_add_test from dvec_complementx import dvec_complementx_test from dvec_mul import dvec_mul_test from dvec_print import dvec_print_test from dvec_sub import dvec_sub_test from dvec_to_i4 import dvec_to_i4_test from equiv_print import equiv_print_test from equiv_print2 import equiv_print2_test from equiv_random import equiv_random_test from euler import euler_test from frobenius_number_order2 import frobenius_number_order2_test from frobenius_number_order2_values import frobenius_number_order2_values_test from gamma_values import gamma_values_test from gamma_log_values import gamma_log_values_test from gray_rank2 import gray_rank2_test from gray_unrank2 import gray_unrank2_test from i4_choose import i4_choose_test from i4_factorial import i4_factorial_test from i4_factorial_values import i4_factorial_values_test from i4_fall import i4_fall_test from i4_gcd import i4_gcd_test from i4_huge import i4_huge_test from i4_log_10 import i4_log_10_test from i4_modp import i4_modp_test from i4_partition_conj import i4_partition_conj_test from i4_partition_count import i4_partition_count_test from i4_partition_count2 import i4_partition_count2_test from i4_partition_count_values import i4_partition_count_values_test from i4_partition_print import i4_partition_print_test from i4_rise import i4_rise_test from i4_sign import i4_sign_test from i4_sqrt import i4_sqrt_test from i4_sqrt_cf import i4_sqrt_cf_test from i4_to_chinese import i4_to_chinese_test from i4_to_dvec import i4_to_dvec_test from i4_to_i4poly import i4_to_i4poly_test from i4_to_van_der_corput import i4_to_van_der_corput_test from i4_uniform_ab import i4_uniform_ab_test from i4mat_mm import i4mat_mm_test from i4mat_print import i4mat_print_test from i4mat_print_some import i4mat_print_some_test from i4mat_u1_inverse import i4mat_u1_inverse_test from i4poly_add import i4poly_add_test from i4poly_degree import i4poly_degree_test from i4poly_dif import i4poly_dif_test from i4poly_mul import i4poly_mul_test from i4poly_print import i4poly_print_test from i4poly_to_i4 import i4poly_to_i4_test from i4vec_ascends import i4vec_ascends_test from i4vec_descends import i4vec_descends_test from i4vec_frac import i4vec_frac_test from i4vec_index import i4vec_index_test from i4vec_indicator0 import i4vec_indicator0_test from i4vec_indicator1 import i4vec_indicator1_test from i4vec_max_index_last import i4vec_max_index_last_test from i4vec_pairwise_prime import i4vec_pairwise_prime_test from i4vec_print import i4vec_print_test from i4vec_product import i4vec_product_test from i4vec_reverse import i4vec_reverse_test from i4vec_sort_bubble_a import i4vec_sort_bubble_a_test from i4vec_sort_heap_index_d import i4vec_sort_heap_index_d_test from i4vec_sum import i4vec_sum_test from i4vec_transpose_print import i4vec_transpose_print_test from i4vec_uniform_ab import i4vec_uniform_ab_test from index_box_next_2d import index_box_next_2d_test from index_box_next_3d import index_box_next_3d_test from inverse_mod_n import inverse_mod_n_test from involute_enum import involute_enum_test from josephus import josephus_test from ksub_random import ksub_random_test from ksub_random2 import ksub_random2_test from ksub_random3 import ksub_random3_test from ksub_random4 import ksub_random4_test from ksub_random5 import ksub_random5_test from ksub_rank import ksub_rank_test from ksub_to_comp import ksub_to_comp_test from ksub_to_compnz import ksub_to_compnz_test from ksub_unrank import ksub_unrank_test from lvec_next import lvec_next_test from moebius_values import moebius_values_test from monomial_count import monomial_count_test from monomial_counts import monomial_counts_test from multinomial_coef1 import multinomial_coef1_test from multinomial_coef2 import multinomial_coef2_test from multiperm_enum import multiperm_enum_test from nim_sum import nim_sum_test from padovan import padovan_test from pent_enum import pent_enum_test from perm_fixed_enum import perm_fixed_enum_test from perm_print import perm_print_test from perm0_check import perm0_check_test from perm0_lex_next import perm0_lex_next_test from perm0_random import perm0_random_test from perm0_random2 import perm0_random2_test from perm0_sign import perm0_sign_test from perm1_check import perm1_check_test from perrin import perrin_test from pord_check import pord_check_test from power_mod import power_mod_test from prime import prime_test from pythag_triple_next import pythag_triple_next_test from r8_agm import r8_agm_test from r8_choose import r8_choose_test from r8_fall import r8_fall_test from r8_fall_values import r8_fall_values_test from r8_gamma import r8_gamma_test from r8_rise import r8_rise_test from r8_rise_values import r8_rise_values_test from r8_to_dec import r8_to_dec_test from r8_uniform_01 import r8_uniform_01_test from r8_uniform_ab import r8_uniform_ab_test from r8mat_print import r8mat_print_test from r8mat_print_some import r8mat_print_some_test from r8poly_add import r8poly_add_test from r8poly_degree import r8poly_degree_test from r8poly_dif import r8poly_dif_test from r8poly_div import r8poly_div_test from r8poly_mul import r8poly_mul_test from r8poly_print import r8poly_print_test from r8vec_frac import r8vec_frac_test from r8vec_indicator1 import r8vec_indicator1_test from r8vec_mirror_next import r8vec_mirror_next_test from r8vec_print import r8vec_print_test from r8vec_uniform_01 import r8vec_uniform_01_test from r8vec_uniform_ab import r8vec_uniform_ab_test from rat_to_cfrac import rat_to_cfrac_test from regro_next import regro_next_test from schroeder import schroeder_test from subset_gray_rank import subset_gray_rank_test from subset_gray_unrank import subset_gray_unrank_test from subset_lex_next import subset_lex_next_test from subset_random import subset_random_test from subtriangle_next import subtriangle_next_test from thue_binary_next import thue_binary_next_test from thue_ternary_next import thue_ternary_next_test from timestamp import timestamp_test from tuple_next import tuple_next_test from tuple_next2 import tuple_next2_test from tuple_next_ge import tuple_next_ge_test from ubvec_add import ubvec_add_test from ubvec_print import ubvec_print_test from ubvec_to_ui4 import ubvec_to_ui4_test from ubvec_xor import ubvec_xor_test from ui4_to_ubvec import ui4_to_ubvec_test from vec_colex_next import vec_colex_next_test from vec_colex_next2 import vec_colex_next2_test from vec_colex_next3 import vec_colex_next3_test from vec_gray_next import vec_gray_next_test from vec_gray_rank import vec_gray_rank_test from vec_gray_unrank import vec_gray_unrank_test from vec_lex_next import vec_lex_next_test from vec_random import vec_random_test from vector_constrained_next import vector_constrained_next_test from vector_constrained_next2 import vector_constrained_next2_test from vector_constrained_next3 import vector_constrained_next3_test from vector_next import vector_next_test from ytb_enum import ytb_enum_test from ytb_next import ytb_next_test from ytb_print import ytb_print_test from ytb_random import ytb_random_test print '' print 'SUBSET_TEST' print ' Python version:' print ' Test the SUBSET library.' agm_values_test() asm_enum_test() asm_triangle_test() bell_test() bell_values_test() catalan_test() catalan_row_next_test() catalan_values_test() cfrac_to_rat_test() change_greedy_test() change_next_test() chinese_check_test() chinese_to_i4_test() comb_next_test() comb_row_next_test() comb_unrank_test() comp_enum_test() comp_next_test() comp_next_grlex_test() comp_random_grlex_test() comp_rank_grlex_test() comp_to_ksub_test() comp_unrank_grlex_test() compnz_enum_test() compnz_next_test() compnz_random_test() compnz_to_ksub_test() congruence_test() count_pose_random_test() derange_enum_test() derange_enum2_test() derange_enum3_test() digraph_arc_print_test() dvec_add_test() dvec_complementx_test() dvec_mul_test() dvec_print_test() dvec_sub_test() dvec_to_i4_test() equiv_print_test() equiv_print2_test() equiv_random_test() euler_test() frobenius_number_order2_test() frobenius_number_order2_values_test() gamma_values_test() gamma_log_values_test() gray_rank2_test() gray_unrank2_test() i4_choose_test() i4_factorial_test() i4_factorial_values_test() i4_fall_test() i4_gcd_test() i4_huge_test() i4_log_10_test() i4_modp_test() i4_partition_conj_test() i4_partition_count_test() i4_partition_count2_test() i4_partition_count_values_test() i4_partition_print_test() i4_rise_test() i4_sign_test() i4_sqrt_test() i4_sqrt_cf_test() i4_to_chinese_test() i4_to_dvec_test() i4_to_i4poly_test() i4_to_van_der_corput_test() i4_uniform_ab_test() i4mat_mm_test i4mat_print_test() i4mat_print_some_test() i4mat_u1_inverse_test() i4poly_add_test() i4poly_degree_test() i4poly_dif_test() i4poly_mul_test() i4poly_print_test() i4poly_to_i4_test() i4vec_ascends_test() i4vec_descends_test() i4vec_frac_test() i4vec_index_test() i4vec_indicator0_test() i4vec_indicator1_test() i4vec_max_index_last_test() i4vec_pairwise_prime_test() i4vec_print_test() i4vec_product_test() i4vec_reverse_test() i4vec_sort_bubble_a_test() i4vec_sort_heap_index_d_test() i4vec_sum_test() i4vec_transpose_print_test() i4vec_uniform_ab_test() index_box_next_2d_test() index_box_next_3d_test() inverse_mod_n_test() involute_enum_test() josephus_test() ksub_random_test() ksub_random2_test() ksub_random3_test() ksub_random4_test() ksub_random5_test() ksub_rank_test() ksub_to_comp_test() ksub_to_compnz_test() ksub_unrank_test() lvec_next_test() moebius_values_test() monomial_count_test() monomial_counts_test() multinomial_coef1_test() multinomial_coef2_test() multiperm_enum_test() nim_sum_test() padovan_test() pent_enum_test() perm_fixed_enum_test() perm_print_test() perm0_check_test() perm0_lex_next_test() perm0_random_test() perm0_random2_test() perm0_sign_test() perm1_check_test() perrin_test() pord_check_test() power_mod_test() prime_test() pythag_triple_next_test() r8_agm_test() r8_choose_test() r8_fall_test() r8_fall_values_test() r8_gamma_test() r8_rise_test() r8_rise_values_test() r8_to_dec_test() r8_uniform_01_test() r8_uniform_ab_test() r8mat_print_test() r8mat_print_some_test() r8poly_add_test() r8poly_degree_test() r8poly_dif_test() r8poly_div_test() r8poly_mul_test() r8poly_print_test() r8vec_frac_test() r8vec_indicator1_test() r8vec_mirror_next_test() r8vec_print_test() r8vec_uniform_01_test() r8vec_uniform_ab_test() rat_to_cfrac_test() regro_next_test() schroeder_test() subset_gray_rank_test() subset_gray_unrank_test() subset_lex_next_test() subset_random_test() subtriangle_next_test() thue_binary_next_test() thue_ternary_next_test() tuple_next_test() tuple_next2_test() tuple_next_ge_test() timestamp_test() tuple_next_test() tuple_next2_test() ubvec_add_test() ubvec_print_test() ubvec_to_ui4_test() ubvec_xor_test() ui4_to_ubvec_test() vec_colex_next_test() vec_colex_next2_test() vec_colex_next3_test() vec_gray_next_test() vec_gray_rank_test() vec_gray_unrank_test() vec_lex_next_test() vec_random_test() vector_constrained_next_test() vector_constrained_next2_test() vector_constrained_next3_test() vector_next_test() ytb_enum_test() ytb_next_test() ytb_print_test() ytb_random_test() # # Terminate. # print '' print 'SUBSET_TEST:' print ' Normal end of execution.' print '' return
return sum(stack) def sum_of_proper_divisors(num): """ determines the sum of the proper divisors of some number e.g. proper divisors of 284 are 1, 2, 4, 71, 142 ==> 220 num (int): number > 1 returns (int): sum """ divisors = get_divisors(num) divisors.remove(num) return sum(divisors) def is_amicable(num): """ determines if a number is amicable num (int): number returns (bool, int): True if amicable, False otherwise """ twin = sum_of_proper_divisors(num) if num == twin: return False else: return num == sum_of_proper_divisors(twin) if __name__ == "__main__": euler_test(21, sum_of_amicable_numbers(10000))
return None def next_permutation(array): """ finds and mutates the next permutation of some array of items array (list): initial permutation returns (bool): True if has next, False if none NOTE: function mutates input array! """ i = len(array) - 1 while i > 0 and array[i - 1] >= array[i]: i -= 1 if i <= 0: return False # find successor to pivot j = len(array) - 1 while array[j] <= array[i - 1]: j -= 1 array[i - 1], array[j] = array[j], array[i - 1] # reverse suffix array[i : ] = array[len(array) - 1 : i - 1 : -1] return True if __name__ == "__main__": #euler_test(24, get_permutation_trivial(10**6, map(str, range(10)))) euler_test(24, get_permutation(10**6, map(str, range(10))))
""" Largest prime factor Problem 3 The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143? """ from euler import euler_test, Stub def largest_prime_factor(num): """ finds the largest prime factor of a number num (int): some number returns (int): solution """ stub = Stub() #stub.start() j = 0 for i in range(2, int(num**0.5)): j += 1 while num % i == 0: stub.msg(i, j) num //= i if num == 1 or num == i: return i if __name__ == "__main__": euler_test(3, largest_prime_factor(600851475143))
def get_collatz_sequence(num): """ finds the Collatz sequence according to the Collatz algorithm num (int): starting number > 1 returns (list): Collatz sequence """ if num < 1: raise ValueError("num must be > 1, instead got " + str(num)) elif num == 1: return [1] elif num % 2 == 0: return [num] + get_collatz_sequence(next_collatz_number(num)) else: return [num] + get_collatz_sequence(next_collatz_number(num)) def next_collatz_number(num): """ finds the next Collatz number according to the Collatz algorithm num (int): starting number returns (int): next number in Collatz sequence """ if num % 2 == 0: return num/2 else: return 3*num + 1 if __name__ == "__main__": euler_test(14, longest_collatz_sequence(10**6))
from euler import euler_test, Stub, is_palindrome def largest_palindrome_product(minimum=100, maximum=999): """ exhaustively finds the largest palindromic product in a range minimum, maximum (int): defines range, inclusive returns (int): solution """ stub = Stub() #stub.start() candidate_solution = None palindrome_products = [] candidate_factors = [None, None] for i in range(maximum, minimum-1, -1): for j in range(i, minimum-1, -1): product = i*j if is_palindrome(product): palindrome_products.append(product) if product > candidate_solution: candidate_solution = product candidate_factors = [i, j] stub.msg(candidate_factors, "factors") return candidate_solution if __name__ == "__main__": euler_test(4, largest_palindrome_product(100, 999))
What is the first term in the Fibonacci sequence to contain 1000 digits? """ from euler import euler_test, gen_fibonacci, Stub def get_fib_with_digits(num_digits): """ finds the first Fibonacci number with a certain amount of digits """ stub = Stub() stub.start() counter = 0 for num in gen_fibonacci(): if count_digits(num) >= num_digits: return counter if counter % 1000 == 0: stub.msg(count_digits(num), counter) counter += 1 def count_digits(num): """ returns the number of digits of some number num (int): a number returns (int): digits """ return len(str(num)) if __name__ == "__main__": euler_test(25, get_fib_with_digits(1000))
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 - 385 = 2640. Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. """ from euler import euler_test, Stub def sum_square_difference(limit): """ finds the difference of the square of sums and sum of squares for numbers 1 through some number limit (int): limit, inclusive returns (int): difference """ stub = Stub() #stub.start() sum_of_squares = sum([num**2 for num in range(limit+1)]) square_of_sums = sum([num for num in range(limit+1)])**2 stub.msg(sum_of_squares, "sum") stub.msg(square_of_sums, "square") return square_of_sums - sum_of_squares if __name__ == "__main__": euler_test(6, sum_square_difference(100))
x, y = triplet.pop(), triplet.pop() return x ** 2 + y ** 2 == z ** 2 def gen_triplets_that_sum_to(value): """ generator function that generates triplets that sum to value via brute force yields (list): list of integers """ for i in range(1, value - 1): for j in range(1, value - i): k = value - (i + j) yield [i, j, k] def sums_to_a_value(numbers, value): """ checks if some list of numbers sums to a value numbers (list): list of numbers value (int): value to check sum returns (boolean): True if sum is equal, False otherwise """ return sum(numbers) == value if __name__ == "__main__": euler_test(9, special_pythagorean_triplet())
calc_product = product(series[i:i+length]) if calc_product > largest: largest = calc_product return largest if __name__ == "__main__": series = "73167176531330624919225119674426574742355349194934\ 96983520312774506326239578318016984801869478851843\ 85861560789112949495459501737958331952853208805511\ 12540698747158523863050715693290963295227443043557\ 66896648950445244523161731856403098711121722383113\ 62229893423380308135336276614282806444486645238749\ 30358907296290491560440772390713810515859307960866\ 70172427121883998797908792274921901699720888093776\ 65727333001053367881220235421809751254540594752243\ 52584907711670556013604839586446706324415722155397\ 53697817977846174064955149290862569321978468622482\ 83972241375657056057490261407972968652414535100474\ 82166370484403199890008895243450658541227588666881\ 16427171479924442928230863465674813919123162824586\ 17866458359124566529476545682848912883142607690042\ 24219022671055626321111109370544217506941658960408\ 07198403850962455444362981230987879927244284909188\ 84580156166097919133875499200524063689912560717606\ 05886116467109405077541002256983155200055935729725\ 71636269561882670428252483600823257530420752963450" series = "".join(series.split()) euler_test(8, largest_product_in_a_series(series, 13))
two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. """ from euler import euler_test, gen_fibonacci, is_even def sum_of_even_fibonacci_numbers(limit): """ finds the sum of even Fibonacci numbers up to some limit, inclusive limit (int): max number returns (int): sum """ even_fibs = [] for fib in gen_fibonacci(): if fib > limit: break elif is_even(fib): even_fibs.append(fib) #print("+ " + str(fib)) else: continue return sum(even_fibs) if __name__ == "__main__": euler_test(2, sum_of_even_fibonacci_numbers(4*10**6))
if ones_num > 0 and tens_num == 0: stack.insert(0, lows[ones_num]) elif ones_num > 0 and tens_num > 1: stack.insert(0, tens_prefix[tens_num] + "-" + lows[ones_num]) elif ones_num == 0 and tens_num > 1: stack.insert(0, tens_prefix[tens_num]) # check hundreds place if huns_num > 0 and (tens_num > 0 or ones_num > 0): stack.insert(0, "and") if huns_num > 0: stack.insert(0, "hundred") stack.insert(0, lows[huns_num]) return " ".join(stack) def count_letters(phrase): """ counts the total number of letters in a string, minus whitespace and punctuation phrase (str): target phrase returns (int): count """ phrase = phrase.translate(string.maketrans("", ""), string.punctuation) phrase = "".join(phrase.split()) return len(phrase) if __name__ == "__main__": euler_test(17, number_letter_counts(1000))
triangle (list): e.g. [[3], [7, 4], [2, 4, 6], [8, 5, 9, 3]] returns (int): max path sun """ # base case peak = triangle[0][0] new_path = path + [peak] if len(triangle) == 1: stub.msg(path, peak + sum(path)) return sum(new_path) # recursion left_triangle = [row[:-1] for row in triangle[1:]] left_sum = exhaustive_max_path_sum(left_triangle, new_path) right_triangle = [row[1:] for row in triangle[1:]] right_sum = exhaustive_max_path_sum(right_triangle, new_path) return max(left_sum, right_sum) if __name__ == "__main__": stub = Stub() stub.start() triangle = [[75], [95, 64], [17, 47, 82], [18, 35, 87, 10], [20, 4, 82, 47, 65], [19, 1, 23, 75, 3, 34], [88, 2, 77, 73, 7, 63, 67], [99, 65, 4, 28, 6, 16, 70, 92], [41, 41, 26, 56, 83, 40, 80, 70, 33], [41, 48, 72, 33, 47, 32, 37, 16, 94, 29], [53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14], [70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57], [91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48], [63, 66, 4, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31], [04, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23]] euler_test(18, max_path_sum(triangle))
""" Smallest multiple Problem 5 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? """ from euler import euler_test, remove_smaller_factors, lcm def smallest_multiple(limit): """ finds the smallest multiple of numbers 1 to 'limit' limit (int): limit, inclusive returns (int): smallest multiple """ factors = remove_smaller_factors(range(1, limit+1)) return lcm(*factors) if __name__ == "__main__": euler_test(5, smallest_multiple(20))
What is the total of all the name scores in the file? """ from euler import euler_test def total_all_name_scores(filename): """ DOCSTRING """ names = open(filename).read().replace('"', '').split(",") names.sort() result = 0 for i in range(len(names)): result += (i+1) * get_alphabetical_score(names[i]) return result def get_alphabetical_score(word): """ calculates a score by summing the alphabetical values of each character word (str): some string returns (int): score """ assert word.isalpha() return sum([(ord(char)-ord('a')+1)for char in word.lower()]) if __name__ == "__main__": euler_test(22, total_all_name_scores('p022_names.txt'))
if product(sw_ne) > largest: stub.msg(sw_ne) largest = product(sw_ne) return largest if __name__ == "__main__": grid_str = "08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08\n\ 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00\n\ 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65\n\ 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91\n\ 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80\n\ 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50\n\ 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70\n\ 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21\n\ 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72\n\ 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95\n\ 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92\n\ 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57\n\ 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58\n\ 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40\n\ 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66\n\ 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69\n\ 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36\n\ 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16\n\ 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54\n\ 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48" grid_str = grid_str.replace(" ", "") grid = [map(int, row.split(" ")) for row in grid_str.split("\n")] euler_test(11, largest_product_in_a_grid(grid, 4))
start, end (date): defines range, inclusive """ sundays = 0 for year in range(start.year, end.year + 1): if year == start.year: for month in range(start.month, 12+1): sundays += sundays_landing_on_first(year, month) elif year == end.year: for month in range(1, end.month+1): sundays += sundays_landing_on_first(year, month) else: for month in range(1, 12+1): sundays += sundays_landing_on_first(year, month) return sundays def sundays_landing_on_first(year, month): """ returns if the specified month/year has a Sunday that lands on the first year, month (int): defines month and year returns (int): 1 if it lands on the first, 0 otherwise """ if calendar.weekday(year, month, 1) == 6: return 1 else: return 0 if __name__ == "__main__": euler_test(19, count_sundays(Date(1901, 1, 1), Date(2000, 12, 31)))
dimensions (list): specifies an m x n grid as [m, n] returns (int): number of paths """ # logging stub = Stub() stub.start() # base case if dimensions == [0, 0]: stub.msg(path_history, "path found") return 1 # path and history initialization paths = 0 path_history_r = path_history_d = path_history # recursion if dimensions[0] > 0: path_history_r += "r" paths += recursive_paths([dimensions[0]-1, dimensions[1]], path_history_r) if dimensions[1] > 0: path_history_d += "d" paths += recursive_paths([dimensions[0], dimensions[1]-1], path_history_d) return paths if __name__ == "__main__": test1, test2 = lattice_paths([2, 2]), lattice_paths([4, 4]) assert test1 == 6, "2x2 should be 6, got %r" % test1 assert test2 == 70, "4x4 should be 70, got %r" % test2 euler_test(15, lattice_paths([20, 20]))
32924185707147349566916674687634660915035914677504, 99518671430235219628894890102423325116913619626622, 73267460800591547471830798392868535206946944540724, 76841822524674417161514036427982273348055556214818, 97142617910342598647204516893989422179826088076852, 87783646182799346313767754307809363333018982642090, 10848802521674670883215120185883543223812876952786, 71329612474782464538636993009049310363619763878039, 62184073572399794223406235393808339651327408011116, 66627891981488087797941876876144230030984490851411, 60661826293682836764744779239180335110989069790714, 85786944089552990653640447425576083659976645795096, 66024396409905389607120198219976047599490197230297, 64913982680032973156037120041377903785566085089252, 16730939319872750275468906903707539413042652315011, 94809377245048795150954100921645863754710598436791, 78639167021187492431995700641917969777599028300699, 15368713711936614952811305876380278410754449733078, 40789923115535562561142322423255033685442488917353, 44889911501440648020369068063960672322193204149535, 41503128880339536053299340368006977710650566631954, 81234880673210146739058568557934581403627822703280, 82616570773948327592232845941706525094512325230608, 22918802058777319719839450180888072429661980811197, 77158542502016545090413245809786882778948721859617, 72107838435069186155435662884062257473692284509516, 20849603980134001723930671666823555245252804609722, 53503534226472524250874054075591789781264330331690] euler_test(13, main(large, 10))