find exponential function given two points - solver

I know I can solve equations in maxima using the commands below but how do I solve for two different equations.
kill(all);
r:.5; a:1; b:-5.7; theta:theta; solve(a*e^(b*theta)=r,theta);
tex(''%);
I'm trying to get the equation of a exponetial function given two points. How do I go about doing this. Example point 1 is at (2,12) and point 2 is at (8,768)

Maxima needs some help to solve this problem, but it can be done. Start by expression the problem data.
(%i1) [x1,y1]:[2,12];
(%o1) [2, 12]
(%i2) [x2,y2]:[8, 768];
(%o2) [8, 768]
(%i3) eq1:y1 = a*exp(b*x1);
2 b
(%o3) 12 = a %e
(%i4) eq2:y2 = a*exp(b*x2);
8 b
(%o4) 768 = a %e
Now try to solve eq1 and eq2 for a and b.
(%i5) solve([eq1, eq2], [a, b]);
(%o5) []
Hmm, that's unsatisfying. I'm guessing that Maxima could solve it if we take logarithms which make it linear.
(%i6) log([eq1, eq2]);
2 b 8 b
(%o6) [log(12) = log(a %e ), log(768) = log(a %e )]
Apply the logexpand flag to simplify. Note that % means the previous result.
(%i7) %, logexpand;
2 b 8 b
(%o7) [log(12) = log(a %e ), log(768) = log(a %e )]
Hmm, that didn't do it. There are different forms of logexpand, try another.
(%i8) %, logexpand=super;
(%o8) [log(12) = 2 b + log(a), log(768) = 8 b + log(a)]
OK, good. Now try to solve it.
(%i9) solve (%, [a, b]);
(%o9) []
Well, that still didn't work. But I see it's linear in log(a) so solve for that instead.
(%i10) solve (%o8, [log(a), b]);
4 log(12) - log(768) log(12) - log(768)
(%o10) [[log(a) = --------------------, b = - ------------------]]
3 6
Great. Here are the numerical values:
(%i11) float (%);
(%o11) [[log(a) = 1.09861228866811, b = 0.6931471805599454]]
I'll try to simplify the exact values.
(%i12) %o10, logexpand=super;
4 log(12) - log(768) log(12) - log(768)
(%o12) [[log(a) = --------------------, b = - ------------------]]
3 6
Hmm, that didn't work. I'll try another function:
(%i13) radcan(%);
(%o13) [[log(a) = log(3), b = log(2)]]
OK, that was a little bit of work, but anyway maybe it helps.

Related

Number of ways to remove items from box

I encountered the following algorithmic question which has strict constraints on runtime (<10s and no large memory footprint) and I am stumped. My approach fails half the test cases.
Question
A box contains a number of items that can only br removed 1 at a time or 3 at a time.
How many ways can the box be emptied? the answer can be very large so return it as modulo of 10^9+7.
for example,there are n=7 items initially.They can be removed nine ways,as follows:
1.(1,1,1,1,1,1,1)
2.(1.1.1.1.3)
3.(1,1,1,3,1)
4.(1,1,3,1,1)
5.(1,3,1,1,1)
6.(3,1,1,1,1)
7.(1,3,3)
8.(3,1,3)
9.(3,3,1)
So the function should return 9.
Function Description:
Your function must take in a parameter, n for the number of items, and return an integer which denotes the number of ways to empty the box.
Constraints: 1<=n<=10^8
Sample cases :
Input: 1
Sample OutPut: 1
Explanation: There is only 1 way to remove 1 item. Answer=(1%1000000007)=1
Input: 7
Sample OutPut: 9
There is only 9 ways to remove 7 items
My Approach
This leads to a standard recurrence relation where f(n) = f(n-3) + f(n-1) for n > 2, so i did it as follows
def memoized_number_of_ways(dic, n):
if n not in dic:
dic[n] = memoized_number_of_ways(dic, n-3) + memoized_number_of_ways(dic, n-1)
return dic[n]
def numberOfWays(n):
# Write your code here
memoize = {1:1,2:1,3:2}
import math
ans = memoized_number_of_ways(memoize,n)
return ans % (math.pow(10,9) + 7)
However this fails on any case where n > 10**2. How can you do this problem while accomodating n up to 10^8 and in less than 10s with not much memory?
Just write your recurrence using matrices (pardon my way of writing matrices, StackOverflow doesn't allow LaTeX).
[f(n) ] = [1 0 1] [f(n-1) ]
[f(n-1)] = [1 0 0] [f(n-2) ]
[f(n-2)] = [0 1 0] [f(n-3) ]
Now all you have to do is raise a 3x3 matrix (modulo fixed constant) to power n (or n-3 or something like that, depending on your "base case column vector", fill in the details), and then multiply it by a "base case column vector". This can be done in time O(logn).
PS: You may want to lookup matrix exponentiation.
Three solutions, fastest takes about 31 μs for n=108 on tio.run (which has medium-fast computers).
A matrix power solution like described by advocateofnone that takes about 1 millisecond (Try it online!):
import numpy as np
from time import time
class ModInt:
def __init__(self, x):
self.x = x % (10**9 + 7)
def __add__(a, b):
return ModInt(a.x + b.x)
def __mul__(a, b):
return ModInt(a.x * b.x)
def __str__(self):
return str(self.x)
def solve(n):
O = ModInt(0)
I = ModInt(1)
A = np.matrix([[O,I,O], [O,O,I], [I,O,I]])
return (A**n)[2,2]
for _ in range(3):
t0 = time()
print(solve(10**8), time() - t0)
Output (result and time in seconds for n=108, three attempts):
109786077 0.0010712146759033203
109786077 0.0010180473327636719
109786077 0.0009677410125732422
Another, taking about 0.5 milliseconds (Try it online!):
import numpy as np
from time import time
def solve(n):
A = np.matrix([[0,1,0], [0,0,1], [1,0,1]])
power = 1
mod = 10**9 + 7
while n:
if n % 2:
power = power * A % mod
A = A**2 % mod
n //= 2
return power[2,2]
for _ in range(3):
t0 = time()
print(solve(10**8), time() - t0)
One based on #rici's solution in the comments, takes about 31 μs (Try it online!):
from timeit import repeat
def solve(n):
m = 10**9 + 7
def abc(n):
if n == 0:
return 0, 1, 0
a, b, c = abc(n // 2)
d = a + c
e = b + d
A = 2*a*b + c*c
C = 2*b*c + d*d
E = 2*c*d + e*e
D = A + C
B = E - D
if n % 2:
A, B, C = B, C, D
return A%m, B%m, C%m
return sum(abc(n)) % m
n = 10**8
print(solve(n))
for _ in range(3):
t = min(repeat(lambda: solve(n), 'gc.enable()', number=1000)) / 1000
print('%.1f μs' % (t * 1e6))
Explanation: Looking at the matrix powers from my previous solutions, I noticed they only actually contain five different values, and they're consecutive result numbers from our desired sequence. For example, A**19 is:
[[277 189 406]
[406 277 595]
[595 406 872]]
I gave them names in increasing order:
| b a c |
| c b d |
| d c e |
Squaring that matrix results in a matrix for larger n, with entries A/B/C/D/E. And if you square the above matrix, you'll find the relationships A = 2*a*b + c*c etc.
My helper function abc(n) computes the entries a/b/c of the n-th matrix power. For n=0, that's the identity matrix, so my a/b/c are 0/1/0 there. And in the end I, return the e-value (computed as e=b+d=a+b+c).
Here's a simple iterative O(n) time / O(1) space solution whose optimized version takes 6 seconds on a medium-fast machine (unoptimized takes 15 seconds there).
Unoptimized (Try it online!):
def solve(n):
mod = 10**9 + 7
a = b = c = 1
for _ in range(n):
a, b, c = b, c, (a+c) % mod
return a
print(solve(7))
print(solve(10**8))
Optimized (Try it online!):
def solve(n):
mod = 10**9 + 7
a = b = c = 1
for _ in range(n // 300):
for _ in range(100):
a += c
b += a
c += b
a %= mod
b %= mod
c %= mod
for _ in range(n % 300):
a, b, c = b, c, (a+c) % mod
return a
Your solution is on the right track and the bug is not related to your algorithm (Yay).
The problem is when you are performing operations on some big numbers you lose precision. Notice that you can apply the mod 10 ** 9 + 7 along your code since addition is not affected by it. By doing so you keep all your numbers below a certain size and you will not have any floating point precision errors:
import math
def memoized_number_of_ways(dic, n):
if n not in dic:
dic[n] = (memoized_number_of_ways(dic, n-3) + memoized_number_of_ways(dic, n-1)) % (math.pow(10,9) + 7)
return dic[n]
def numberOfWays(n):
memoize = {1:1,2:1,3:2}
ans = memoized_number_of_ways(memoize,n)
return ans
Note that for you to be able to answer the question for n > 1000 you need to solve this recursion error problem.
Unfortunately even a very efficient solution (hint: you don't really need more than 3 items in your dict at any moment) will not solve the question for n ~ 10 ** 9 under a second. And you will need to find another way - a great option is the second answer here :)

programming mathematica to find a specific perfect square number set

A colleague of mine gave the following question to his C programming class which i found very interesting. It can easily be done in any programming language and immediately i thought wolfram.
The question is this:
The number 25 is a unique perfect square, If we increase each digit by one, it becomes 36 which is also a a perfect square!
write a program to find another set of numbers with the same qualities.
I am sure this can be easily done in mathematica.
Can someone explain how i can do this in mathematica. please note the reason of the question is just an excuse to get me into mathematica programming of which i know nothing.
thanks to all.
A more functional solution.
Table[x^2, {x, 1, 100}] // Select[IntegerQ[Sqrt[FromDigits[IntegerDigits[#] + 1]]] &]
How should the digit 9 be handled?
IntegerDigits[19]
(* {1, 9} *)
IntegerDigits[19] + 1
(* {2, 10} *)
FromDigits[IntegerDigits[19] + 1]
(* 30 *)
Should the +1 carry so the resulting number is 20 rather than 30?
You can easily expand this to any base and you only need to know how long the number is in a given base. What I mean is the following. Assume in base 10, the number 25. To check the premise, we need to add 11. But 11 is nothing more than:
25 + 11
= 25 + 10^1 + 10^0
= 25 + (10^2-1)/(10-1)
= 36 = 6^2
imagine now the number 72 × 72 = 5184, but represented in base 3 (518410 = 210100003). Doing now the computation in base 3, you get
21010000 + 11111111
= 21010000 + 3^7 + 3^6 + 3^5 + 3^4 + 3^3 + 3^2 + 3^1 + 3^0
= 21010000 + (3^8-1)/(3-1)
= 102121111 = 10102^2
where 1021211113 = 846410 = 9210 × 9210.
As you notice, all you need to do is add the number (bn - 1)/(b-1) to the number and check if it is a square. Here n, represents the total amount of digits of the number x in base b.
With a simple lookuptable, you do this in Mathematica as:
b = 10
x = Table[n^2, {n, 1, 1000}];
Select[x, MemberQ[x, # + (b^IntegerLength[#, b] - 1)/(b - 1)] &];
{25, 289, 2025, 13225, 100489, 198025, 319225, 466489}
and the full list for base 2 till base 10 is then:
Table[Select[x, MemberQ[x, # + (b^IntegerLength[#, b] - 1)/(b - 1)] &], {b, 2, 10}]
Instead of throwing you into the ocean, lets help you paddle around in the shallow end of the pool first.
n=1;
While[n<100,
d=IntegerDigits[n];(*get the list of digits making up n*)
newd=d+1;(*add one to every element of the list giving a new list*)
newn=FromDigits[newd];(*turn that new list of digits back into a number*)
If[IntegerQ[Sqrt[newn]],Print[{n,newn}]];
n++
]
That doesn't only look at square values of n, but it might give you the hint needed about how to increment the digits and test for a square result.
There are always at least a dozen different ways of doing anything in Mathematica and some of the culture revolves around making the programs as short, and potentially cryptic, as possible. You can start picking that up later. Simplicity seems better when getting started with a new language.
I hope you have fun.
find[from_, to_] := Module[{a, b, c, d, e},
a = Range[from, to];
b = a^2;
c = IntegerDigits[b];
(*Add 1's to the digits of the square,
except where the square contains a 9*)
d = MapThread[
If[MemberQ[#2, 9], Null,
#1 + FromDigits[ConstantArray[1, Length[#2]]]] &,
{b, c}];
(*Find the positions where the square roots are integers*)
e = Position[Sqrt[d], _?IntegerQ, {1}];
Extract[a, e]]
find[1, 1000000]
{5, 45, 115, 2205, 245795, 455645}
For example
Sqrt[45^2 + 1111]
56
and
Sqrt[455645^2 + 111111111111]
564556

bsxfun implementation in matrix multiplication

As always trying to learn more from you, I was hoping I could receive some help with the following code.
I need to accomplish the following:
1) I have a vector:
x = [1 2 3 4 5 6 7 8 9 10 11 12]
2) and a matrix:
A =[11 14 1
5 8 18
10 8 19
13 20 16]
I need to be able to multiply each value from x with every value of A, this means:
new_matrix = [1* A
2* A
3* A
...
12* A]
This will give me this new_matrix of size (12*m x n) assuming A (mxn). And in this case (12*4x3)
How can I do this using bsxfun from matlab? and, would this method be faster than a for-loop?
Regarding my for-loop, I need some help here as well... I am not able to storage each "new_matrix" as the loop runs :(
for i=x
new_matrix = A.*x(i)
end
Thanks in advance!!
EDIT: After the solutions where given
First solution
clear all
clc
x=1:0.1:50;
A = rand(1000,1000);
tic
val = bsxfun(#times,A,permute(x,[3 1 2]));
out = reshape(permute(val,[1 3 2]),size(val,1)*size(val,3),[]);
toc
Output:
Elapsed time is 7.597939 seconds.
Second solution
clear all
clc
x=1:0.1:50;
A = rand(1000,1000);
tic
Ps = kron(x.',A);
toc
Output:
Elapsed time is 48.445417 seconds.
Send x to the third dimension, so that singleton expansion would come into effect when bsxfun is used for multiplication with A, extending the product result to the third dimension. Then, perform the bsxfun multiplication -
val = bsxfun(#times,A,permute(x,[3 1 2]))
Now, val is a 3D matrix and the desired output is expected to be a 2D matrix concatenated along the columns through the third dimension. This is achieved below -
out = reshape(permute(val,[1 3 2]),size(val,1)*size(val,3),[])
Hope that made sense! Spread the bsxfun word around! woo!! :)
The kron function does exactly that:
kron(x.',A)
Here is my benchmark of the methods mentioned so far, along with a few additions of my own:
function [t,v] = testMatMult()
% data
%{
x = [1 2 3 4 5 6 7 8 9 10 11 12];
A = [11 14 1; 5 8 18; 10 8 19; 13 20 16];
%}
x = 1:50;
A = randi(100, [1000,1000]);
% functions to test
fcns = {
#() func1_repmat(A,x)
#() func2_bsxfun_3rd_dim(A,x)
#() func2_forloop_3rd_dim(A,x)
#() func3_kron(A,x)
#() func4_forloop_matrix(A,x)
#() func5_forloop_cell(A,x)
#() func6_arrayfun(A,x)
};
% timeit
t = cellfun(#timeit, fcns, 'UniformOutput',true);
% check results
v = cellfun(#feval, fcns, 'UniformOutput',false);
isequal(v{:})
%for i=2:numel(v), assert(norm(v{1}-v{2}) < 1e-9), end
end
% Amro
function B = func1_repmat(A,x)
B = repmat(x, size(A,1), 1);
B = bsxfun(#times, B(:), repmat(A,numel(x),1));
end
% Divakar
function B = func2_bsxfun_3rd_dim(A,x)
B = bsxfun(#times, A, permute(x, [3 1 2]));
B = reshape(permute(B, [1 3 2]), [], size(A,2));
end
% Vissenbot
function B = func2_forloop_3rd_dim(A,x)
B = zeros([size(A) numel(x)], 'like',A);
for i=1:numel(x)
B(:,:,i) = x(i) .* A;
end
B = reshape(permute(B, [1 3 2]), [], size(A,2));
end
% Luis Mendo
function B = func3_kron(A,x)
B = kron(x(:), A);
end
% SergioHaram & TheMinion
function B = func4_forloop_matrix(A,x)
[m,n] = size(A);
p = numel(x);
B = zeros(m*p,n, 'like',A);
for i=1:numel(x)
B((i-1)*m+1:i*m,:) = x(i) .* A;
end
end
% Amro
function B = func5_forloop_cell(A,x)
B = cell(numel(x),1);
for i=1:numel(x)
B{i} = x(i) .* A;
end
B = cell2mat(B);
%B = vertcat(B{:});
end
% Amro
function B = func6_arrayfun(A,x)
B = cell2mat(arrayfun(#(xx) xx.*A, x(:), 'UniformOutput',false));
end
The results on my machine:
>> t
t =
0.1650 %# repmat (Amro)
0.2915 %# bsxfun in the 3rd dimension (Divakar)
0.4200 %# for-loop in the 3rd dim (Vissenbot)
0.1284 %# kron (Luis Mendo)
0.2997 %# for-loop with indexing (SergioHaram & TheMinion)
0.5160 %# for-loop with cell array (Amro)
0.4854 %# arrayfun (Amro)
(Those timings can slightly change between different runs, but this should give us an idea how the methods compare)
Note that some of these methods are going to cause out-of-memory errors for larger inputs (for example my solution based on repmat can easily run out of memory). Others will get significantly slower for larger sizes but won't error due to exhausted memory (the kron solution for instance).
I think that the bsxfun method func2_bsxfun_3rd_dim or the straightforward for-loop func4_forloop_matrix (thanks to MATLAB JIT) are the best solutions in this case.
Of course you can change the above benchmark parameters (size of x and A) and draw your own conclusions :)
Just to add an alternative, you maybe can use cellfun to achieve what you want. Here's an example (slightly modified from yours):
x = randi(2, 5, 3)-1;
a = randi(3,3);
%// bsxfun 3D (As implemented in the accepted solution)
val = bsxfun(#and, a, permute(x', [3 1 2])); %//'
out = reshape(permute(val,[1 3 2]),size(val,1)*size(val,3),[]);
%// cellfun (My solution)
val2 = cellfun(#(z) bsxfun(#and, a, z), num2cell(x, 2), 'UniformOutput', false);
out2 = cell2mat(val2); % or use cat(3, val2{:}) to get a 3D matrix equivalent to val and then permute/reshape like for out
%// compare
disp(nnz(out ~= out2));
Both give the same exact result.
For more infos and tricks using cellfun, see: http://matlabgeeks.com/tips-tutorials/computation-using-cellfun/
And also this: https://stackoverflow.com/a/1746422/1121352
If your vector x is of lenght = 12 and your matrix of size 3x4, I don't think that using one or the other would change much in term of time. If you are working with higher size matrix and vector, now that might become an issue.
So first of all, we want to multiply a vector with a matrix. In the for-loop method, that would give something like that :
s = size(A);
new_matrix(s(1),s(2),numel(x)) = zeros; %This is for pre-allocating. If you have a big vector or matrix, this will help a lot time efficiently.
for i = 1:numel(x)
new_matrix(:,:,i)= A.*x(i)
end
This will give you 3D matrix, with each 3rd dimension being a result of your multiplication. If this is not what you are looking for, I'll be adding another solution which might be more time efficient with bigger matrixes and vectors.

Pseudo number generation

Following is text from Data structure and algorithm analysis by Mark Allen Wessis.
Following x(i+1) should be read as x subscript of i+1, and x(i) should be
read as x subscript i.
x(i + 1) = (a*x(i))mod m.
It is also common to return a random real number in the open interval
(0, 1) (0 and 1 are not possible values); this can be done by
dividing by m. From this, a random number in any closed interval [a,
b] can be computed by normalizing.
The problem with this routine is that the multiplication could
overflow; although this is not an error, it affects the result and
thus the pseudo-randomness. Schrage gave a procedure in which all of
the calculations can be done on a 32-bit machine without overflow. We
compute the quotient and remainder of m/a and define these as q and
r, respectively.
In our case for M=2,147,483,647 A =48,271, q = 127,773, r = 2,836, and r < q.
We have
x(i + 1) = (a*x(i))mod m.---------------------------> Eq 1.
= ax(i) - m (floorof(ax(i)/m)).------------> Eq 2
Also author is mentioning about:
x(i) = q(floor of(x(i)/q)) + (x(i) mod Q).--->Eq 3
My question
what does author mean by random number is computed by normalizing?
How author came with Eq 2 from Eq 1?
How author came with Eq 3?
Normalizing means if you have X ∈ [0,1] and you need to get Y ∈ [a, b] you can compute
Y = a + X * (b - a)
EDIT:
2. Let's suppose
a = 3, x = 5, m = 9
Then we have
where [ax/m] means an integer part.
So we have 15 = [ax/m]*m + 6
We need to get 6. 15 - [ax/m]*m = 6 => ax - [ax/m]*m = 6 => x(i+1) = ax(i) - [ax(i)/m]*m
If you have a random number in the range [0,1], you can get a number in the range [2,5] (for example) by multiplying by 3 and adding 2.

Google Code Jam 2008: Round 1A Question 3

At Google Code Jam 2008 round 1A, there is problem:
Calculate last three digits before the
decimal point for the number
(3+sqrt(5))^n
n can be big number up to 1000000.
For example: if n = 2 then (3+sqrt(5))^2 = 27.4164079... answer is 027.
For n = 3: (3+sqrt(5))^3 = 3935.73982... answer is 935.
One of the solution is to create matrix M 2x2 : [[0, 1], [-4, 6]] than calculate matrix P = M^n, Where calculation preformed by modulo 1000.
and the result is (6*P[0,0] + 28*P[0,1] - 1) mod 1000.
Who can explain me this solution?
I'll present a method to solve this problem without even understanding the solution.
Assuming that you are familiar with the fibonacci numbers:
ghci> let fib = 0 : 1 : zipWith (+) fib (tail fib)
ghci> take 16 fib
[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610]
And are also familiar with its closed form expression:
ghci> let calcFib i = round (((1 + sqrt 5) / 2) ^ i / sqrt 5)
ghci> map calcFib [0..15]
[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610]
And you notice the similarity of ((1 + sqrt 5) / 2)n and (3 + sqrt 5)n.
From here one can guess that there is probably a series similar to fibonacci to calculate this.
But what series? So you calculate the first few items:
ghci> let calcThing i = floor ((3 + sqrt 5) ^ i)
ghci> map calcThing [0..5]
[1,5,27,143,751,3935]
Guessing that the formula is of the form:
thingn = a*thingn-1 + b*thingn-2
We have:
27 = a*5 + b*1
143 = a*27 + b*5
We solve the linear equations set and get:
thingn = 4*thingn-1 + 7*thingn-2 (a = 4, b = 7)
We check:
ghci> let thing = 1 : 5 : zipWith (+) (map (* 4) (tail thing)) (map (* 7) thing)
ghci> take 10 thing
[1,5,27,143,761,4045,21507,114343,607921,3232085]
ghci> map calcThing [0..9]
[1,5,27,143,751,3935,20607,107903,564991,2958335]
Then we find out that sadly this does not compute our function. But then we get cheered by the fact that it gets the right-most digit right. Not understanding why, but encouraged by this fact, we try to something similar. To find the parameters for a modified formula:
thingn = a*thingn-1 + b*thingn-2 + c
We then arrive at:
thingn = 6*thingn-1 - 4*thingn-2 + 1
We check it:
ghci> let thing =
1 : 5 : map (+1) (zipWith (+)
(map (*6) (tail thing))
(map (* negate 4) thing))
ghci> take 16 thing == map calcThing [0..15]
True
Just to give an answer to a very old question:
Thanks to yairchu i've got the idea to reread the prove of Binet's formula on the wikipedia page. It's there not really that clear, but we can work with it.
We see on the wikipedia page there is a closed form with 'computation by rounding': Fn = ⌊φ/√5⌋n.
If we could replace the φ/√5 with 3 + √5 (call the latter x). We could compute the floor of xn fairly easily, especially mod 1000, by finding the nth term in our freshly constructed sequence (this is the analogon of F (later we will call this analogon U)).
What sequence are we looking for? Well, we'll try following the prove for the Binet's formula. We need a quadratic equation with x as a root. Let's say x2 = 6 x-4 this one has roots x and y := 3 - √5. The handy part is now:
Define Un (for every a and b) such:
Un = a xn + b yn
by definition of x and y you can see that
Un = 6 Un-1 - 4 Un-2
Now we can choose a and b freely. We need Un to be integers so I propose choosing a=b=1. Now is U0 = 2, U1 = 6, U2 = 28...
We still need to get our 'computation by rounding'. You can see that yn < 1 for every n (because y ≅ 0.76 < 1) so Un = xn + yn = ⌈xn⌉.
If we can compute Un we can find ⌊xn⌋, just subtract 1.
We could compute Un by it's recursive formula but that would require O(n) computation time. We can do better!
For computing such a recursive formula we can use matrices:
⌈ 0 1⌉ ⌈ U(n-1) ⌉ ⌈ U(n) ⌉
⌊-4 6⌋ ⌊ U(n) ⌋ = ⌊U(n+1)⌋
Call this matrix M. Now does M*(U(1), U(2)) compute (U(2), U(3)).
Now we can compute P = Mn-1 (notice that I use one less than n, you can see that this is right if you test the small cases: n=0, n=1, n=2) P*(6,28) gives us now the nth and (n+1)th term of our sequence so:
(P*(6,28))0 - 1 = ⌊xn⌋
Now we can take everything mod 1000 (this is simplifying the calculations (a lot)) and we get the desired result in computation time O(log(n)) (or even better with the computational wonders of powers of matrices (over a cyclic finite field)). This explains the very weird looking solution, I guess.
I don't know how to explain that, but the auther of the problem have compose this analysis.

Resources