Cody

Solution 1323595

Submitted on 1 Nov 2017 by Elmar Zander
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
memfib = memoize_this(@fib); [seq, n1] = fib(1, memfib); assert(n1 == 1); [seq, n2] = fib(20, memfib); assert(n2 - n1 == 19); [seq, n3] = fib(100, memfib); assert(n3 - n2 == 81); function [seq, n] = fib(n, memfib) persistent num if isempty(num) num = 1; else num = num + 1; end if n < 3 seq = ones(1, n); else seq = memfib(n-1, memfib); seq = [seq, seq(end-1) + seq(end)]; end n = num; end

memfcn = function_handle with value: @memoize_this/foo ans = 1 1 ans = 1 1 2 ans = 1 1 2 3 ans = 1 1 2 3 5 ans = 1 1 2 3 5 8 ans = 1 1 2 3 5 8 13 ans = 1 1 2 3 5 8 13 21 ans = 1 1 2 3 5 8 13 21 34 ans = 1 1 2 3 5 8 13 21 34 55 ans = 1 1 2 3 5 8 13 21 34 55 89 ans = 1 1 2 3 5 8 13 21 34 55 89 144 ans = 1 1 2 3 5 8 13 21 34 55 89 144 233 ans = 1 1 2 3 5 8 13 21 34 55 89 144 233 377 ans = 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 ans = 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 ans = Columns 1 through 15 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Columns 16 through 17 987 1597 ans = Columns 1 through 15 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Columns 16 through 18 987 1597 2584 ans = Columns 1 through 15 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Columns 16 through 19 987 1597 2584 4181 ans = Columns 1 through 15 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Columns 16 through 19 987 1597 2584 4181 ans = Columns 1 through 15 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Columns 16 through 20 987 1597 2584 4181 6765 ans = Columns 1 through 15 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Columns 16 through 21 987 1597 2584 4181 6765 10946 ans = Columns 1 through 15 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Columns 16 through 22 987 1597 2584 4181 6765 10946 17711 ans = Columns 1 through 15 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Columns 16 through 23 987 1597 2584 4181 6765 10946 17711 28657 ans = Columns 1 through 15 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Columns 16 through 24 987 1597 2584 4181 6765 10946 17711 28657 46368 ans = Columns 1 through 15 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Columns 16 through 25 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 ans = Columns 1 through 15 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Columns 16 through 26 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 ans = Columns 1 through 15 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Columns 16 through 27 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 ans = Columns 1 through 15 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Columns 16 through 28 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 ans = Columns 1 through 15 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Columns 16 through 29 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 ans = Columns 1 through 15 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Columns 16 through 30 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 ans = Columns 1 through 15 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Columns 16 through 30 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 Column 31 1346269 ans = Columns 1 through 15 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Columns 16 through 30 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 Columns 31 through 32 1346269 2178309 ans = Columns 1 through 15 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Columns 16 through 30 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 Columns 31 through 33 1346269 2178309 3524578 ans = Columns 1 through 15 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Columns 16 through 30 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 Columns 31 through 34 1346269 2178309 3524578 5702887 ans = Columns 1 through 15 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Columns 16 through 30 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 Columns 31 through 35 1346269 2178309 3524578 5702887 9227465 ans = Columns 1 through 15 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Columns 16 through 30 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 Columns 31 through 36 1346269 2178309 3524578 5702887 9227465 14930352 ans = Columns 1 through 15 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Columns 16 through 30 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 Columns 31 through 37 1346269 2178309 3524578 5702887 9227465 14930352 24157817 ans = Columns 1 through 15 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Columns 16 through 30 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 Co...