Javatpoint Logo
Javatpoint Logo

Kynea Numbers in Java

In this section, we will learn what is Kynea number and also create Java programs to that calculates the Kynea number. The Kynea number program is frequently asked in Java coding interviews and academics.

Kynea numbers are numbers that are recursively defined as:

F(k) = 4 x F(k - 1) - 2 k + 1 + 3, where F(0) is equal to 2.

Thus,

For k = 1,

F(1) = 4 x F(1 - 1) - 2 1 + 1 + 3 = 4 x F(0) - 22 + 3 = 4 x 2 - 4 + 3 = 8 - 4 + 3 = 4 + 3 = 7

For k = 2,

F(2) = 4 x F(2 - 1) - 2 2 + 1 + 3 = 4 x F(1) - 23 + 3 = 4 x 7 - 8 + 3 = 28 - 8 + 3 = 20 + 3 = 23

For k = 3,

F(3) = 4 x F(3 - 1) - 2 3 + 1 + 3 = 4 x F(2) - 24 + 3 = 4 x 23 - 16 + 3 = 92 - 16 + 3 = 76 + 3 = 79

For k = 4,

F(4) = 4 x F(4 - 1) - 2 4 + 1 + 3 = 4 x F(3) - 25 + 3 = 4 x 79 - 32 + 3 = 316 - 32 + 3 = 284 + 3 = 287

For k = 5,

F(5) = 4 x F(5 - 1) - 2 5 + 1 + 3 = 4 x F(4) - 26 + 3 = 4 x 287 - 64 + 3 = 1148 - 64 + 3 = 1084 + 3 = 1087

Approach: Using Recursion

The above recursive formula can easily be implemented using recursion. Observe the following program.

FileName: KyneaNumbers.java

Output:

The first 10 Kynea Numbers are: 
2 7 23 79 287 1087 4223 16639 66047 263167

Complexity Analysis: The program is using recursion as well as fast exponentiation. The recursion goes till k = 0, thus, for recursion, the time complexity is O(k), and for fast exponentiation, the time complexity is O(log2(k + 1). Thus, the overall time complexity of the program is O(k * log2(k + 1)), where k is the Kth Kynea number that has to be found. The program is not using any extra space, thus making the space complexity of the program constant, i.e., O(1).

Approach: Using Auxiliary Array

We can avoid the recursion to reduce the time complexity with the help of an auxiliary array. The following program shows the same.

FileName: KyneaNumbers1.java

Output:

The first 10 Kynea Numbers are: 
2 7 23 79 287 1087 4223 16639 66047 263167

Complexity Analysis: The time complexity of the program is only due to fast exponentiation. Thus, the time complexity of the program is O(log2(k + 1), where k is the kth Kynea number that has to be found. Since the program is using an auxiliary array for storing the previously computed results, the space complexity of the O(n), where n is the total number of Kynea number that has to be found.

We can still reduce the space complexity of the program as it is evident from the recursive formula that the current value is only dependent on the previously computed values. Observe the following program.

FileName: KyneaNumbers2.java

Output:

The first 10 Kynea Numbers are: 
2 7 23 79 287 1087 4223 16639 66047 263167

Complexity Analysis: The time complexity of the program is the same as the previous program. However, the space complexity of the program is O(1), as the program is not using any auxiliary array.

Approach: Using Mathematical Formula

The Mathematical formula for finding the Kynea numbers is:

F(k) = 4k + 2k + 1 - 1, for k >= 0

Thus,

F(0) = 40 + 20 + 1 - 1 = 1 + 21 - 1 = 2

F(1) = 41 + 21 + 1 - 1 = 4 + 22 - 1 = 4 + 4 - 1 = 7

F(2) = 42 + 22 + 1 - 1 = 42 + 23 - 1 = 16 + 8 - 1 = 23

F(3) = 43 + 23 + 1 - 1 = 43 + 24 - 1 = 64 + 16 - 1 = 79

F(4) = 44 + 24 + 1 - 1 = 44 + 25 - 1 = 256 + 32 - 1 = 287

F(5) = 45 + 25 + 1 - 1 = 45 + 26 - 1 = 1024 + 64 - 1 = 1087

Observe the following program that uses this mathematical formula for computing the Kynea numbers.

FileName: KyneaNumbers2.java

Output:

The first 10 Kynea Numbers are: 
2 7 23 79 287 1087 4223 16639 66047 263167

Complexity Analysis: The time complexity, as well as the space complexity of the program, is the same as the previous program.


Next TopicUTF in Java





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA