Sunday, 24 September 2023

Solving Project Euler Problem 1: Multiples of 3 and 5

Problem

"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."

Solution

Here, we need to find and sum all the numbers below 1000 that are divisible by either 3 or 5.

To solve this problem, we can iterate through the numbers from 1 to 999 (since we want numbers below 1000) and checking if each number is divisible by either 3 or 5. If a number is divisible, we add it to a running sum. At the end of the iteration, the sum will contain the answer.

Here is a Python implementation of this:


From the code;

1.      We initialize a variable sum to zero. This variable will store the sum of multiples of 3 or 5.

2.      We use a for loop to iterate through numbers from 1 to 999.

3.      Inside the loop, we use the modulo operator (%) to check if the current number is divisible by 3 or 5. If the remainder of the division is zero, it means the number is a multiple of either 3 or 5, so we add it to the sum.

4.      After the loop completes, we print the final value of sum, which represents the sum of all multiples of 3 or 5 below the given limit.

Solving Project Euler Problems provides a great opportunity to practice coding and algorithmic thinking.

 

 

 

No comments:

Post a Comment

Solving Project Euler Problem 10: Summation of Primes

  Problem "The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million." Solution ...