topic
Given a sequence of 1, 1, 1, 3, 5, 9, 17, ..., starting from the 4th item, each item is the sum of the first 3 items. Find the last 4 digits of the 20190324 item.
Parsing
**The breakthrough point of this question is: **How to find the number behind a number?
If it is one, then we can get: x%10.
If it is two? x%100???-yes x%100
and so on-if we find the last 4 digits of a number- x% 10000
and then according to the question, we can get this kind of Fibonacci sequence, so according to We can find a similar method.
Code:
public static void main(String[] args) {
int n = 20190324;
Long resFib = fib(n);
System.out.println(resFib);
}
static public long fib(int n) {
if (n <= 3) {
return 1;
}
int a = 1, b = 1, c = 1, r = 4;
while (r <= n) {
int sum = a + b + c;
a = b % 10000;
b = c % 10000;
c = sum % 10000;
r++;
}
return c;
}
Answer: 4659
Reflection
I didn't understand what to ask for the last four digits. I only learned to ask for the last digit (reversing an integer).
Now I have learned it! ! !
end.