Skip to content

Instantly share code, notes, and snippets.

@tarsisazevedo
Created May 16, 2011 00:49
Show Gist options
  • Save tarsisazevedo/973735 to your computer and use it in GitHub Desktop.
Save tarsisazevedo/973735 to your computer and use it in GitHub Desktop.
from unittest import TestCase
class TestFatoresPrimo(TestCase):
def test_fatores_primos_13195(self):
self.assertEquals([5, 7, 13, 29], fatores_primos(13195))
def test_fatores_primos_600851475143(self):
self.assertEquals([71, 839, 1471, 6857], fatores_primos(600851475143))
def num_eh_primo(num):
if num <= 1:
return False
for i in range(2,num):
if num % i == 0:
return False
return True
def crivo(numero):
primos = (possivel_primo for possivel_primo in xrange(2, numero+1) if num_eh_primo(i))
return primos
def fatores_primos(numero):
primos = crivo(numero)
fatores_primos = []
divisor = primos.next()
while numero != 1:
if numero % divisor == 0:
fatores_primos.append(divisor)
numero = numero / divisor
else:
divisor = primos.next()
return fatores_primos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment