Given an integer N, the task is to find the count of natural Hexadecimal numbers with N digits.
Examples:
Input: N = 1
Output: 15
Input: N = 2
Output: 240
Approach: It can be observed that for the values of N = 1, 2, 3, …, a series will be formed as 15, 240, 3840, 61440, 983040, 15728640, … which is a GP series whose common ratio is 16 and a = 15.
Hence the nth term will be 15 * pow(16, n – 1).
So, the count of n-digit natural hexadecimal numbers will be 15 * pow(16, n – 1).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int count( int n)
{
return 15 * pow (16, n - 1);
}
int main()
{
int n = 2;
cout << count(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int count( int n)
{
return ( int ) ( 15 * Math.pow( 16 , n - 1 ));
}
public static void main(String args[])
{
int n = 2 ;
System.out.println(count(n));
}
}
|
Python3
def count(n) :
return 15 * pow ( 16 , n - 1 );
if __name__ = = "__main__" :
n = 2 ;
print (count(n));
|
C#
using System;
class GFG
{
static int count( int n)
{
return ( int ) (15 * Math.Pow(16, n - 1));
}
public static void Main(String []args)
{
int n = 2;
Console.WriteLine(count(n));
}
}
|
Javascript
<script>
function count(n)
{
return 15 * Math.pow(16, n - 1);
}
var n = 2;
document.write(count(n));
</script>
|
Time Complexity: O(log n)
Auxiliary space: O(1), If the recursive stack is considered then it would be O(log(n))