Given the function fun(x) as defined as:
fun(0) = 24, fun(1) = 42
fun(x) = x * fun(x - 1) + fun(x - 2)
Let F equal fun(1000) . What are the last 3 digits of F ?
Details and assumptions
Try writing your program so that it takes only 1 second to run.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
The last three digits of F are 024 .
Solution 1: Use iteration to compute each element in the sequence until you reach the 1000th one. Note: You only have to keep track of the last 3 digits of each element, because the higher order digits do not affect the results of multiplication and addition on the last 3 digits.
Solution 2 : Use recursion with memoization - storing return values in a lookup table so they don't have to be re-computed.