Skip to content

My solutions to some Project Euler problems. Emphasis on elegance (where possible) and performance.

License

Notifications You must be signed in to change notification settings

tfpf/project-euler

Repository files navigation

Project Euler

Trying my hand at Project Euler as I stumble along learning Rust. I shall only add the solutions to the first hundred problems here with the intention being to showcase whatever useful data structures I build along the way. (This is permitted according to the Project Euler guidelines.) Further, I shall restrict myself to the standard library.

style lint sanity tests

To solve, say, problem 16, enter the following command.

cargo r 16

Run it without arguments to sequentially solve all problems for which I have written solutions.

cargo r

Most solutions are rather concise; the heavy lifting is done in the utils module. This highlights the intent of the code by hiding confounding implementation details. Items of particular note therein are the following.

  • is_prime: fast prime checker which combines trial division and the Miller-Rabin algorithm.
  • pow: modular exponentiation calculator, emulating the pow function of Python.
  • Long: arbitrary-precision integer type with support for addition and multiplication.
    • Long::factorial: factorial calculator.
    • Long::pow: exponentiation calculator.
  • SieveOfAtkin: fast prime-generating sieve. The sieve of Atkin is faster than the sieve of Eratosthenes.
    • SieveOfAtkin::is_prime: prime checker for numbers the sieve is generated up to.
    • SieveOfAtkin::iter: iterator over generated primes.
  • Polygonal: figurate (triangle, quadrilateral, pentagon, hexagon, …) number generator. Uses only additions and subtractions.
    • Polygonal::invert: figurate number checker.
  • PythagoreanTriplets: Pythagorean triplets generator.

No part of the code in this repository has been written by or in consultation with artificial intelligence chatbots such as (but not limited to) Bard and ChatGPT.

Problems and Solutions

Problem Solution
1 multiples_of_3_or_5.rs
2 even_fibonacci_numbers.rs
3 largest_prime_factor.rs
4 largest_palindrome_product.rs
5 smallest_multiple.rs
6 sum_square_difference.rs
7 ten_thousand_and_first_prime.rs
8 largest_product_in_a_series.rs
9 special_pythagorean_triplet.rs
10 summation_of_primes.rs
11 largest_product_in_a_grid.rs
12 highly_divisible_triangular_number.rs
13 large_sum.rs
14 longest_collatz_sequence.rs
15 lattice_paths.rs
16 power_digit_sum.rs
17 number_letter_counts.rs
18 maximum_path_sum_i.rs
19 counting_sundays.rs
20 factorial_digit_sum.rs
21 amicable_numbers.rs
22 names_scores.rs
23 non_abundant_sums.rs
24 lexicographic_permutations.rs
25 thousand_digit_fibonacci_number.rs
26 reciprocal_cycles.rs
27 quadratic_primes.rs
28 number_spiral_diagonals.rs
29 distinct_powers.rs
30 digit_fifth_powers.rs
31 coin_sums.rs
32 pandigital_products.rs
33 digit_cancelling_fractions.rs
34 digit_factorials.rs
35 circular_primes.rs
36 double_base_palindromes.rs
37 truncatable_primes.rs
38 pandigital_multiples.rs
39 integer_right_triangles.rs
40 champernownes_constant.rs
41 pandigital_prime.rs
42 coded_triangle_numbers.rs
43 sub_string_divisibility.rs
44 pentagon_numbers.rs
45 triangular_pentagonal_and_hexagonal.rs
46 goldbachs_other_conjecture.rs
47 distinct_primes_factors.rs
48 self_powers.rs
49 prime_permutations.rs
50 consecutive_prime_sum.rs
51 prime_digit_replacements.rs
52 permuted_multiples.rs
53 combinatoric_selections.rs
54 poker_hands.rs
55 lychrel_numbers.rs
56 powerful_digit_sum.rs
57 square_root_convergents.rs
58 spiral_primes.rs
59 xor_decryption.rs
61 cyclical_figurate_numbers.rs
62 cubic_permutations.rs
63 powerful_digit_counts.rs
64 odd_period_square_roots.rs
65 convergents_of_e.rs
66 diophantine_equation.rs
67 maximum_path_sum_ii.rs
68 magic_5_gon_ring.rs
69 totient_maximum.rs
71 ordered_fractions.rs
72 counting_fractions.rs
74 digit_factorial_chains.rs
75 singular_integer_right_triangles.rs
76 counting_summations.rs
77 prime_summations.rs
78 coin_partitions.rs
81 path_sum_two_ways.rs
85 counting_rectangles.rs
87 prime_power_triples.rs
92 square_digit_chains.rs
97 large_non_mersenne_prime.rs
99 largest_exponential.rs