1 min to read
Multiples of 3 and 5 - Project Euler Solution
In this post, we are going to solve Project Euler “Multiples of 3 and 5” programming problem. You can find the original question here -> Multiples of 3 and 5
Problem Statement: Multiples of 3 and 5
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.
You can find the original question here -> Project Euler
Multiples of 3 and 5 in python
Explanation
first we will define a function multiples which will take a number as an argument and return the sum of all the multiples of 3 and 5 below that number.
We will initialize a variable sum with 0 and iterate over the range of the number and check if the number is divisible by 3 or 5 or not. If it is divisible by 3 or 5 then we will add it to the sum variable.
Complexity Analysis
- Time complexity : O(n). We traverse the list containing n elements only once.
Conclusion
In this post, we have seen how to solve Project Euler “Multiples of 3 and 5” programming problem. If you want to solve more such problems please visit Project Euler