Jacobsthal Number in JavaIn this section, we will learn about finding the jacobsthal number in Java. Mathematically, the jacobsthal numbers are defined as: J_{a }= (2^{a}  (1)^{a}) / 3 where a >= 0 Thus, For a = 0, J_{0 }= (2^{0 } (1)^{0}) / 3 = (1  (1)) / 3 = 0 / 3 = 0 For a = 1, J_{1 }= (2^{1 } (1)^{1}) / 3 = (2  (1)) / 3 = 3 / 3 = 1 For a = 2, J_{2 }= (2^{2 } (1)^{2}) / 3 = (4  (1)) / 3 = 3 / 3 = 1 For a = 3, J_{3 }= (2^{3 } (1)^{3}) / 3 = (8  (1)) / 3 = 9 / 3 = 3 For a = 4, J_{4 }= (2^{4 } (1)^{4}) / 3 = (16  (1)) / 3 = 15 / 3 = 5 For a = 5, J_{4 }= (2^{5 } (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. J_{0} = 0 and J_{1} = 1 & J_{a} = J_{a  1} + 2 * J_{a  2}, for a >= 2 Thus, For a = 2 J_{2 }= J_{2  1 }+ 2 * J_{2  2 }= J_{1 }+ 2 * J_{0 }= 1 + 2 * 0 = 1 + 0 = 1 For a = 3, J_{3 }= J_{3  1 }+ 2 * J_{3  2 }= J_{2 }+ 2 * J_{1 }= 1 + 2 * 1 = 1 + 2 = 3 For a = 4, J_{4 }= J_{4  1 }+ 2 * J_{4  2 }= J_{3 }+ 2 * J_{2 }= 3 + 2 * 1 = 3 + 2 = 5 For a = 5, J_{5 }= J_{5  1 }+ 2 * J_{5  2 }= J_{4 }+ 2 * J_{3 }= 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, J_{a }= J_{a  1 }+ 2 * J_{a  2} J_{a  1 }is the last jacobsthal number, and J_{a  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
