[Python] Can this integer factorization in Python be improved?

Discussão em 'Python' iniciado por Stack, Fevereiro 5, 2025.

  1. Stack

    Stack Membro Participativo

    I've seen many ways of doing integer factorizations in Python, but didn't really understand them, so I tried to do it on my own way:

    def factorisation(n):
    fact = []
    i = 2
    while i <= n:
    if n % i == 0:
    fact.append(i)
    n //= i
    else:
    i += 1
    return fact


    I think it is working, but I don't really know why the while loop from i to n... From my lesson I learnt that we have to do it from 2 to sqrt(n).

    Did I misunderstand something?

    Can I improve it?

    Continue reading...

Compartilhe esta Página