Project Euler 3: (Iron)Python

Posted by Ben Griswold on Johnny Coder See other posts from Johnny Coder or by Ben Griswold
Published on Mon, 13 Sep 2010 02:42:58 +0000 Indexed on 2010/12/06 16:59 UTC
Read the original article Hit count: 319

In my attempt to learn (Iron)Python out in the open, here’s my solution for Project Euler Problem 3

As always, any feedback is welcome.

# Euler 3
# http://projecteuler.net/index.php?section=problems&id=3
# The prime factors of 13195 are 5, 7, 13 and 29.
# What is the largest prime factor of the number
# 600851475143?
import time
start = time.time()

def largest_prime_factor(n):
  max = n
  divisor = 2

  while (n >= divisor ** 2):
    if n % divisor == 0:
        max, n = n, n / divisor
    else:
        divisor += 1      

  return max

print largest_prime_factor(600851475143)

print "Elapsed Time:", (time.time() - start) * 1000, "millisecs"
a=raw_input('Press return to continue')

© Johnny Coder or respective owner

Related posts about ironpython

Related posts about languages