Permalink
Browse files

euler12 made pythonic and conforms to pep8 guidelines and pyflakes ch…

…ecked
  • Loading branch information...
1 parent 4f3c3bf commit 05f79c1fd4775c7a914d92de67d64ca4ba7ffe45 @akshar-raaj committed Mar 4, 2012
Showing with 15 additions and 40 deletions.
  1. +15 −40 euler12.py
View
@@ -1,45 +1,20 @@
-def generate_nth_triangle_number(n):
- n_th_triangle=(n*(n+1))/2
- return n_th_triangle
+generate_nth_triangle_number = lambda x: (x * (x + 1)) / 2
-"""def find_num_of_divisors(number):
- num_of_divisors=2
- start=2
- end=number/2
- end=int(end)
- while(start<=end):
- if(number%start==0):
- num_of_divisors=num_of_divisors+1
- start=start+1
- return num_of_divisors"""
def findDivisors(number):
- start=1
- end=number**0.5
- end=int(end)
- list=[]
- while(start<=end):
- if(number%start==0):
- quotient=number/start
- list.append(start)
- list.append(quotient)
- start=start+1
- return list
+ end = int(number ** 0.5)
+ number_of_divisors = 0
+ for i in xrange(1, end + 1):
+ if number % i == 0:
+ number_of_divisors += 2
+ return number_of_divisors
-is_number_found=False
-n=2
-triangle_number=1
-num_of_divisors=1
-while(not is_number_found):
- triangle_number=generate_nth_triangle_number(n)
- #num_of_divisors=find_num_of_divisors(triangle_number)
- list_of_divisors=findDivisors(triangle_number)
- if(len(list_of_divisors)>500):
+from itertools import count
+for i in count(2):
+ triangle_number = generate_nth_triangle_number(i)
+ num_of_divisors = findDivisors(triangle_number)
+ if num_of_divisors > 500:
break
- n=n+1
-print ("Value of the first triangle number to have over five hundred divisors is {0}".format(triangle_number))
-#li=findDivisors(28)
-#print li
-#print generate_nth_triangle_number(10)
-#print find_num_of_divisors(28)
-
+
+print ("First triangle number to have over\
+ five hundred divisors is {0}".format(triangle_number))

0 comments on commit 05f79c1

Please sign in to comment.