Problem 44311. Number of Even Elements in Fibonacci Sequence
Solution Stats
Problem Comments

5 Comments
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.
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.
You dont need generate all d fibonacci number to find the numbers of even.Think like that 2 odd fibonacci numbers will make a even number
like 1 1 make 2, and 3 5 make 8.So 3 numbers in group will have a even number
Not a hard problem at all lol
Solution Comments

3 Comments
Hahaha funny, proof that math is the shortcut
Good solution
Math is a hack.

1 Comment
@Kashfia Rahman Oyshei: your comment was deleted, as it revealed a potential solution. Regarding your solution failing at large values of d, look at other comments to the problem for hints.

1 Comment
it is working in my Matlab

1 Comment
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.

1 Comment
I think there is a bug in the assertions after 4th one

3 Comments
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.

4 Comments
My code works to d=50 and fails on the higher values in the test suite. I think it is a hardwarelimited 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 32bit 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.
me too.

1 Comment
Not a true solution. Would fail if Test Suite were expanded.

1 Comment
Not a general solution. Will fail if Test Suite expanded.
Problem Recent Solvers949
Suggested Problems

Sum of diagonal of a square matrix
1453 Solvers

Find out sum and carry of Binary adder
854 Solvers

356 Solvers

377 Solvers

152 Solvers
More from this Author76
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!