Pell Number in JavaIn this section, we will learn what is Pell number and also create Java programs to check if the given number is a Pell number or not. The Pell number program frequently asked in Java coding interviews and academics. Pell NumberIt is a sequence or series of numbers that starts with 0 and 1, and subsequent numbers are the sum of twice the previous Pell number and the Pell number before that. It is an OEIS sequence A000129. Sometimes it is also known as lambda numbers. It is a recursively defined sequence similar to the Fibonacci sequence. We can represent the Pell sequence mathematically, as follows: Where n>1. A Pell prime is a Pell number that is prime. The first few Pell primes are 2, 5, 29, 5741, 33461, 44560482149, 1746860020068409, 68480406462161287469, etc. It is an OEIS sequence A086383. It can be used to find square triangular numbers, to construct integer approximations to the right isosceles triangle, and to solve certain combinatorial enumeration problems. Pell Number ExampleLet's see how the Pell series is generated. We know the first and second terms of the series are 0 and 1. On the basis of these two terms, we can calculate the next Pell number. P_{0} = 0 P_{1} = 1 P_{2} = 1 * 2 + 0 = 2 P_{3} = 2 * 2 + 1 = 5 P_{4} = 5 * 2 + 2 = 12 P_{5} = 12 * 2 + 5 = 29 P_{6} = 29 * 2 + 12 = 70 P_{7} = 70 * 2 + 29 = 169 P_{8} = 169 * 2 + 70 = 408 P_{9} = 408 * 2 + 169 = 985 P_{10} = 985 * 2 + 408 = 2378 We have calculated the first ten terms of the Pell series. Similarly, we can calculate other terms such as 5741, 13860, 33461, 80782, 195025, 470832, 1136689, 2744210, 6625109, 15994428, 38613965, 93222358, 225058681, 543339720, 1311738121, 3166815962, 7645370045, 18457556052, 44560482149, 107578520350, 259717522849. Steps to Find Pell number
Let's implement the above steps in a Java program. Pell Number Java ProgramUsing Recursion ApproachPellNumberExample1.java Output: The 7th term of the series is: 169 Let's see another approach to find the Pell numbers using an iterative approach. Using Iterative ApproachPellNumberExample2.java Output: The 17th term of the series is: 1136689 Print All Pell Numbers up to Specified LimitPellNumberExample3.java Output: Enter the limit: 20 First 20 Pell numbers are: 1, 2, 5, 12, 29, 70, 169, 408, 985, 2378, 5741, 13860, 33461, 80782, 195025, 470832, 1136689, 2744210, 6625109, 15994428
Next TopicJava Get Post
