Big number is a problem. Instead we can find the regular pattern in which those even and odd numbers appear. Then everything becomes simple and easy.
i have the program on my matlab it work normaly but cody does not accepet the code don't know why?????
function y = evenFibo(n)
y(1) = 1;y(2) = 1;
for i = 3:n
y(i) = y(i-1) + y(i-2);
end
e = y;
E = mod(e,2) == 0;
evennumbers = e(E);
y = length(evennumbers);
if n > 2
fprintf('The even Fibo number is: %d',y)
end
end
Gorgeously simple once you figure it out
hint: copy and paste the first few test suites into your MATLAB console and try plotting it with ':o'
My solution is working in my local computer but is not consistent with Test Suites starting from d = 100. I don't still get the answer.
One liner solution:
function y = evenFibo(d)
y = floor(d/3);
end
it is working in my Matlab
i'm pretty confident of my code but it doesn't work for d>= 100 due to the lack of percision since it surpasses the capability of double and uint64 so you can't test if the number is even.
I think there is a bug in the assertions after 4th one
What's wrong with this?? It's working in matlab for me .
me2
The fibonacci() function is in a toolbox. Only functions in vanilla Matlab are recognized by Cody.
My code works to d=50 and fails on the higher values in the test suite. I think it is a hardware-limited rounding error (?swamping) with very large numbers. When I test eps(fibonacci(100)) on my system, the answer is 6.5 ie my system can not accurately distinguish odd from even at that large a number.
Leo, your theory is correct: The numbers that you're calculating for d>50 are too large to be represented by a 32-bit digit, and won't be calculated correctly for mod(x,2). Think very carefully about the number pattern in the Fibonacci sequence, and see if a pattern emerges.
Indeed, My code works until the d = 50 because it's a large number, so our algorithm is correct we should not worry about it, I think we have succeed in this challenge.
Not a true solution. Would fail if Test Suite were expanded.
Not a general solution. Will fail if Test Suite expanded.
3138 Solvers
Determine the number of odd integers in a vector
408 Solvers
280 Solvers
323 Solvers
261 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!