Cullen Number in Java
Cullen number is the member of integer sequence which is defined in OEIS sequence A002064. It was first studied by James Cullen in 1905. In this section, we will discuss what is Cullen number and also create Java programs to check if the given number is a Cullen number or not. The Cullen number program frequently asked in Java coding interviews and academics.
A number that is in the form of 2n*n + 1 or (n*2n) +1 where n is an integer. Most of the Cullen numbers are composite numbers. It is denoted by Cn or C(n). It is divisible by p=2n-1 if p is a prime number and in the form of 8k-3.
If any number of the form 2n*n + 1 or (n*2n) +1 is prime, it is called prime Cullen number. It is defined in OEIS sequence A005849.
Sometimes, a generalized Cullen number base b is defined to be a number of the form n*bn + 1, where n + 2 > b; if a prime can be written in that form, it is then called a generalized Cullen prime. The Woodall numbers (2n*n - 1) are sometimes also called Cullen numbers of the second kind.
The largest known generalized Cullen prime is 2805222 * 252805222 + 1 which is 3,921,539 digits long number discovered by Tom Greer in March 2020.
Cullen Number Example
3 = 21 * 1 + 1
9 = 22 * 2 + 1
25 = 23 * 3 + 1
Some other Cullen numbers are 1, 3, 9, 25, 65, 161, 385, 897, 2049, 4609, 10241, 22529, 49153, 106497, 229377, 491521, 1048577, 2228225, 4718593, 9961473, 20971521, 44040193, 92274689, 192937985, 402653185, 838860801, 1744830465, 3623878657, 7516192769, 15569256449, 32212254721.
1 * 21 + 1 = 3, that is prime.
141 * 2141 + 1 = 393050634124102232869567034555427371542904833, that is also a prime number.
The next prime Cullen is hard to calculate because is a too large number approx. 1423 digits long.
Some other prime Cullen numbers are 1, 141, 4713, 5795, 6611, 18496, 32292, 32469, 59656, 90825, 262419, 361275, 481899, 1354828, 6328548, 6679881.
Still, it is conjectured that there are infinitely many Cullen primes.
Cullen Number Java Program
There are two ways to find Cullen number:
Using Bitwise Left-Shit Operator
In the following program, we have used the bitwise left-shift (<<) operator to find the nth Cullen number. The operator shifts the bits of a number towards the left in a specified number of positions.
Suppose, n=7 then according to the statement (1 << n) * n + 1:
(1<<7) * 7 + 1 (where 1<<7 is equal to 1*27 = 128)
128 * 7 + 1
896 + 1 = 897
Let's implement the above logic in a Java program.
Enter the number: 7 Cullen Number for the value n = 7 is: 897
Let's see another logic for the same.
Using Lambada Expression
In the following program, we have used the Function interface that is a functional interface. It can be used as the assignment target for a lambda expression or method reference. It belongs to java.util.function package. The syntax of the Function interface is as follows:
Where T and R are the type parameters. T is the type of input to the function and R is the type of the result of the function.
The interface provides the apply() method. It applies this function to the given argument.
It accepts t (function argument) as a parameter and returns the function result.
Next TopicMobile Number Validation in Java