from math import sqrt def nth_tri(n): """returns nth triangular number""" total = 0 for x in range(1, n+1): total +=x return total def count_div(n): """returns number of divisors n has""" count = 2 #to go ahead and count n and 1, which are always divisors of n sqRoot = sqrt(n) for x in range(2, int(sqRoot) + 1): if n % x == 0: count += 2 if int(sqRoot) == sqRoot: count -= 1 return count #finding value of first triangular number to have over 500 divisors n = 30 #and it shall remain a mystery as to why I started at 30 while count_div(nth_tri(n)) <= 500: n += 1 print "The ", n, "th tri number has ", count_div(nth_tri(n)), " divisors." print nth_tri(n)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question