Foros del Web » Programando para Internet » Python »

[Aporte] Factorización de números

Estas en el tema de [Aporte] Factorización de números en el foro de Python en Foros del Web. @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código Python: Ver original def factor ( n ) :     """return a list with the prime factors of n. Complexity: O(sqrt(n))""" ...
  #1 (permalink)  
Antiguo 22/01/2011, 22:16
Avatar de 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)

Etiquetas: aportes
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 09:55.