Dado un número entero N , la tarea es encontrar el conteo de números octales naturales hasta N dígitos.
Ejemplos:
Entrada: N = 1
Salida: 7
Explicación:
1, 2, 3, 4, 5, 6, 7 son números octales naturales de 1 dígito.
Entrada: N = 2
Salida: 63
Explicación:
Hay un total de 56 números octales de dos dígitos y 7 números octales de un dígito. Por lo tanto, 56 + 7 = 63.
Planteamiento: Al observar detenidamente, se forma una serie de progresión geométrica [ 7 56 448 3584 28672 229376… ] siendo el primer término 7 y una razón común de 8 .
Por lo tanto,
Nth term = Number of Octal numbers of N digits = 7 * 8N - 1
Finalmente, el conteo de todos los números octales hasta N dígitos se puede encontrar iterando un ciclo de 1 a N y calculando la suma del i-ésimo término usando la fórmula anterior.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find the count of // natural octal numbers upto N digits #include <bits/stdc++.h> using namespace std; // Function to return the count of // natural octal numbers upto N digits int count(int N) { int sum = 0; // Loop to iterate from 1 to N // and calculating number of // octal numbers for every 'i'th digit. for (int i = 1; i <= N; i++) { sum += 7 * pow(8, i - 1); } return sum; } // Driver code int main() { int N = 4; cout << count(N); return 0; }
Java
// Java program to find the count of // natural octal numbers upto N digits public class GFG { // Function to return the count of // natural octal numbers upto N digits static int count(int N) { int sum = 0; // Loop to iterate from 1 to N // and calculating number of // octal numbers for every 'i'th digit. for (int i = 1; i <= N; i++) { sum += 7 * Math.pow(8, i - 1); } return sum; } // Driver code public static void main (String[] args) { int N = 4; System.out.println(count(N)); } } // This code is contributed by AnkitRai01
Python3
# Python3 program to find the count of # natural octal numbers upto N digits # Function to return the count of # natural octal numbers upto N digits def count(N) : sum = 0; # Loop to iterate from 1 to N # and calculating number of # octal numbers for every 'i'th digit. for i in range(N + 1) : sum += 7 * (8 **(i - 1)); return int(sum); # Driver code if __name__ == "__main__" : N = 4; print(count(N)); # This code is contributed by AnkitRai01
C#
// C# program to find the count of // natural octal numbers upto N digits using System; class GFG { // Function to return the count of // natural octal numbers upto N digits static int count(int N) { int sum = 0; // Loop to iterate from 1 to N // and calculating number of // octal numbers for every 'i'th digit. for (int i = 1; i <= N; i++) { sum += (int)(7 * Math.Pow(8, i - 1)); } return sum; } // Driver code public static void Main () { int N = 4; Console.WriteLine(count(N)); } } // This code is contributed by AnkitRai01
Javascript
<script> // Javascript program to find the count of // natural octal numbers upto N digits // Function to return the count of // natural octal numbers upto N digits function count(N) { var sum = 0; // Loop to iterate from 1 to N // and calculating number of // octal numbers for every 'i'th digit. for (var i = 1; i <= N; i++) { sum += 7 * Math.pow(8, i - 1); } return sum; } // Driver code var N = 4; document.write(count(N)); </script>
4095
Complejidad de tiempo: O(N)
Espacio Auxiliar: O(1)