Project Euler in Python
What I learned
Tetsuo Koyama
http://github.com/tkoyama010
October 1, 2023
1 / 16
Slide 2
Slide 2 text
About @tkoyama010 [3]
2 / 16
Slide 3
Slide 3 text
What is Project Euler?
About
Project Euler is a website dedicated to a series of computational problems intended to be
solved with computer programs.
Notice
Each problem has been designed according to a one-minute rule, which means that although
it may take several hours to design a successful algorithm with more difficult problems, an
efficient implementation will allow a solution to be obtained on a modestly powered computer
in less than one minute.
3 / 16
Slide 4
Slide 4 text
What is Project Euler?
About
Project Euler is a website dedicated to a series of computational problems intended to be
solved with computer programs.
Notice
Each problem has been designed according to a one-minute rule, which means that although
it may take several hours to design a successful algorithm with more difficult problems, an
efficient implementation will allow a solution to be obtained on a modestly powered computer
in less than one minute.
3 / 16
Slide 5
Slide 5 text
What is Project Euler? Problem 1
4 / 16
Slide 6
Slide 6 text
Answer 1: Multiples of 3 or 5
5 / 16
Brute-force search
1 # Input number n (=1000).
2 n = 1000
3
4 # Answer
5 print(sum(i for i in range(n) if i % 3 == 0 or i % 5 == 0))
Slide 7
Slide 7 text
Answer 1: Multiples of 3 or 5 [2]
6 / 16
Slide 8
Slide 8 text
Answer 1: Multiples of 3 or 5
7 / 16
Slide 9
Slide 9 text
Answer 1: Multiples of 3 or 5
8 / 16
sum3 or 5(n) = sum3(n) + sum5(n) − sum15(n)
1 # Input number n (=1000).
2 n = 1000
3
4 # Number of subdivable integers of 3, 5, and 15.
5 n_03 = (n - 1) // 3
6 n_05 = (n - 1) // 5
7 n_15 = (n - 1) // 15
8
9 # Sum of subdivable integers of 3, 5, and 15.
10 sum_03 = 3 * n_03 * (n_03 + 1) // 2
11 sum_05 = 5 * n_05 * (n_05 + 1) // 2
12 sum_15 = 15 * n_15 * (n_15 + 1) // 2
13
14 # Answer
15 print(sum_03 + sum_05 - sum_15)
Slide 10
Slide 10 text
Answer 1: Multiples of 3 or 5
9 / 16
Slide 11
Slide 11 text
Problem 2
10 / 16
Slide 12
Slide 12 text
Answer 2: Even Fibonacci Numbers
11 / 16
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 . . .
1 sum_ = 0
2 f1 = 1
3 f2 = 2
4 f3 = 3
5 while f2 < n:
6 sum_ += f2
7 for _ in range (3):
8 f1 = f2
9 f2 = f3
10 f3 = f1 + f2
11 print(sum_)
Slide 13
Slide 13 text
Problem Archives [1]
12 / 16
Slide 14
Slide 14 text
FAQ
13 / 16
1. I’ve written my program but should it take days to get to the answer?
2. Does it matter if it takes more than one minute to solve?
3. I solved it by using a search engine, does that matter?