Last two digits Fibonacci number Java
PROGRAM TO FIND LAST TWO DIGITS OF N'TH FIBONACCI NUMBER.
OUTPUT
1 61 13 53
import
java.util.Arrays;
class
GFG {
// Fills f[] with first 300
// fibonacci numbers
static
void
precomput(
long
f[])
{
/* 0th and 1st number of the
series are 0 and 1*/
f[
0
] =
0
;
f[
1
] =
1
;
/* Add the previous 2 numbers in
the series and store last two
digits of result */
for
(
int
i =
2
; i <
300
; i++)
f[i] = (f[i-
1
] + f[i-
2
]) %
100
;
}
// Returns last two digits of n'th
// Fibonacci Number
static
long
findLastDigit(
long
f[],
int
n)
{
return
(f[(n%
300
)]);
}
/* Driver program to test above function */
public
static
void
main (String args[])
{
// Precomputing units digit of
// first 300 Fibonacci numbers
long
f[] =
new
long
[
300
];
Arrays.fill(f,
0
);
precomput(f);
int
n =
1
;
System.out.println(findLastDigit(f, n));
n =
61
;
System.out.println(findLastDigit(f, n));
n =
7
;
System.out.println(findLastDigit(f, n));
n =
67
;
System.out.println(findLastDigit(f, n));
}
}
1 61 13 53
Comments
Post a Comment