1

I want to find Amicable number using Python. Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number. (A proper divisor of a number is a positive factor of that number other than the number itself. For example, the proper divisors of 6 are 1, 2, and 3.) I want to explore 1~20000. So I wrote some codes..

for i in range(1,20000):
    a=0
    for j in range(1,i):
        if i%j==0:
            a=a+j
        break
for p in range(1,20000):
    s=0
    for k in range(1,p):
        if p%k==0:
            s=s+k
        break
if s==a and i!=p:
    print(i,'amicable',p)

I learn only these things.. 'for', 'while',range...So I want to use them.

Eugene
  • 27
  • 4
  • better define a function to compute divisiors of a number (which you do already in a part of your code) and then call the duivisors function for different numbers and check if they are amicable. Note that there might be more efficient methods than exhaustive search which is what you do here, but these are more advanced – Nikos M. Apr 06 '20 at 16:08
  • https://www.geeksforgeeks.org/pairs-amicable-numbers/ – Nikos M. Apr 06 '20 at 16:10
  • https://algorithmcode.wordpress.com/2012/05/10/21-amicable-numbers/ – Nikos M. Apr 06 '20 at 16:12
  • https://en.wikipedia.org/wiki/Amicable_numbers#Rules_for_generation – Nikos M. Apr 06 '20 at 16:14
  • 1
    related https://stackoverflow.com/questions/38094818/what-is-the-most-efficient-way-to-find-amicable-numbers-in-python – Nikos M. Apr 06 '20 at 16:14

0 Answers0