Un sistema numérico puede considerarse como una notación matemática de números que utiliza un conjunto de dígitos o símbolos. En palabras más simples, el sistema numérico es un método para representar números. Todo sistema numérico se identifica con la ayuda de su base o raíz.
Por ejemplo, los sistemas numéricos binarios , octales , decimales y hexadecimales se utilizan en la programación de microprocesadores . En este artículo, se analiza uno de esos sistemas numéricos.
Sistema numérico ternario:
si el valor base de un sistema numérico es 3, esta representación se conoce como representación ternaria . Los dígitos en este sistema son 0, 1 y 2 .
También existe un sistema numérico llamado Ternario Equilibrado que comprende los dígitos −1, 0 y +1 . La representación ternaria de un número es más compacta que la del sistema numérico binario.
Pasos para convertir decimal a ternario:
- Divide el número entre 3.
- Obtenga el cociente entero para la próxima iteración.
- Obtenga el resto para el dígito ternario.
- Repite los pasos hasta que el cociente sea igual a 0.
Por ejemplo: sea N = 101. La siguiente imagen ilustra la conversión paso a paso de 101 10 a la base-3.
A continuación se muestra la implementación de Decimal a Binario y Viceversa:
C++
// C++ program to convert decimal // number to ternary number #include <cstdio> #include <iostream> #include <math.h> using namespace std; // Function to convert a decimal // number to a ternary number void convertToTernary(int N) { // Base case if (N == 0) return; // Finding the remainder // when N is divided by 3 int x = N % 3; N /= 3; if (x < 0) N += 1; // Recursive function to // call the function for // the integer division // of the value N/3 convertToTernary(N); // Handling the negative cases if (x < 0) cout << x + (3 * -1); else cout << x; } // Function to convert the decimal to ternary void convert(int Decimal) { cout << "Ternary number of " << Decimal << " is: "; // If the number is greater // than 0, compute the // ternary representation // of the number if (Decimal != 0) { convertToTernary(Decimal); } else cout << "0" << endl; } // Driver code int main() { int Decimal = 2747; convert(Decimal); return 0; }
Java
// Java program to convert decimal // number to ternary number import java.io.*; class GFG { // Function to convert a decimal // number to a ternary number static void convertToTernary(int N) { // Base case if (N == 0) return; // Finding the remainder // when N is divided by 3 int x = N % 3; N /= 3; if (x < 0) N += 1; // Recursive function to // call the function for // the integer division // of the value N/3 convertToTernary(N); // Handling the negative cases if (x < 0) System.out.print( x + (3 * -1)); else System.out.print( x); } // Function to convert the decimal to ternary static void convert(int Decimal) { System.out.print("Ternary number of " +Decimal +" is: "); // If the number is greater // than 0, compute the // ternary representation // of the number if (Decimal != 0) { convertToTernary(Decimal); } else System.out.println("0" ); } // Driver Code public static void main (String[] args) { int Decimal = 2747; convert(Decimal); } } // This code is contributed by shivanisinghss2110
Python3
# Python3 program to convert decimal # number to ternary number # Function to convert a decimal # number to a ternary number def convertToTernary(N): # Base case if (N == 0): return; # Finding the remainder # when N is divided by 3 x = N % 3; N //= 3; if (x < 0): N += 1; # Recursive function to # call the function for # the integer division # of the value N/3 convertToTernary(N); # Handling the negative cases if (x < 0): print(x + (3 * -1), end = ""); else: print(x, end = ""); # Function to convert the # decimal to ternary def convert(Decimal): print("Ternary number of ", Decimal, " is: ", end = ""); # If the number is greater # than 0, compute the # ternary representation # of the number if (Decimal != 0): convertToTernary(Decimal); else: print("0", end = ""); # Driver Code if __name__ == '__main__': Decimal = 2747; convert(Decimal); # This code is contributed by Rajput-Ji
C#
// C# program to convert decimal // number to ternary number using System; class GFG { // Function to convert a decimal // number to a ternary number static void convertToTernary(int N) { // Base case if (N == 0) return; // Finding the remainder // when N is divided by 3 int x = N % 3; N /= 3; if (x < 0) N += 1; // Recursive function to // call the function for // the integer division // of the value N/3 convertToTernary(N); // Handling the negative cases if (x < 0) Console.Write( x + (3 * -1)); else Console.Write( x); } // Function to convert the decimal to ternary static void convert(int Decimal) { Console.Write("Ternary number of " +Decimal +" is: "); // If the number is greater // than 0, compute the // ternary representation // of the number if (Decimal != 0) { convertToTernary(Decimal); } else Console.WriteLine("0" ); } // Driver Code public static void Main (string[] args) { int Decimal = 2747; convert(Decimal); } } // This code is contributed by AnkitRai01
Javascript
<script> // Javascript program to convert decimal // number to ternary number // Function to convert a decimal // number to a ternary number function convertToTernary(N) { // Base case if (N == 0) return; // Finding the remainder // when N is divided by 3 let x = N % 3; N = parseInt(N / 3, 10); if (x < 0) N += 1; // Recursive function to // call the function for // the integer division // of the value N/3 convertToTernary(N); // Handling the negative cases if (x < 0) document.write(x + (3 * -1)); else document.write(x); } // Function to convert the decimal to ternary function convert(Decimal) { document.write("Ternary number of " + Decimal + " is: "); // If the number is greater // than 0, compute the // ternary representation // of the number if (Decimal != 0) { convertToTernary(Decimal); } else document.write("0" + "</br>"); } let Decimal = 2747; convert(Decimal); // This code is contributed by divyeshrabadiya07. </script>
Ternary number of 2747 is: 10202202
Complejidad de tiempo: O (log 3 N)
Espacio Auxiliar: O(log 3 N)
Pasos para convertir ternario a decimal:
- Conecta cada dígito del número ternario con su correspondiente potencia de tres.
- Multiplica cada dígito por su correspondiente potencia de tres y suma todos los números que obtuviste.
Por ejemplo: sea N = 10202. La siguiente imagen ilustra la conversión paso a paso de 10202 3 a la base-10.
A continuación se muestra la implementación de Decimal a Binario y Viceversa:
C++
// C++ program to convert a // ternary number to decimal number #include <cstdio> #include <iostream> #include <math.h> using namespace std; // Function to convert a ternary // number to a decimal number void convertToDecimal(int N) { cout << "Decimal number of " << N << " is: "; // If the number is greater than 0, // compute the decimal // representation of the number if (N != 0) { int decimalNumber = 0, i = 0, remainder; // Loop to iterate through // the number while (N != 0) { remainder = N % 10; N /= 10; // Computing the decimal digit decimalNumber += remainder * pow(3, i); ++i; } cout << decimalNumber << endl; } else cout << "0" << endl; } // Driver code int main() { int Ternary = 10202202; convertToDecimal(Ternary); return 0; }
Java
// Java program to convert a // ternary number to decimal number class GFG{ // Function to convert a ternary // number to a decimal number static void convertToDecimal(int N) { System.out.print("Decimal number of " + N + " is: "); // If the number is greater than 0, // compute the decimal // representation of the number if (N != 0) { int decimalNumber = 0, i = 0, remainder; // Loop to iterate through // the number while (N != 0) { remainder = N % 10; N /= 10; // Computing the decimal digit decimalNumber += remainder * Math.pow(3, i); ++i; } System.out.print(decimalNumber + "\n"); } else System.out.print("0" + "\n"); } // Driver code public static void main(String[] args) { int Ternary = 10202202; convertToDecimal(Ternary); } } // This code is contributed by Rajput-Ji
Python3
# Python3 program to convert a # ternary number to decimal number import math; # Function to convert a ternary # number to a decimal number def convertToDecimal(N): print("Decimal number of", N, "is:", end = " "); # If the number is greater than 0, # compute the decimal # representation of the number if (N != 0): decimalNumber = 0; i = 0; remainder = 0; # Loop to iterate through # the number while (N != 0): remainder = N % 10; N = N // 10; # Computing the decimal digit decimalNumber += remainder * math.pow(3, i); i += 1; print(decimalNumber); else: print("0"); # Driver code Ternary = 10202202; convertToDecimal(Ternary); # This code is contributed by Code_Mech
C#
// C# program to convert a ternary // number to decimal number using System; class GFG{ // Function to convert a ternary // number to a decimal number static void convertToDecimal(int N) { Console.Write("Decimal number of " + N + " is: "); // If the number is greater than // 0, compute the decimal // representation of the number if (N != 0) { int decimalNumber = 0; int i = 0, remainder; // Loop to iterate through // the number while (N != 0) { remainder = N % 10; N /= 10; // Computing the decimal digit decimalNumber += remainder * (int)Math.Pow(3, i); ++i; } Console.Write(decimalNumber + "\n"); } else Console.Write("0" + "\n"); } // Driver code public static void Main() { int Ternary = 10202202; convertToDecimal(Ternary); } } // This code is contributed by shivanisinghss2110
Javascript
<script> // Javascript program to convert a ternary // number to decimal number // Function to convert a ternary // number to a decimal number function convertToDecimal(N) { document.write("Decimal number of " + N + " is: "); // If the number is greater than // 0, compute the decimal // representation of the number if (N != 0) { let decimalNumber = 0; let i = 0, remainder; // Loop to iterate through // the number while (N != 0) { remainder = N % 10; N = parseInt(N / 10, 10); // Computing the decimal digit decimalNumber += remainder * Math.pow(3, i); ++i; } document.write(decimalNumber + "</br>"); } else document.write("0" + "</br>"); } // Driver code let Ternary = 10202202; convertToDecimal(Ternary); // This code is contributed by divyesh072019 </script>
Producción:
Decimal number of 10202202 is: 2747
Complejidad de tiempo: O (log 10 N)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por harsh_thoriya y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA