Exercise in python 3n +1

Asked

Viewed 134 times

0

3n +1

Description

Consider the following algorithm to generate a sequence of numbers. Start with an integer n. If n is even, divide by 2. If n is odd, multiply by 3 and add 1. Repeat this process with the new value of n, ending when n = 1. For example, the following sequence of numbers will be generated when n is 22:

22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

Although there is no proof yet, mathematicians believe that this algorithm always ends with n=1, for any integer n. Well, for this problem here in Huxley, this property holds for any integer less than 1,000,000.

For an input n, the "cycle size" of n is the amount of numbers generated up to 1 (including 1 itself). In the example above, the "cycle size" of 22 is 16.

Given two numbers i and j, determine the maximum "cycle size" of all numbers between i and j, including both i and j.

input format

The input consists of a series of pairs of integers i and j, a pair of integers per line. All integers will be less than 1,000,000 and greater than 0.

Note that the entry only ends when there are no more numbers. Find out how to make your program work in this case. Each language has a different way of reading while there is still input to read.

output format

For each pair of integers i and j, print i and j in the same order in which they appear in the input and then print the maximum "cycle size" found. These 3 numbers must be separated by a space, with all 3 numbers in a row and being an output line for each line of the input.

See the example file to better understand the input and output format.

inserir a descrição da imagem aqui

My code:

def formula(base):
    numeros = []
    numeros.append(base)

    while base != 1:
        if base % 2 == 0:
            base /= 2
            numeros.append(int(base))
        else:
            base *= 3
            base += 1
            numeros.append(int(base))

    return len(numeros)

def auxiliar(i,j):
    lista = list(range(i,j+1))
    resultados = []

    for elemento in lista:
        resultados.append(formula(elemento))

    return f'{i} {j} {max(resultados)}'


while True:
    entrada = str(input())
    if entrada == '':
        break
    else:
        dados = entrada.split()
        i = int(dados[0])
        j = int(dados[1])

        print(auxiliar(i,j))

The problem:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • 1

    ...the entry only ends when there are no more numbers... in your program the entry ends when you type a pair. You have to first read all the pairs that the tester sends in and then start calculating. I won’t do it because I think it will take the pleasure of the journey towards solution.

  • 1

    input already returns a string, so do str(input()) is redundant and unnecessary, just do input() and you already have the string. About the EOF problem: https://answall.com/a/495050/112052

  • 1

    Thanks @Augustovasques

  • 1

    Thank you @hkotsubo

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.