-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci.java
More file actions
113 lines (101 loc) · 3.36 KB
/
Copy pathFibonacci.java
File metadata and controls
113 lines (101 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.mytest.junit.samples;
import java.math.BigInteger;
// all the print statements in this class were used for debugging purposes.
public class Fibonacci {
/**
* A static Method to calculate the sum of the first
* <code>n</code> Fibonacci numbers.
*
* @param n First <code>n</code> Fibonacci numbers to sum up.
* @return The sum (BigInteger).
*/
public static BigInteger sumOfFibo(int n) {
BigInteger[] nums = new BigInteger[3];
BigInteger sum = BigInteger.valueOf(0);
for(int i = 0; i <= n; i++) {
if(i <= 1) {
nums[i] = BigInteger.valueOf(i);
sum = BigInteger.valueOf(i);
}
else {
nums[2] = nums[0].add(nums[1]);
nums[0] = nums[1];
nums[1] = nums[2];
sum = sum.add(nums[2]);
}
}
return sum;
}
/**
* A static method to calculate the last digit of the sum
* of Fibonacci numbers in a given interval <code>[f, t]</code>.
* <p>
* This method simply subtracts two large sums (BigInteger)
* of <code>t</code> first numbers and <code>f - 1</code>
* first numbers of the Fibonacci sequence. It serves only
* as a comparison to {@link lastDigitOfSum}, as it is very
* time and space consuming.
*
* @param f The beginning of the interval (inclusive).
* @param t The end of the interval (inclusive).
* @return The last digit of the sum (byte).
*/
public static byte lastDigitOfBigInt(int f, int t) {
BigInteger ten = BigInteger.valueOf(10);
byte result;
System.out.print("expected:");
if(f == 0) {
result = sumOfFibo(t).subtract(sumOfFibo(f)).mod(ten).byteValue();
System.out.printf("%d\n", result);
return result;
}
result = sumOfFibo(t).subtract(sumOfFibo(f - 1)).mod(ten).byteValue();
System.out.printf("%d\n", result);
return result;
}
/**
* A static method to calculate the last digit of the sum
* of Fibonacci numbers in a given interval <code>[f, t]</code>.
* <p>
* This method is a much more efficient version of
* {@link lastDigitOfBigInt}. It uses the principle that in the
* Fibonacci sequence, the last digits are repeated every 60
* items. The same applies to the sum of the first <code>n</code>
* numbers. It generates two arrays to store the 60 last digits, one
* for the Fibonacci sequence and one for the sums.
*
* @param f The beginning of the interval (inclusive).
* @param t The end of the interval (inclusive).
* @return The last digit of the sum (byte).
*/
public static byte lastDigitOfSum(long f, long t) {
byte[] lastDigit = new byte[60];
byte[] sumLastDigit = new byte[60];
byte resFrom = (byte) (f % 60);
byte resTo = (byte) (t % 60);
byte result;
for(int i = 0; i < 60; i++) {
if(i <= 1) {
lastDigit[i] = (byte) i;
sumLastDigit[i] = (byte) (i + 10);
}
else {
lastDigit[i] = (byte) ((lastDigit[i - 1] + lastDigit[i - 2]) % 10);
sumLastDigit[i] = (byte) ((sumLastDigit[i - 1] + lastDigit[i]) % 100);
if(sumLastDigit[i] < 10)
sumLastDigit[i] = (byte) (sumLastDigit[i] + 10);
}
}
System.out.print("result:");
if(resFrom == 0) {
result = (byte) ((sumLastDigit[resTo] - sumLastDigit[resFrom] % 10) % 10);
System.out.printf("%d\n", result);
System.out.println("----------");
return result;
}
result = (byte) ((sumLastDigit[resTo] - sumLastDigit[resFrom - 1] % 10) % 10);
System.out.printf("%d\n", result);
System.out.println("----------");
return result;
}
}