Posted on by Kalkicode
Code Number

# Find Nth Kynea Number

A Kynea number, also known as a Kynea Mersenne number, is a special type of number that can be expressed as (2^(2 * n)) + (2^n) - 1, where 'n' is a non-negative integer. In mathematical terms, the nth Kynea number is defined as Kn = (2^(2 * n)) + (2^n) - 1.

## Problem Statement

The task is to find the nth Kynea number for a given non-negative integer 'n' using the formula mentioned above.

## Explanation with Suitable Example

Let's understand the process of finding the Kynea numbers with an example for n = 4.

• We need to find the 4th Kynea number, so we put n = 4 in the formula: K4 = (2^(2 * 4)) + (2^4) - 1
• Calculate the values: K4 = (2^8) + (2^4) - 1 = 256 + 16 - 1 = 271.
• Therefore, the 4th Kynea number is 271.

## Pseudocode and Algorithm

1. Create a function `findPower` that takes two integers `number` and `size` as input and returns an integer.

• If `number` is 0, return 0.
• If `size` is 0, return 1.
• Initialize a variable `result` to `number`.
• Run a loop from 2 to `size` (inclusive) and update `result` by multiplying it with `number` in each iteration.
• Return `result`.
2. Create a function `nthKyneaNo` that takes an integer `number` as input and does not return anything.

• If `number` is less than or equal to 0, return (no valid Kynea number for negative or zero 'n').
• Calculate the 2^(2 * number) using `findPower(2, 2 * number)`.
• Calculate the 2^(number + 1) using `findPower(2, number + 1)`.
• Subtract 1 from the sum of the above two calculated values.
• Print the output as `number + " 'nth Kynea number is : " + result`.

## Code Solution

Here given code implementation process.

``````/*
Java Program for
Find Nth Kynea Number
*/
public class KyneaNumber
{
public int findPower(int number, int size)
{
if (number == 0)
{
return 0;
}
else if (size == 0)
{
return 1;
}
else
{
int result = number;
for (int i = 2; i <= size; ++i)
{
result = result * number;
}
return result;
}
}
public void nthKyneaNo(int number)
{
if (number <= 0)
{
// When invalid kynea number
return;
}
int result = (findPower(2, 2 * number) + findPower(2, number + 1) - 1);
System.out.println(number + " 'nth Kynea number is : " + result);
}
public static void main(String[] args)
{
// Test Case
}
}``````

#### Output

``````4 'nth Kynea number is : 287
7 'nth Kynea number is : 16639
9 'nth Kynea number is : 263167
15 'nth Kynea number is : 1073807359``````
``````// Include namespace system
using System;
/*
Csharp Program for
Find Nth Kynea Number
*/
public class KyneaNumber
{
public int findPower(int number, int size)
{
if (number == 0)
{
return 0;
}
else if (size == 0)
{
return 1;
}
else
{
var result = number;
for (var i = 2; i <= size; ++i)
{
result = result * number;
}
return result;
}
}
public void nthKyneaNo(int number)
{
if (number <= 0)
{
// When invalid kynea number
return;
}
var result = (this.findPower(2, 2 * number) +
this.findPower(2, number + 1) - 1);
Console.WriteLine(number + " 'nth Kynea number is : " +
result);
}
public static void Main(String[] args)
{
// Test Case
}
}``````

#### Output

``````4 'nth Kynea number is : 287
7 'nth Kynea number is : 16639
9 'nth Kynea number is : 263167
15 'nth Kynea number is : 1073807359``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ Program for
Find Nth Kynea Number
*/
class KyneaNumber
{
public: int findPower(int number, int size)
{
if (number == 0)
{
return 0;
}
else if (size == 0)
{
return 1;
}
else
{
int result = number;
for (int i = 2; i <= size; ++i)
{
result = result *number;
}
return result;
}
}
void nthKyneaNo(int number)
{
if (number <= 0)
{
// When invalid kynea number
return;
}
int result = (this->findPower(2, 2 *number) +
this->findPower(2, number + 1) - 1);
cout << number << " \'nth Kynea number is : " << result << endl;
}
};
int main()
{
// Test Case
return 0;
}``````

#### Output

``````4 'nth Kynea number is : 287
7 'nth Kynea number is : 16639
9 'nth Kynea number is : 263167
15 'nth Kynea number is : 1073807359``````
``````// C Program for
// Find Nth Kynea Number
#include <stdio.h>

int pow_number(int number, int size)
{
if (number == 0)
{
return 0;
}
else if (size == 0)
{
return 1;
}
else
{
int result = number;
for (int i = 2; i <= size; ++i)
{
result = result *number;
}
return result;
}
}
void kynea_number(int number)
{
if (number <= 0)
{
// When invalid kynea number
return;
}
printf("%d 'nth Kynea number is : %d\n", number,
(pow_number(2, 2 *number) + pow_number(2, number + 1) - 1));
}
int main()
{
kynea_number(4);
kynea_number(7);
kynea_number(9);
kynea_number(15);
return 0;
}``````

#### Output

``````4 'nth Kynea number is : 287
7 'nth Kynea number is : 16639
9 'nth Kynea number is : 263167
15 'nth Kynea number is : 1073807359``````
``````package main
import "fmt"
/*
Go Program for
Find Nth Kynea Number
*/

func findPower(number, size int) int {
if number == 0 {
return 0
} else if size == 0 {
return 1
} else {
var result int = number
for i := 2 ; i <= size ; i++ {
result = result * number
}
return result
}
}
func nthKyneaNo(number int) {
if number <= 0 {
// When invalid kynea number
return
}
var result int = (findPower(2, 2 * number) + findPower(2, number + 1) - 1)
fmt.Println(number, "'nth Kynea number is :", result)
}
func main() {

// Test Case
nthKyneaNo(4)
nthKyneaNo(7)
nthKyneaNo(9)
nthKyneaNo(15)
}``````

#### Output

``````4 'nth Kynea number is : 287
7 'nth Kynea number is : 16639
9 'nth Kynea number is : 263167
15 'nth Kynea number is : 1073807359``````
``````<?php
/*
Php Program for
Find Nth Kynea Number
*/
class KyneaNumber
{
public  function findPower(\$number, \$size)
{
if (\$number == 0)
{
return 0;
}
else if (\$size == 0)
{
return 1;
}
else
{
\$result = \$number;
for (\$i = 2; \$i <= \$size; ++\$i)
{
\$result = \$result * \$number;
}
return \$result;
}
}
public  function nthKyneaNo(\$number)
{
if (\$number <= 0)
{
// When invalid kynea number
return;
}
\$result = (\$this->findPower(2, 2 * \$number) +
\$this->findPower(2, \$number + 1) - 1);
printf("%s\n", \$number.
" 'nth Kynea number is : ".strval(\$result));
}
public static
function main(\$args)
{
// Test Case
}
}
KyneaNumber::main(array());``````

#### Output

``````4 'nth Kynea number is : 287
7 'nth Kynea number is : 16639
9 'nth Kynea number is : 263167
15 'nth Kynea number is : 1073807359``````
``````/*
Node JS Program for
Find Nth Kynea Number
*/
class KyneaNumber
{
findPower(number, size)
{
if (number == 0)
{
return 0;
}
else if (size == 0)
{
return 1;
}
else
{
var result = number;
for (var i = 2; i <= size; ++i)
{
result = result * number;
}
return result;
}
}
nthKyneaNo(number)
{
if (number <= 0)
{
// When invalid kynea number
return;
}
var result = (this.findPower(2, 2 * number) +
this.findPower(2, number + 1) - 1);
console.log(number + " 'nth Kynea number is : " + result);
}
}

function main()
{
// Test Case
}
// Start program execution
main();``````

#### Output

``````4 'nth Kynea number is : 287
7 'nth Kynea number is : 16639
9 'nth Kynea number is : 263167
15 'nth Kynea number is : 1073807359``````
``````#  Python 3 Program for
#  Find Nth Kynea Number
class KyneaNumber :
def findPower(self, number, size) :
if (number == 0) :
return 0
elif (size == 0) :
return 1
else :
result = number
i = 2
while (i <= size) :
result = result * number
i += 1

return result

def nthKyneaNo(self, number) :
if (number <= 0) :
#  When invalid kynea number
return

result = (self.findPower(2, 2 * number) +
self.findPower(2, number + 1) - 1)
print(number ,"'nth Kynea number is :", result)

def main() :
#  Test Case

if __name__ == "__main__": main()``````

#### Output

``````4 'nth Kynea number is : 287
7 'nth Kynea number is : 16639
9 'nth Kynea number is : 263167
15 'nth Kynea number is : 1073807359``````
``````#  Ruby Program for
#  Find Nth Kynea Number
class KyneaNumber
def findPower(number, size)
if (number == 0)
return 0
elsif (size == 0)
return 1
else

result = number
i = 2
while (i <= size)
result = result * number
i += 1
end

return result
end

end

def nthKyneaNo(number)
if (number <= 0)
#  When invalid kynea number
return
end

result = (self.findPower(2, 2 * number) +
self.findPower(2, number + 1) - 1)
print(number ,"'nth Kynea number is : ", result, "\n")
end

end

def main()
#  Test Case
end

main()``````

#### Output

``````4'nth Kynea number is : 287
7'nth Kynea number is : 16639
9'nth Kynea number is : 263167
15'nth Kynea number is : 1073807359
``````
``````/*
Scala Program for
Find Nth Kynea Number
*/
class KyneaNumber()
{
def findPower(number: Int, size: Int): Int = {
if (number == 0)
{
return 0;
}
else if (size == 0)
{
return 1;
}
else
{
var result: Int = number;
var i: Int = 2;
while (i <= size)
{
result = result * number;
i += 1;
}
return result;
}
}
def nthKyneaNo(number: Int): Unit = {
if (number <= 0)
{
// When invalid kynea number
return;
}
var result: Int = (findPower(2, 2 * number) +
findPower(2, number + 1) - 1);
println(""+ number + " 'nth Kynea number is : " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: KyneaNumber = new KyneaNumber();
// Test Case
}
}``````

#### Output

``````4 'nth Kynea number is : 287
7 'nth Kynea number is : 16639
9 'nth Kynea number is : 263167
15 'nth Kynea number is : 1073807359``````
``````import Foundation
/*
Swift 4 Program for
Find Nth Kynea Number
*/
class KyneaNumber
{
func findPower(_ number: Int, _ size: Int) -> Int
{
if (number == 0)
{
return 0;
}
else if (size == 0)
{
return 1;
}
else
{
var result: Int = number;
var i: Int = 2;
while (i <= size)
{
result = result * number;
i += 1;
}
return result;
}
}
func nthKyneaNo(_ number: Int)
{
if (number <= 0)
{
// When invalid kynea number
return;
}
let result: Int = (self.findPower(2, 2 * number) +
self.findPower(2, number + 1) - 1);
print(String(number) + " \'nth Kynea number is : " + String(result));
}
static func main(_ args: [String])
{
// Test Case
}
}
KyneaNumber.main([String]());``````

#### Output

``````4 'nth Kynea number is : 287
7 'nth Kynea number is : 16639
9 'nth Kynea number is : 263167
15 'nth Kynea number is : 1073807359``````
``````/*
Kotlin Program for
Find Nth Kynea Number
*/
class KyneaNumber
{
fun findPower(number: Int, size: Int): Int
{
if (number == 0)
{
return 0;
}
else if (size == 0)
{
return 1;
}
else
{
var result: Int = number;
var i: Int = 2;
while (i <= size)
{
result = result * number;
i += 1;
}
return result;
}
}
fun nthKyneaNo(number: Int): Unit
{
if (number <= 0)
{
// When invalid kynea number
return;
}
val result: Int = (this.findPower(2, 2 * number) +
this.findPower(2, number + 1) - 1);
println(""+number + "'nth Kynea number is : " + result);
}
}
fun main(args: Array < String > ): Unit
{
// Test Case
}``````

#### Output

``````4'nth Kynea number is : 287
7'nth Kynea number is : 16639
9'nth Kynea number is : 263167
15'nth Kynea number is : 1073807359``````

## Java Code Explanation

The provided Java code follows the algorithm described above.

1. The `findPower` function computes the power of a number using a loop.
2. The `nthKyneaNo` function calculates the Kynea number using the provided formula and prints the result.

## Output Explanation

The given Java code has been tested with four test cases: 4, 7, 9, and 15.

• For n = 4, the 4th Kynea number is calculated as 271.
• For n = 7, the 7th Kynea number is calculated as 16639.
• For n = 9, the 9th Kynea number is calculated as 263167.
• For n = 15, the 15th Kynea number is calculated as 1073807359.

## Time Complexity

1. The `findPower` function runs a loop 'size' times, so its time complexity is O(size).
2. The `nthKyneaNo` function involves calling `findPower` twice, so its time complexity can be considered as O(size).

It is important to note that the value of 'size' is proportional to the value of 'n'. So, the time complexity can be represented as O(n).

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post