2009-01-31

Project Euler: Problem 2

Here's my solution to Problem 2.

"""Sums all the even-valued Fibonacci numbers less than 4,000,000.
For more info, see http://projecteuler.net/index.php?section=problems&id=2
"""
# Taken from the Python Decorator Library
class memoized(object):
"""Decorator that caches a function's return value each time it is called.
If called later with the same arguments, the cached value is returned, and
not re-evaluated.
"""
def __init__(self, func):
self.func = func
self.cache = {}
def __call__(self, *args):
try:
return self.cache[args]
except KeyError:
self.cache[args] = value = self.func(*args)
return value
except TypeError:
# uncachable -- for instance, passing a list as an argument.
# Better to not cache than to blow up entirely.
return self.func(*args)
def __repr__(self):
"""Return the function's docstring."""
return self.func.__doc__
@memoized
def fib(term):
"""Calculate the nth Fibonacci number."""
if (1 >= term):
return term
return fib(term - 2) + fib(term - 1)
def find_highest_necessary_num(max):
"""Find the highest n such that fib(n) < max."""
n = 0
while(fib(n) < max):
n += 1
return n
def problem_two():
"""Calculate the sum of the even Fibonacci numbers less than 4,000,000."""
fibs = [fib(n) for n in range(find_highest_necessary_num(4000000))]
return sum([cur_fib for cur_fib in fibs if not cur_fib % 2])
if (__name__ == "__main__"):
print problem_two()
view raw problem2.py hosted with ❤ by GitHub

Back to flipping out...

Project Euler: Problem 1

As part of my ongoing efforts to learn Python, I've decided to start working through the Project Euler problem sets. If you don't want to see spoilers, then leave now. Here's the Python script I whipped out for Problem One.

"""Finds the solution to Problem 1 from Project Euler.
For more details, see http://projecteuler.net/index.php?section=problems&id=1
"""
def problem_one():
"""Sum the natural numbers less than 1000 that are multiples of 3 or 5."""
return sum([n for n in range(1000) if ((0 == n % 3) or (0 == n % 5))])
if (__name__ == "__main__"):
print problem_one()
view raw problem1.py hosted with ❤ by GitHub

Back to flipping out...

2009-01-30

Sneak Attack—Send IMs from Quicksilver

If you're using Adium and Quicksilver, and you hate having to mouse over to the contacts list and double-click, this one's for you.

Back to flipping out...

Sneak Attack—Code Like a Pythonista: Idiomatic Python

Pure goodness, right up there with PEP-8 and PEP-20.

Back to flipping out...

2009-01-09

I know it was you, Bravos. You broke my heart!

Low. Bush league.Disgraceful. The Atlanta Braves have opted to let John Smoltz go to the Red Sox. The face of the franchise, a man who altered his career multiple times to help the team, who took hometown discounts, and who pitched through pain to help them win, has gone to another team because the organization didn't think he was worth a $5.5 million flyer. This from the same organization that just made a contract offer to Mike Hampton! John Smoltz deserved better; he earned better. As an organization that has always prided itself on doing things The Right Way™, the Braves should be better. I can't remember ever being ashamed of the Braves in my entire life… until now.

We now return you to your regularly (or not) scheduled programming blog updates.