Kynea Numbers in Java17 Mar 2025 | 7 min read 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 RecursionThe 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 ArrayWe 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 FormulaThe 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 |
Java 9 Interface Private Methods
Java 9 Private Interface Methods In Java 9, we can create private methods inside an interface. Interface allows us to declare private methods that help to share common code between non-abstract methods. Before Java 9, creating private methods inside an interface cause a compile time error. The following...
1 min read
Future in Java 8
In Java, the Callable interface was introduced in Java 5 as an alternative to existing Runnable interface. It wraps a task and pass it to a Thread or thread pool for asynchronous execution. The Callable represents an asynchronous computation, whose value is available through a Future...
4 min read
Complex Java Programs
To know more and in-depth about a programming language, one should practice the specific programming language programs. Working with programs will make you learn and understand the programming language better and will never forget the concepts when implemented practically. Especially if you are a beginner, then...
8 min read
Two Types of Streams Offered by Java
In Java, the streams are majorly utilized for providing and offering several programming paradigms that are used for data processing and in an expensive and concise manner. Java contains two major stream types and they are Intermediate Streams and Terminal Streams. Let's understand about both Intermediate Stream and...
7 min read
Final Method Overloading in Java| Can We Overload Final Methods
? Java, being an object-oriented programming language, provides a powerful mechanism known as method overloading, allowing developers to define multiple methods with the same name but different parameters within the same class. However, when it comes to final methods, a question arises: can we overload final methods...
6 min read
Difference Between Collection.stream().forEach() and Collection.forEach() in Java
Collection.forEach() and Collection.stream().forEach() are both used for iterating across the collections and are not significantly different from one another. There is no major difference between the two, as both of them provide the same result. However, there are some. Collection.stream().forEach() Method Iteration in a group of objects...
4 min read
Check if One String Swap Can Make Strings Equal in Java
Two strings, str1 and str2, of the same length are given to us. Selecting two indices in a string, which need not be distinct, and switching the characters at these indices is known as a string swap. If, at most, one string swap can be done...
4 min read
Abstract Method in Java
In object-oriented programming, abstraction is defined as hiding the unnecessary details (implementation) from the user and focusing on essential information (functionality). It increases the efficiency and reduces complexity. In Java, abstraction can be achieved using abstract classes and methods. Abstract Method In Java, an abstract method is...
5 min read
String Literal Vs String Object in Java
The Java.lang.String or String class, which is a notable class in API. With its several unique features that are not immediately apparent to many programmers, the String class is unique in the Java API. Understanding the String class is a prerequisite for learning Java. It...
4 min read
3N+1 Problem in Java
The 3N+1 problem is an abstract mathematical problem that is a conjecture (not yet proven). It is also known as Collatz problem. In this section, we will discuss the 3N+1 problem along with its Java program. The task is to write a Java program that will read...
3 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India