# Fermat Number in Java

In this section, we will discuss what is Fermat number and also create Java programs to check if the given number is a Fermat number or not. The Fermat number program frequently asked in Java coding interviews and academics.

## Fermat Number

Fermat number first studied by Pierre de Fermat. A non-negative odd integer that is in the form of 22k +1 (where k>=0) is known as a Fermat number. It is denoted by Fn. It is OEIS integer sequence A000215. It is difficult to generate Fermat numbers. Since they are used in cryptography and pseudo-random number generation. It also has some powerful mathematical properties.

### Fermat Number Example

First few Fermat numbers are 3, 5, 17, 257, 65537, 4294967297, 18446744073709551617.

First 10 Fermat numbers:

F₀ = 220 +1 = 3

F₁ = 221 +1 = 5

F₂ = 222 +1 = 17

F₃ = 223 +1 = 257

F₄ = 224 +1 = 65537

F5 = 225 +1=4294967297

F6 = 226 +1=18446744073709551617

F₇ = 227 +1 = 340282366920938463463374607431768211457

F₈ = 228 +1 = 115792089237316195423570985008687907853269984665640564039457584007913129639937

F₉ = 229 +1 =

134078079299425970995740249982058461274793658205923933777235614437217640300
73546976801874298166903427690031858186486050853753882811946569946433649006084097
It is conjectured that just the first 5 numbers in this sequence are primes. It is OEIS sequence A019434.

F₀ = 220 +1 = 3

F₁ = 221 +1 = 5

F₂ = 222 +1 = 17

F₃ = 223 +1 = 257

F₄ = 224 +1 = 65537

## Steps to Find Fermat Number

1. Read or initialize an integer n.
2. Calculate 2n by using the Math.pow() method and store the result in a variable (p).
3. Now, calculate 2p and store the result in a variable (res).
4. Add 1 in the variable (res) store the result in a variable (f). It is a Fermat number.

Let's implement the above steps in a Java program.

## Fermat Number Java Program

FermatNumberExample1.java

Output:

```3.0
5.0
17.0
257.0
65537.0
4.294967297E9
1.8446744073709552E19
3.4028236692093846E38
1.157920892373162E77
1.3407807929942597E154
Infinity
```

FermatNumberExample.java

Output:

```First 10 Fermat numbers:
F = 3
F = 5
F = 17
F = 257
F = 65537
F = 4294967297
F = 18446744073709551617
F = 340282366920938463463374607431768211457
F = 115792089237316195423570985008687907853269984665640564039457584007913129639937
F = 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084097

First 12 Fermat numbers factored:
F = 3 (PRIME)
F = 5 (PRIME)
F = 17 (PRIME)
F = 257 (PRIME)
F = 65537 (PRIME)
Pollard rho try factor 4294967297 elapsed time = 8 ms (factor = 641).
F = 641 * 6700417
Pollard rho try factor 18446744073709551617 elapsed time = 33 ms (factor = 274177).
F = 274177 * 67280421310721
```

### Feedback   