So as many already probably know Euler was a famous European mathematician and physicist. Recently at work a lot of talk has been going around about Project Euler; basically a website which poses mathematical problems that need to be solved by writing program. So a few of us at work have decided to take the plunge and learn a completely new language while attempting to answer some of these questions.

So I guess I should start with saying I'll be using/learning Python for all of my problems. Not a terribly hard choice as I'm already pretty familiar with Perl and PHP which seem to be somewhat similar to Python. Anyways, if anyone else wants to get in on the fun. Feel free to do so and post links to your own blogs/solutions or simply just put them here! I plan on using these solutions as an excuse to actually get posting more on here. So, we'll see how that goes. I may also have to make some sort of hiding script so solutions aren't completely revealed to those that want to accomplish everything on their own. Without further adieu - the first 3 problems and my solutions.

Problem 1

Add all the natural numbers below 1000 that are multiples of 3 or 5.

A simple brute force script - nothing really too elegant.

#!/usr/bin/python
total = 0;
for n in range(1, 1000):
  if n%3 == 0 or n%5 == 0:
    total += n

print total

Problem 2

Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed one million.

Another brute force script - nothing fancy.

#!/usr/bin/python
f1    = 0
f2    = 1
total = 0;

while (f1+f2) < 1000000:
  tmp = f1 + f2
  if tmp%2 == 0:
    total += tmp
  f1 = f2
  f2 = tmp

print total

Problem 3

Find the largest prime factor of 317584931803.

This problem was the first one that actually posed some thought. Given the example "The prime factors of 13195 are 5, 7, 13 and 29.", as well as doing some research; prime factors are just prime numbers that all multiply into the initial number given. Rather than explaining, I'll just let the code speak for itself.

#!/usr/bin/python
limit = 317584931803

#function to find if a number is prime
def is_prime(n) :
  test  = 2
  while (test <= n/2):
    if(n%test == 0):
      return False
    else:
      test += 1
  return True

primes  = []  #holds prime factors
num = 2       #starting point

#loop through all possibilities until we hit the limit
while (num <= limit):

  #assume it's not a multiple of any prime numbers
  is_multiple = False
  add = 1

  #check already known primes
  for prime in primes:
    #check to see if it's a multiple of a prime number
    if(num%prime == 0):
      is_multiple = True

  #if it's not a multiple and it devides into our current limit
  if(not is_multiple and limit%num == 0):

    #actually check to see if it's prime
    if(is_prime(num)):

      #it is! don't increment our current number yet
      #until it's not a factor of our limit
      add = 0

      #add to our array, and re-calculate the new limit
      primes.append(num)
      limit = limit/num

  #incriment to the next number
  num += add

#print our results
print primes

So, all in all these seem like some pretty alright challenges. I've already got my roomate doing them (Java) and I wouldn't be surprised if a few other friends (on or off the blogroll) would get interested as well. I'd also be very interested to hear from some of the veteran pyCoders on things they would've done different, or some key aspects of the language that I'm missing. I quickly found out already that the following code results in an overflow - hence why i'm using the while loop as opposed to a for x in y loop. As always, comments, criticism and randomness is welcome!

for x in range(2, 317584931803):
  #creates array with 317584931803 variables! yikes!

Any better ways to loop through that without having to do a while loop?