def sumPropDiv(n): """returns sum of proper divisors of n""" dSum = 0 for x in range(1, n/2 + 1): if n % x == 0: dSum += x return dSum def amicSum(number): """finds the sum of all amicable numbers less than number, with number greater than 4.""" answer = 0 for x in range(4, number): if sumPropDiv(x) > 4: if sumPropDiv(sumPropDiv(x)) == x and sumPropDiv(x) != x: answer += x print x, "and", sumPropDiv(x), "are an amicable pair." return answer print amicSum(10000)
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