给定整数N ,任务是找到具有N个数字的自然十六进制数的计数。
例子:
Input: N = 1
Output: 15
Input: N = 2
Output: 240
方法:可以观察到,对于N = 1,2,3,…的值,一个序列将形成为15,240,3840,61440,983040,15728640,… ,这是GP序列,其公共比率为16和a = 15 。
因此,第n个项将为15 * pow(16,n – 1) 。
因此, n位自然十六进制数的计数将为15 * pow(16,n – 1) 。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to return the count of n-digit
// natural hexadecimal numbers
int count(int n)
{
return 15 * pow(16, n - 1);
}
// Driver code
int main()
{
int n = 2;
cout << count(n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the count of n-digit
// natural hexadecimal numbers
static int count(int n)
{
return (int) (15 * Math.pow(16, n - 1));
}
// Driver code
public static void main(String args[])
{
int n = 2;
System.out.println(count(n));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the above approach
# Function to return the count of n-digit
# natural hexadecimal numbers
def count(n) :
return 15 * pow(16, n - 1);
# Driver code
if __name__ == "__main__" :
n = 2;
print(count(n));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the count of n-digit
// natural hexadecimal numbers
static int count(int n)
{
return (int) (15 * Math.Pow(16, n - 1));
}
// Driver code
public static void Main(String []args)
{
int n = 2;
Console.WriteLine(count(n));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
240
时间复杂度: O(1)