It's been almost a year since my last post. What have I been doing all this time? Procrastinating!
If there was a contest for the laziest person in the world right now, I'd win hands down.
So while I was whiling away my time yet again, I came across this article, which I had read a few months ago, by Peter Norvig on learning programming in which he contends that it would take 10 years to properly learn programming. That reminded me to the article that had linked me to Norvig's article, on gamedev.net - Become a Good Programmer in Six Really Hard Steps.
I reread both of them. While reading the gamedev.net article I realized that I should have been doing all the things the article says, instead of procrastinating. Then it hit me. If I keep this up, I'll never accomplish any of the goals I had set for myself. This lazy streak has to end.
Now, where to begin. This step has been my achilles heel. I'm good at planning, but the problem is that I plan too far ahead & get lost in the cool stuff I'll be doing in the months or years to come. The here & now gets neglected. Then because I'm also good at refactoring, as time goes by, instead of feeling guilt or pressure I just refactor my goals, plans & I'm all set day dreaming about the future I might have if I stopped day dreaming.
So I decided to start small. I remembered about Project Euler - a site with interesting programming puzzles. It's not much but I had been going back to the site every couple of months with the intention of getting started on the problems but always found some excuse to not start straight away.
tl;dr : Was lazy, decided to stop fucking around & get started on Project Euler problems.
I created an account & decided to tackle the problems in decreasing order of the number people that have solved them, i.e from the easiest to the hardest.
Solved 4 problems last night, rather this morning.
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.
Simple enough. Add all multiples of 3 below 1000, then add to that all multiples of 5 below 1000 except those which are also multiples of 3.
Answer : 233168num = 0i = 3while i<1000:num += ii += 3i = 5while i<1000:if(i%3):num += ii += 5print num
Next up, Problem 2 : Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
This wasn't that hard either. I just started with 1 & 1 as the first two terms, this way I would have every 3rd term as even.
total = 0Answer : 4613732
term_n1 = 1
term_n = 0
i = 3
term_n = term_n1 + term_n2
term_n2 = term_n1
term_n1 = term_n
total += term_n
i = 0
i += 1
Problem 6 : The sum of the squares of the first ten natural numbers is,
12 + 22 + ... + 102 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)2 = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 385 = 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
Easier than the last one.
import mathAnswer : 25164150
sum_of_sq = 0sq_of_sum = 0for num in range(1,101): sum_of_sq += pow(num,2) sq_of_sum += numsq_of_sum = pow(sq_of_sum,2)diff = sq_of_sum - sum_of_sqprint diff
Problem 5 : 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
Now this one caused me a little bit of a problem. My own mistake though, tried to be too smart. I had determined that we only had to check for multiples of 11, 13, 14, 16, 17, 18, 19 & 20 to get the answer. Then I noticed that all of them would have to be multiples of 20 & from that I somehow ended up with 11, 13, 7, 4, 17, 9, 19. Which didn't give the right answer so I went back to my old list & it worked perfectly.
But the code needs to be optimized. It took 5-6 seconds to run, which for such a small problem is pretty unacceptable.
def high_mul(x, y):i = xj = ywhile i!=j:if i<j:i += xelif i>j:j += yreturn i
Answer : 232792560def func(a, b):if (a == b):return aelse :return(high_mul(a, b))numbers = [11, 13, 14, 16, 17, 18, 19, 20]solution = 1for num in numbers:solution = func(solution, num)print solution
tl;dr2 : Wrote some code.
This is my Project Euler badge. Right now it's just at 4 solved, hopefully I can get it higher in the coming weeks.