Created
July 6, 2011 04:25
-
-
Save tarsisazevedo/1066561 to your computer and use it in GitHub Desktop.
euler 14
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from unittest import TestCase, main | |
class TestGerarSequencia(TestCase): | |
def test_sequencia_de_13_deve_ser_13_40_20_10_5_16_8_4_2_1(self): | |
self.assertEquals([13, 40, 20, 10, 5, 16, 8, 4, 2, 1], gerar_sequencia(13)) | |
def test_qual_numero_com_maior_sequencia_entre_1_e_1000000(self): | |
self.assertEquals(maior_numero_com_maior_sequencia_ate(1000000), 837799) | |
def maior_numero_com_maior_sequencia_ate(numero): | |
sequencia = [] | |
maior_numero = 0 | |
for i in range(1, numero + 1): | |
numeros = gerar_sequencia(i) | |
if len(numeros) > len(sequencia): | |
sequencia = numeros | |
maior_numero = i | |
return maior_numero | |
def gerar_sequencia(numero): | |
numeros = [numero] | |
while numero != 1: | |
if numero % 2 == 0: | |
numero /= 2 | |
numeros.append(numero) | |
else: | |
numero = 3 * numero + 1 | |
numeros.append(numero) | |
return numeros | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment