Skip to content

Commit d7dfba5

Browse files
authored
Added Last Digit Of The Sum Of The Fibonacci Number
1 parent d4f6b3d commit d7dfba5

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

FibonacciSumLastDigit.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.*;
2+
3+
public class FibonacciSumLastDigit {
4+
private static long getFibonacciSumNaive(long n) {
5+
if (n <= 1)
6+
return n;
7+
8+
long previous = 0;
9+
long current = 1;
10+
long sum = 1;
11+
12+
for (long i = 0; i < n - 1; ++i) {
13+
long tmp_previous = previous;
14+
previous = current;
15+
current = tmp_previous + current;
16+
sum += current;
17+
}
18+
19+
return sum % 10;
20+
}
21+
22+
public static void main(String[] args) {
23+
Scanner scanner = new Scanner(System.in);
24+
long n = scanner.nextLong();
25+
long s = getFibonacciSumNaive(n);
26+
System.out.println(s);
27+
}
28+
}
29+

0 commit comments

Comments
 (0)