We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent d4f6b3d commit d7dfba5Copy full SHA for d7dfba5
1 file changed
FibonacciSumLastDigit.java
@@ -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