recently I need to calculate this: $a(n+1)=((a(n-2)+a(n-1)+a(n)) \bmod 10000)$
and get $a(20000000)$ (for example).
I know RecurrenceTable
,but
RecurrenceTable[{a[n + 1] == Mod[a[n] + a[n - 1] + a[n - 2], 10000],
a[1] == 1, a[2] == 1, a[3] == 1}, a, {n, 1, 20000000}] // Last
require a bit of long time.(and extra space)
using RSolve
is also useless.
Nest
seems only support $a_{n+1}=f(a_n)$
this is also not so useful.
a[1] == 1;
a[2] == 1;
a[3] == 1;
a[n_] := a[n] = Mod[a[n - 3] + a[n - 1] + a[n - 2], 100000];
a[20000000]
So is there a way which can Space Complexity be $O(1)$ as well as fast? (I know matrix exponentiating by squaring,but it seems hard to write.)