I have been showing off my excitement of learning python with last few blog post in my blog talking about the best tools to learn python with and so on. One of my recent post was about the Project Euler and I didn't forget to mention how good of an tool to learn and use python practically. As you can see I am a beginner as well, so without farther bragging I will start solving these problems one by one. Continue reading for more stuff.
First of all if you are complete beginner and still confused about what loops like for and whiles or how if statement works go head to my post teaching everything in python for beginner that you need before solving Project Euler problems.
Since there is no point if you just copy paste my solution, I will discuss about some strategy and thought process that will help you solve it yourself first.
Here's my code:
I hope you found it useful or learn something new. I am going to solve more problems in Euler with either python or C++ in the future. You can come back here in the future for more articles like this.
First of all if you are complete beginner and still confused about what loops like for and whiles or how if statement works go head to my post teaching everything in python for beginner that you need before solving Project Euler problems.
Since there is no point if you just copy paste my solution, I will discuss about some strategy and thought process that will help you solve it yourself first.
Problem 1: 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.Here's how I solved it. First figured out how to find the multiples of a number, then sum it. As revise I also had to eliminated the repeated multiples of both 3 and 5.
Here's my code:
m = list(range(0,1000,3)) # putting all the multiples of 3 in list m
n = list(range(0,1000,5)) # multiples of 5 in list n
l = list(range(0,1000,15)) # multiples of both 3 and 5 (3*5) = 15 in list l
print((sum(m+n))-(sum(l))) # printing sum of m+n and (-) the sum of l
I hope you found it useful or learn something new. I am going to solve more problems in Euler with either python or C++ in the future. You can come back here in the future for more articles like this.
No comments:
Post a Comment