Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Project Euler in Python

Project Euler in Python

Project Euler is a website dedicated to a series of computational problems intended to be solved with computer programs. 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. In this presentation I will share what I have learned working on Project Euler in Python.

The first problem is to calculate the sum of multiples of 3 and 5 that are less than or equal to 1000. I first solved the problem with Brute force search. However, when I measured this code on the HackerRank site, it showed that there was a speed problem. To get a more efficient solution you could also calculate the sum of the numbers less than1000 that are divisible by 3, plus the sum of the numbers less than 1000 that are divisible by 5. But as you have summed numbers divisible by 15 twice you would have to subtract the sum of the numbers divisible by 15.

Tetsuo Koyama

October 01, 2023
Tweet

Other Decks in Technology

Transcript

  1. 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
  2. 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
  3. 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))
  4. 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)
  5. 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_)
  6. 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?
  7. License Get the source of this theme and the demo

    presentation from http://github.com/famuvie/beamerthemesimple cba 15 / 16