both solutions are using check for odd/even like this
arrFib.filter(x => x % 2 != 0)
Maybe it’s worth mentioning on the hints page, that when dealing with Fibonacci numbers one could use this property: Every third number of the sequence is even relevant wiki, check out divisibility properties section
in order to do some filtering like this, for example:
Is it actually faster in Javascript? Since JS doesn’t have a proper integer type, that would be surprising to me.
This is a micro optimization I usually skip since 1) it’s less clear to most readers 2) compilers know how to optimize Mod 2 and 3) any language without an optimization pass probably won’t be used for a task where this difference is large enough to be appreciable.
Your solution is relatively heavy, in my personal opinion. I think you can simplify matters if you avoid arrays - the second solution explicitly calls out the fact that dynamically creating arrays is relatively expensive. Here is my effort to simplify while still showing the same idea:
function sumFibs(num) {
let f0 = 0;
let f1 = 1;
let f2 = 1;
let sum = 0;
while (f1 <= num) {
// Update sum
// Note: every third Fibonacci number is even
// and the rest are odd
sum += f1;
if (f2 <= num) sum += f2;
// Compute next three Fibonacci numbers
[f0, f1] = [f1 + f2, f2 + (f1 + f2)];
f2 = f0 + f1;
}
return sum;
}
That said though, I think this would be a cool solution to add to the list of suggested solutions.
Yeah, I saw that. But my main intention was to exploit property of Fibs (every 3rd is even) somehow. In the process, I remembered that I also can avoid division here using booleans. To avoid arrays - wasn’t able to figure it out.
This your idea - to generate fibs in groups of three - I couldn’t figure it out on my own, that’s for sure!
Well, this is solution for intermediate algo scripting section, right? I personally think, at this stage of curriculum students should be able to understand such lines of code. Maybe I am wrong though.
It still feels a bit like magic in the update part, but maybe this rearranging of comments helps?
function sumFibs(num) {
// Every third Fibbonaci number is even
// and the rest are odd
let f0 = 0;
let f1 = 1;
let f2 = 1;
// Generate triples until num is reached
let sum = 0;
while (f1 <= num) {
// Update sum
sum += f1;
if (f2 <= num) sum += f2;
// Compute next three Fibonacci numbers
[f0, f1] = [f1 + f2, f2 + (f1 + f2)];
f2 = f0 + f1;
}
return sum;
}
Could you please elaborate on what is actually happening here? I understand its leveraging destructuring assignment to some extent but that’s about it. If there is an explanation or documentation that explains what computation the syntax above achieves, it would be appreciated.
I mean any explanation/documentation beyond the destructuring assignment lessons within fCC.