Program to find the nth Kynea number
Given a positive integer n, the task is to find nth Kynea number.
Kynea number: In mathematics, a Kynea number is a positive integer of the form:
where n is a positive integer.
The equivalent formula for nth Kynea number is:
The first few Kynea number are:
7, 23, 79, 287, 1087, 4223, 16639, 66047, 263167, 1050623, 4198399, …..
Examples:
Input: 2
Output: 23
Putting n = 2 in formula,
= 42 + 2 2+1 – 1
= 16 + 8 -1
= 23
Method 1: A Simple Solution is to find out the nth number by putting the value of n in the formula
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long nthKyneaNumber( int n)
{
n = (1 << n) + 1;
n = n * n;
n = n - 2;
return n;
}
int main()
{
int n = 8;
cout << nthKyneaNumber(n);
return 0;
}
|
Java
class GFG {
static long nthKyneaNumber( int n)
{
n = ( 1 << n) + 1 ;
n = n * n;
n = n - 2 ;
return n;
}
public static void main(String[] args)
{
int n = 2 ;
System.out.println(nthKyneaNumber(n));
}
}
|
Python
def nthKyneaNumber( n):
n = ( 1 << n) + 1
n = n * n
n = n - 2
return n
n = 2
print (nthKyneaNumber(n))
|
C#
using System;
class GFG {
static long nthKyneaNumber( int n)
{
n = (1 << n) + 1;
n = n * n;
n = n - 2;
return n;
}
public static void Main()
{
int n = 2;
Console.WriteLine(nthKyneaNumber(n));
}
}
|
PHP
<?php
function nthKyneaNumber( $n )
{
$n = (1 << $n ) + 1;
$n = $n * $n ;
$n = $n - 2;
return $n ;
}
$n = 8;
echo nthKyneaNumber( $n );
|
Javascript
<script>
function nthKyneaNumber(n)
{
n = (1 << n) + 1;
n = n * n;
n = n - 2;
return n;
}
let n = 8;
document.write( nthKyneaNumber(n));
</script>
|
Time Complexity: O(1), only constant operations are being used.
Auxiliary Space: O(1), as no extra space is required.
Method 2: This solution is based on the fact that every Kynea number follows a specific pattern in their binary representation. nth Kynea number can be represented in binary as a single leading one followed by exactly n-1 consecutive 0’s, followed by n+1 consecutive 1’s.
Example:
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 |
Observing the binary pattern of Kynea number in above table, the nth Kynea number can be easily calculated using the formula:
Example:
Input: n = 3
Output: 79
Using formula,
= 26 + 24 -1
= 64 + 15
= 79
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
long nthKyneaNumber( int n)
{
return ((1 << (2 * n)) + (1 << (n + 1)) - 1);
}
int main()
{
int n = 2;
cout << nthKyneaNumber(n);
return 0;
}
|
Java
class GFG {
static long nthKyneaNumber( int n)
{
return (( 1 << ( 2 * n)) + ( 1 << (n + 1 )) - 1 );
}
public static void main(String[] args)
{
int n = 2 ;
System.out.println(nthKyneaNumber(n));
}
}
|
Python
def nthKyneaNumber( n):
return (( 1 << ( 2 * n)) + ( 1 << (n + 1 )) - 1 )
n = 2
print (nthKyneaNumber(n))
|
C#
using System;
class GFG {
static long nthKyneaNumber( int n)
{
return ((1 << (2 * n)) + (1 << (n + 1)) - 1);
}
public static void Main()
{
int n = 2;
Console.WriteLine(nthKyneaNumber(n));
}
}
|
PHP
<?php
function nthKyneaNumber( $n )
{
return ((1 << (2 * $n )) +
(1 << ( $n + 1)) - 1);
}
$n = 2;
echo nthKyneaNumber( $n );
?>
|
Javascript
<script>
function nthKyneaNumber(n)
{
return ((1 << (2 * n)) +
(1 << (n + 1)) - 1);
}
let n = 2;
document.write( nthKyneaNumber(n));
</script>
|
Time Complexity: O(1), only constant operations are being used.
Auxiliary Space: O(1), as no extra space is required.
Last Updated :
11 Mar, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...