Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Prática de Cifragem | Sistema Numérico Octal
Sistemas de Numeração 101

book
Prática de Cifragem

Até agora, tudo bem 😉.

Como você já sabe converter números decimais para binário, um passo útil é aprender a fazer a ação oposta. Conforme você se familiariza com os passos anteriores, este não vai ser um desafio para você. O número 1 em código binário ainda é um, 0 ainda é zero, mas aqui você tem 8 números, então o 9 deve ser convertido adequadamente 0->0 1->1 2->2 3->3 4->4 5->5 6->6 7->7. No exemplo, definimos 1234 como um número decimal.

  1. Você precisa dividir o número por 8 e anotar o resto da divisão.

  2. Em seguida, você deve calcular o número recebido e aplicar a ele o primeiro passo.

  3. Você pode parar se o resultado da divisão for 0.

  4. Reescreva os restos na ordem inversa.

Como mencionado, quanto mais prática, melhor. Aqui você vai realizar uma tarefa complexa, na qual trabalhará não apenas com uma lista de números convertidos, mas também com strings: isso significa que você não deve apenas acrescentar os restos, mas primeiramente convertê-los em strings. Tente realizar a tarefa para ver as representações bonitas em string.

# Defininig the decimal number
decimal_number = 4321
# Creating a list for storing the converted binary number
octal_number = [ ]
# The text should be realised here due to the reason that further the decimal number will be changed
print("The number in decimal numeral system is:", decimal_number)
# Check if the decimal number is zero: further we will convert it till the number is zero
if decimal_number == 0:
# 0 in the octal numeral system is the representation of 0 in decimal one, so we append 0 to the list of the octal numbers
octal_number.append(str(0))
#otherwise
else:
# The loop executes till the number is zero
while decimal_number != 0:
# Counting the remainder of dividing by eight
remainder = decimal_number % 8
# Appending the converted remainder for creating an octal representation
octal_number.append(str(remainder))
# This operation allows to decrease number eight times and work with the integer part of new one
decimal_number = decimal_number//8
# Reversing the string
octal_number = octal_number[::-1]
# Joining all items of the string to make it more readable
octal_number = " ".join(octal_number)
# Printing the result
print("The number in octal numeral system is:", octal_number)
1234567891011121314151617181920212223242526
# Defininig the decimal number decimal_number = 4321 # Creating a list for storing the converted binary number octal_number = [ ] # The text should be realised here due to the reason that further the decimal number will be changed print("The number in decimal numeral system is:", decimal_number) # Check if the decimal number is zero: further we will convert it till the number is zero if decimal_number == 0: # 0 in the octal numeral system is the representation of 0 in decimal one, so we append 0 to the list of the octal numbers octal_number.append(str(0)) #otherwise else: # The loop executes till the number is zero while decimal_number != 0: # Counting the remainder of dividing by eight remainder = decimal_number % 8 # Appending the converted remainder for creating an octal representation octal_number.append(str(remainder)) # This operation allows to decrease number eight times and work with the integer part of new one decimal_number = decimal_number//8 # Reversing the string octal_number = octal_number[::-1] # Joining all items of the string to make it more readable octal_number = " ".join(octal_number) # Printing the result print("The number in octal numeral system is:", octal_number)
copy
Tarefa

Swipe to start coding

Escreva o código que decodificará o número 3196 do sistema de numeração decimal para octal. Preencha as lacunas e siga o algoritmo. Se tudo estiver correto, você receberá outro número especial 🤯, a explicação está esperando por você no final do capítulo.

  1. Defina uma lista vazia para armazenar o numero_octal.
  2. Imprima a variável numero_decimal.
  3. Verifique se a variável numero_decimal é 0.
  4. Anexe 0 se a variável numero_decimal for 0.
  5. Defina o laço que funciona enquanto o numero_decimal não for 0.
  6. Conte o resto da divisão do numero_decimal por 8.
  7. Anexe o resto convertido na lista de numeros_octais.
  8. Diminua o numero_decimal usando a divisão inteira por 8.
  9. Faça a string de numeros_octais invertida.

Solução

decimal_number = 3196
octal_number = []
print("The number in decimal numeral system is:", decimal_number)
if decimal_number == 0:
octal_number.append(str(0))
else:
while decimal_number != 0:
remainder = decimal_number % 8
octal_number.append(str(remainder))
decimal_number = decimal_number // 8
octal_number = octal_number[::-1]
octal_number =" ".join(octal_number)
print("The number in octal numeral system is:", octal_number)

Nota

Aposto que você já recebeu 6174. Este é um número de Kaprekar e recebe esse nome em homenagem ao matemático D.R. Kaprekar. Tente fazer o simples cálculo com um número que consista em pelo menos dois dígitos diferentes: 5678, por exemplo. Primeiramente, escreva este número em ordem decrescente e ascendente e subtraia: 8765 - 5678 = 3087, em seguida, faremos o mesmo, mas com o número 3087 8730 - 0378 = 8352, o mesmo aqui 8532 - 2358 = 6174 7641 - 1467 = 6774. Você pode aplicar o mesmo algoritmo a diferentes números e receberá 6174 todas as vezes.

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 2
single

single

decimal_number = 3196
# Define an empty list for storing the octal number
octal_number = ___
# Output the decimal_number variable
___("The number in decimal numeral system is: ", ___)
# Check if the decimal_number variable is zero
if decimal_number___:
# Append converted 0 in this case
octal_number.append(str(0))
else:
# Define the loop which works while decimal number is not zero
while ___:
# Count the remainder of division decimal_number by 8
remainder = ___
# Append converted remainder to the string of octal numbers
octal_number.___
# Decrese decimal_number by integer division it by 8
decimal_number = decimal_number ___ ___
# Make the string reversed
octal_number = ___[___]
octal_number = " ".join(octal_number)
print("The number in octal numeral system is: ", octal_number)

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

some-alt