Tuesday, November 9, 2010

College Days

I thought that in college I'd be going to parties, playing music, and hanging out with non-nerdy people.  Instead, I find myself helping my comp-sci friends with programming assignments and coding in my free time.  Don't get me wrong, I love the comp-sci people, but the de-nerdening I had expected for in college is not forthcoming.

I've spent the bulk of my last two days trying to learn Python.  I worked out this script to solve problem 21 of Project Euler.
#!\usr\local\bin\python

def sum(a):
        divTot = 0
        for k in range(1,a):
                if (a%k) == 0:
                        divTot += k
        return divTot

amicableNumbers = []
total = 0

for i in range(0, 10000):
        temp = sum(i)
        if temp < i:
                if (sum(temp) == i):
                        amicableNumbers.append(i)
                        amicableNumbers.append(temp)

for k in range(0,len(amicableNumbers)):
        total += amicableNumbers[k]

print total
  It takes a few seconds to work, but it does the trick.