-3
import random
class myClass:
    
    
    def amicableNumbers(self):
        sum1=0
        sum2=0
        num1=1
        num2=1
        z= 2*(10**7956785)
        zz= z+100000000
        while (sum1 != num2 and sum2 != num1):
            num1,num2= random.randint(z,zz), random.randint(z,zz)
            for i in range(1,num1):
                if(num1 % i == 0):
                    sum1+=i
            for i in range(1,num2):
                if(num2 % i == 0):
                    sum2+=i
            
            if(sum1 == num2 and sum2 == num1):
                print(num1,'and',num2,"are Amicable Numbers")
            else: 
                sum1=0
                sum2=0
            
def main():       
    
    obj1=myClass()
    obj1.amicableNumbers()
    
    
if "__main__" :
    main()

i have asked this question previously but need more elaboration than previous. the suggestions went along the lines of 'have you checked if it will ever be true' it will be. i checked.

Joetmo
  • 1
  • 1
  • 1
    [codereview.se] is a better place to ask. The [help page](https://codereview.stackexchange.com/help/on-topic) states that Performance is an acceptable topic. – 001 Aug 17 '20 at 13:42
  • Also, [Wikipedia](https://en.wikipedia.org/wiki/Amicable_numbers) has some formulas you might be able to use. – 001 Aug 17 '20 at 13:46

1 Answers1

0

You are working with some very large numbers. I think it will always be a bit slow when having for loops running so many iterations. Just doing this calculation:

z=2*(10**7956785)

took about 6 seconds, when I run it.

There is an answer to this here, in which they have avoided the nested loops. However, they are using smaller numbers. I would also check this guide, where different algorithms are shown.

I would have posted this as a comment, but my reputation isn't high enough.

Rasmus
  • 77
  • 7