lundi 28 août 2017

Python breaking a nested loop with multiple if functions in it

I have a code in which I need to find the prime factor of a number. I need to know how to break my code after it found the prime factors, so when it reaches 1. I'm just starting coding with python and I'm not yet that familiair with other librairies, so I wondered if I could make a break with just the regular python code. It should stop after num reaches 1.

num = int(input("Give me a number:"))
priemgetallen = []

for reeks in range(2, num+1):
    print(reeks)
    for priemgetal in range(2,reeks):
        if reeks % priemgetal != 0:
            print(priemgetal)
            if num%priemgetal == 0:
                print("This is the old num", num)
                num = num/priemgetal
                print("This is the new num", num)
                priemgetallen.append(priemgetal)
                if num > 1:
                    if num%priemgetal == 0:
                        print("This is the new num", num)
                        num = num/priemgetal
                        print("This is the old num", num)
                        priemgetallen.append(priemgetal)
                else:
                    print(priemgetallen)
                    break
            else:
                print("Num stays old")

print(priemgetallen)  

Aucun commentaire:

Enregistrer un commentaire