Python and Project Euler

So after having been distracted by other projects, I finally made good on my holiday goal of learning Python. After having read the John Zelle book (ref my earlier post 2 months ago), I starting practicing Python by solving math problems on project euler, a community based interest group online for people who like math and programming. Here are a few results:

Problem: 

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.

model answer

My answer

Problem

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?

model answer

my answer

So there you go. While I managed to solve the problems, I used so many lines of code compared to the model answer. I took an average of 2 hours to do each problem. The process can be painful and frustrating at times, but the satisfaction is definitely there when you managed to solve it. And not to mention that this is a surefire way to learn Python!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s