Ver Mensaje Individual
  #1 (permalink)  
Antiguo 22/01/2011, 22:16
Avatar de razpeitia
razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 1 mes
Puntos: 1360
[Aporte] Factorización de números

Código Python:
Ver original
  1. def factor(n):
  2.     """return a list with the prime factors of n. Complexity: O(sqrt(n))"""
  3.     l = [] #List of factors
  4.     i = 2
  5.     while i * i <= n: #From 2 to sqrt(n)
  6.         while n % i == 0: #While i is factor of n, add to the list of factors.
  7.             l.append(i)
  8.             n /= i
  9.         i += 1
  10.     if n != 1: #If n isn't 1, then is a factor.
  11.         l.append(n)
  12.     return l
  13.        
  14. for i in range(2, 21):
  15.     print i, "->", factor(i)