Problem 3095. Return fibonacci sequence do not use loop and condition version 2
Calculate the nth Fibonacci number,return sequence
Given n, return f where f = fib(n) and f(1) = 1, f(2) = 1, f(3) = 2, ...
Examples:
Input n = 2 : 5 Output f is [1 2 3 5] Input n = 7 : 10 Output f is [13 21 34 55]
but, loop and conditional statement is forbidden
Solution Stats
Problem Comments
-
8 Comments
For some reason, I keep getting test suite errors:
L 3 (C 29-33): Invalid syntax at 'error'. Possibly, a ), }, or ] is missing. L 3 (C 29-33): Invalid syntax at 'error'. Possibly, a ), }, or ] is missing.
Hi M.C., in this problem some of the functions are banned by author. This cause "standard" solutions known from previous fibonacci numbers related problems to fail. Examine your code against functions and operators listed in 1st test.
Thanks Jan. I would have expected to receive the message "No fancy functions!" if was using something I shouldn't be. Here is my solution (which works in my matlab and doesn't appear to have anything "illegal"):
function y = fib(x)
[v,d] = eig([1 1; 1 0]);
M = v*bsxfun(@times, [d(1,1).^(x-1);d(2,2).^(x-1)], v\[1;1]);
y = uint8(M(2,:));
end
Ok, I see - I'm not allowed to use ^. The list of banned functions seems much more restrictive than not using a for or a conditional. Back to the drawing board!
have fun!
The problem has way more restrictions than described. We cannot even use round, fix, ceil or floor, which is a pain (almost prohibiting approx. methods). In such cases, It is probably better that the author states what is the method that he is looking for.
Btw, stating which method or function to use only makes a problem easier for people that already know it, which means that they are not learning something new. And that is still true regardless of whether people know it or not. Someone that know the required method will try everything they know and will solve the problem, while someone that doesn't know the required method will also try everything, but will be stuck after emptying their options (and may do some research, give up, or cheat*). If we want to estimulate learning (avoiding the unwanted alternatives), revealing the method may help so that people don't give up. Of course, it is also possible to give some tips or a link.
* Cheating is the prefered choice at Cody and probably also in real life.
I totally agree with Rafael
Solution Comments
Show commentsProblem Recent Solvers54
Suggested Problems
-
480 Solvers
-
324 Solvers
-
Is this triangle right-angled?
5855 Solvers
-
Back to basics - mean of corner elements of a matrix
416 Solvers
-
Integer Sequence - II : New Fibonacci
541 Solvers
More from this Author17
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!