Jacobsthal Number in JavaIn this section, we will learn about finding the jacobsthal number in Java. Mathematically, the jacobsthal numbers are defined as: Ja = (2a - (-1)a) / 3 where a >= 0 Thus, For a = 0, J0 = (20 - (-1)0) / 3 = (1 - (1)) / 3 = 0 / 3 = 0 For a = 1, J1 = (21 - (-1)1) / 3 = (2 - (-1)) / 3 = 3 / 3 = 1 For a = 2, J2 = (22 - (-1)2) / 3 = (4 - (1)) / 3 = 3 / 3 = 1 For a = 3, J3 = (23 - (-1)3) / 3 = (8 - (-1)) / 3 = 9 / 3 = 3 For a = 4, J4 = (24 - (-1)4) / 3 = (16 - (1)) / 3 = 15 / 3 = 5 For a = 5, J4 = (25 - (-1)5) / 3 = (32 - (-1)) / 3 = 33 / 3 = 11 ImplementationLet's see how one can implement the above mathematical formula. Observe the following Java program. FileName: JacobsthalNumberExample1.java Output: The first 20 Jacobsthal numbers are: 0 1 1 3 5 11 21 43 85 171 341 683 1365 2731 5461 10923 21845 43691 87381 174763 Using RecursionThe jacbosthal numbers can also be found using recursion. However, to find jacbosthal numbers using recursion, we must know the recursive formula of it. J0 = 0 and J1 = 1 & Ja = Ja - 1 + 2 * Ja - 2, for a >= 2 Thus, For a = 2 J2 = J2 - 1 + 2 * J2 - 2 = J1 + 2 * J0 = 1 + 2 * 0 = 1 + 0 = 1 For a = 3, J3 = J3 - 1 + 2 * J3 - 2 = J2 + 2 * J1 = 1 + 2 * 1 = 1 + 2 = 3 For a = 4, J4 = J4 - 1 + 2 * J4 - 2 = J3 + 2 * J2 = 3 + 2 * 1 = 3 + 2 = 5 For a = 5, J5 = J5 - 1 + 2 * J5 - 2 = J4 + 2 * J3 = 5 + 2 * 3 = 5 + 6 = 11 ImplementationLet's see the implementation of the above recursive formula. FileName: JacobsthalNumberExample2.java Output: The first 20 Jacobsthal numbers are: 0 1 1 3 5 11 21 43 85 171 341 683 1365 2731 5461 10923 21845 43691 87381 174763 Iterative ImplementationInstead of recursion, one can also use loops to find the jacobsthal numbers. The following program depicts the same. ImplementationLet's see the implementation of the above recursive formula in an iterative way. FileName: JacobsthalNumberExample3.java Output: The first 20 Jacobsthal numbers are: 0 1 1 3 5 11 21 43 85 171 341 683 1365 2731 5461 10923 21845 43691 87381 174763 The above program uses an array to compute the jacobsthal numbers, whose size is dependent on the total number of jacobsthal numbers we want to find. Therefore, for computing up to 10 jacobsthal numbers, we will use an array of size 10, and for computing up to 20 jacobsthal numbers, we will use an array of size 20. If we observe carefully, we will find that the current jacobsthal number is dependent on the last two jacobsthal number. In the following recursive formula, Ja = Ja - 1 + 2 * Ja - 2 Ja - 1 is the last jacobsthal number, and Ja - 2 is the second last jacobsthal number. Therefore, instead of using an array, we can use only two variables to find the jacobsthal numbers. The following program illustrates the same. FileName: JacobsthalNumberExample4.java Output: The first 20 Jacobsthal numbers are: 0 1 1 3 5 11 21 43 85 171 341 683 1365 2731 5461 10923 21845 43691 87381 174763 Note: The above approach is the best approach for finding the Jacobsthal numbers as the space complexity of the program is O(1) also, it takes the least time as compared to the other approaches.Next TopicJava BLOB |
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