My solutions to the problems found at Project Euler.
Jump to: | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 16 | 17 | 20 | 21 | 22 | 25 | 3-2 |#! /usr/bin/python
# Problem: Find the first sequence in the sequence of triangle numbers where
# our number of divisors was > 500.
#
# Solution: Implement a simple-but-effective divisor test.
#
# Notes: I had a function that did this that I called find_whole_factors in previous
# solutions, but HOLY CRAP it was bad. This is a marked improvement. After some
# research online and finding out that not only is testing every possible divisor
# 1-n is bad, it's really bad! A little bit of learning goes a long way.
import math
def divisors(n):
""" Return a list of divisors for n. """
sqrtn = int(math.sqrt(n))
divisors = []
if sqrtn ** 2 == n:
divisors.append(sqrtn)
for number in xrange(1,sqrtn):
if n % number == 0:
divisors.append(number)
divisors.append(n/number)
return divisors
if __name__ == "__main__":
running = True
counter = 1
highest = 0
seqsum = 0
while running:
seqsum += counter
factors = divisors(seqsum)
if len(factors) > highest:
highest = len(factors)
print highest
if len(factors) > 500:
running = False
print counter, seqsum
else:
counter += 1