📜  查找第n个Kynea号码的程序

📅  最后修改于: 2021-05-04 09:04:42             🧑  作者: Mango

给定正整数n,任务是找到第n个Kynea数。
凯尼(Kynea)数在数学中,凯尼(Kynea)数是以下形式的正整数:

凯尼娅数

其中n是一个正整数。
第n个Kynea数的等价公式为:

kynea数

前几个Kynea号码是:

例子:

Input: 2
Output: 23
    Putting n = 2 in formula,
    = 42 + 2 2+1 - 1
    = 16 + 8 -1
    = 23  

方法1:一个简单的解决方案是通过将n的值放在公式中找出第n个数字

kynea数

下面是上述方法的实现:

C++
// CPP code to find nth Kynea number
 
#include 
using namespace std;
 
// Function to calculate nth kynea number
long nthKyneaNumber(int n)
{
 
    // Calculate nth kynea number
    // using formula ((2^n + 1)^2 ) -2
 
    // Firstly calculate 2^n + 1
    n = (1 << n) + 1;
 
    // Now calculate (2^n + 1)^2
    n = n * n;
 
    // Now calculate ((2^n + 1)^2 ) - 2
    n = n - 2;
 
    // return nth Kynea number
    return n;
}
 
// Driver Program
int main()
{
    int n = 8;
 
    // print nth kynea number
    cout << nthKyneaNumber(n);
 
    return 0;
}


Java
// JAVA code to find nth Kynea number
 
class GFG {
 
    // Function to calculate nth kynea number
    static long nthKyneaNumber(int n)
    {
 
        // Calculate nth kynea number
        // using formula ((2^n + 1)^2 ) -2
 
        // Firstly calculate 2^n + 1
        n = (1 << n) + 1;
 
        // Now calculate (2^n + 1)^2
        n = n * n;
 
        // Now calculate ((2^n + 1)^2 ) - 2
        n = n - 2;
 
        // return nth Kynea number
        return n;
    }
 
    // Driver Program
    public static void main(String[] args)
    {
        int n = 2;
 
        // print nth kynea number
        System.out.println(nthKyneaNumber(n));
    }
}


Python
# Python code to find nth Kynea number
 
# Function to calculate nth kynea number
def nthKyneaNumber( n):
     
    # Calculate nth kynea number
    # using formula ((2 ^ n + 1)^2 ) -2
     
    # Firstly calculate 2 ^ n + 1
    n = ( 1 << n) + 1
     
    # Now calculate (2 ^ n + 1)^2
    n = n * n
     
    # Now calculate ((2 ^ n + 1)^2 ) - 2
    n = n-2
     
     
    # return nth Kynea number
    return n
     
 
 
# Driver Code
n = 2
 
# print nth kynea number
print(nthKyneaNumber(n))


C#
// C# code to find nth Kynea number
 
using System;
class GFG {
 
    // Function to calculate nth kynea number
    static long nthKyneaNumber(int n)
    {
 
        // Calculate nth kynea number
        // using formula ((2^n + 1)^2 ) -2
 
        // Firstly calculate 2^n + 1
        n = (1 << n) + 1;
 
        // Now calculate (2^n + 1)^2
        n = n * n;
 
        // Now calculate ((2^n + 1)^2 ) - 2
        n = n - 2;
 
        // return nth Kynea number
        return n;
    }
 
    // Driver Program
    public static void Main()
    {
        int n = 2;
 
        // print nth kynea number
        Console.WriteLine(nthKyneaNumber(n));
    }
}


PHP


Javascript


C++
// CPP code to find nth Kynea number
 
#include 
using namespace std;
 
// Function to calculate nth kynea number
long nthKyneaNumber(int n)
{
 
    // Calculate nth kynea number
    return ((1 << (2 * n)) + (1 << (n + 1)) - 1);
}
 
// Driver Program
int main()
{
    int n = 2;
 
    // print nth kynea number
    cout << nthKyneaNumber(n);
 
    return 0;
}


Java
// JAVA code to find nth Kynea number
 
class GFG {
 
    // Function to calculate nth kynea number
    static long nthKyneaNumber(int n)
    {
 
        // Calculate nth kynea number
        return ((1 << (2 * n)) + (1 << (n + 1)) - 1);
    }
 
    // Driver Program
    public static void main(String[] args)
    {
        int n = 2;
 
        // print nth kynea number
        System.out.println(nthKyneaNumber(n));
    }
}


Python
# Python code to find nth Kynea number
 
# Function to calculate nth kynea number
def nthKyneaNumber( n):
 
    # Calculate nth kynea number   
    return (( 1 << (2 * n)) + ( 1 << (n + 1)) -1 )
     
 
# Driver Code
n = 2
 
# print nth kynea number
print(nthKyneaNumber(n))


C#
// C# code to find nth Kynea number
 
using System;
class GFG {
 
    // Function to calculate nth kynea number
    static long nthKyneaNumber(int n)
    {
 
        // Calculate nth kynea number
        return ((1 << (2 * n)) + (1 << (n + 1)) - 1);
    }
 
    // Driver Program
    public static void Main()
    {
        int n = 2;
 
        // print nth kynea number
        Console.WriteLine(nthKyneaNumber(n));
    }
}


PHP


Javascript


输出:
66047

方法2:此解决方案基于以下事实:每个Kynea数字在其二进制表示形式中均遵循特定的模式。第n个Kynea数可以二进制形式表示为单个前导,后跟正好是n-1个连续的0,然后是n + 1个连续的1。
例子:

23 is 2nd kynea number
It can be represented in binary as 10111 
(Single leading one, followed by n - 1 ( i.e 2-1=1 ) consecutive 0's, 
followed by n + 1 ( i.e 2 + 1 = 3 ) consecutive 1's.)
n nth Kynea Number Binary Representation
1 7 111
2 23 10111
3 79 1001111
4 287 100011111
5 1087 10000111111
6 4223 1000001111111

观察上表中的Kynea数的二进制模式,可以使用以下公式轻松计算第n个Kynea数:

凯尼娅数

例子:

Input: n = 3
Output: 79
    Using formula,
    = 26 + 24 -1
    = 64 + 15 
    = 79      

下面是上述方法的实现

C++

// CPP code to find nth Kynea number
 
#include 
using namespace std;
 
// Function to calculate nth kynea number
long nthKyneaNumber(int n)
{
 
    // Calculate nth kynea number
    return ((1 << (2 * n)) + (1 << (n + 1)) - 1);
}
 
// Driver Program
int main()
{
    int n = 2;
 
    // print nth kynea number
    cout << nthKyneaNumber(n);
 
    return 0;
}

Java

// JAVA code to find nth Kynea number
 
class GFG {
 
    // Function to calculate nth kynea number
    static long nthKyneaNumber(int n)
    {
 
        // Calculate nth kynea number
        return ((1 << (2 * n)) + (1 << (n + 1)) - 1);
    }
 
    // Driver Program
    public static void main(String[] args)
    {
        int n = 2;
 
        // print nth kynea number
        System.out.println(nthKyneaNumber(n));
    }
}

Python

# Python code to find nth Kynea number
 
# Function to calculate nth kynea number
def nthKyneaNumber( n):
 
    # Calculate nth kynea number   
    return (( 1 << (2 * n)) + ( 1 << (n + 1)) -1 )
     
 
# Driver Code
n = 2
 
# print nth kynea number
print(nthKyneaNumber(n))

C#

// C# code to find nth Kynea number
 
using System;
class GFG {
 
    // Function to calculate nth kynea number
    static long nthKyneaNumber(int n)
    {
 
        // Calculate nth kynea number
        return ((1 << (2 * n)) + (1 << (n + 1)) - 1);
    }
 
    // Driver Program
    public static void Main()
    {
        int n = 2;
 
        // print nth kynea number
        Console.WriteLine(nthKyneaNumber(n));
    }
}

的PHP


Java脚本


输出:
23