I first became familiar with Leonhard Euler in a Differential Equations class at UT Dallas. In this context, it was all about Euler’s method for solving differential equations like :

ax²y″+bxy′+cy = 0; around x=0

This project has less to do with calculus, and more to do with the site projecteuler.net. Euler was a prolific mathematician and can be attributed to a number of important methods and identities in applied mathematics, physics, fluid dynamics, and even music.

#### History

In the past the Project Euler series of problems has been helpful in getting started with a new language. I’ve used it to pick up the basics in flow control and algorithm building in VB, VBScript, C++, and now Python. Each time, I create a new login and work through the problems a first time with a focus on getting the answer, then run another pass focusing on optimizing those solutions once I am more familiar with the syntax and best practices of a language.

My Python experience has been a little unique when compared to other languages, partly due to my new experience in Linux web hosting and the command-line focused nature of the work. I built out a simple control and timing structure to allow easy implementation of new solutions.

From my ProjectEuler repo :

def run_prob(method): start = time.clock() print 'Executing {}'.format(method.__name__) print 'Answer : {}'.format(method()) # Executes the function print 'Took {} seconds.'.format(time.clock()-start) if __name__ == '__main__': # Populate the list of global/local namespace members possibles = globals().copy() possibles.update(locals()) while True: # Prompt for user's choice func = 'problem_{}'.format(raw_input('Select a problem Number (q to quit):')) # Quit condition if func[-1].uppercase() == 'Q': break method = possibles.get(func) # Error if the choice is not a valid problem if not method: print 'Please select a valid problem!' # Else execute the specified method else: run_prob(method)

This solution is pretty basic, but allows the easy addition of problem solutions as functions named problem_n where n is the problem number.

bill@pokeybill:~/project_euler$ python project_euler.py Select a problem Number (q to quit):1 Executing problem_1 Answer : xxxxxx (No spoilers!) Took 0.000227 seconds. Select a problem Number (q to quit):q bill@pokeybill:~/project_euler$

Another approach could implement the same code as a function wrapper.

#### Problem Functions

The top of my project_euler.py file looks like this :

#!/usr/bin/env python # -*- coding: utf-8 -*- # Project Euler problem file import time import math #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!PROBLEMS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #*******************************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. def problem_1(): return sum([n for n in range(1000) if (n % 3 == 0 or n % 5 == 0)])

You see a summation of each problem, followed by a problem solution. In the refinement process I plan on using sub-versions to denote improved functions (ie problem_1a ).

Thus far, I’ve only needed the * math* python module for calculations, and

*for basic operation timing. The intention of this exercise is to write my own solutions while minimally leveraging available shortcuts. Some of the*

**time****functions**section (more to come on that later) functions I’ve written have already been useful in a few more serious projects.

def problem_1(): return sum([n for n in range(1000) if (n % 3 == 0 or n % 5 == 0)])

Problem 1’s solution is provided – a basic list comprehension makes a tidy, one-line answer. Each additional problem will follow the same pattern (though most won’t be one-line solutions).

#*******************************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. def problem_2(): return sum([f for f in gen_fib(4000000) if f % 2 == 0])

Problem 2 is a little more complex, involving a separate **functions** section function gen_fib() to return a fibonacci sequence up to 4,000,000. Stay tuned for more, including a breakdown of some of the math functions going into these solutions.