This one is interesting, using pythons idea that integers can be considered true in booleans operations like not (except zero which is false) and I guess that not(none) is true?
I should probably just write my own versions of some of these, but it’s kind of fun to comment or ask questions on some of the other code and languages to learn something.
I also don’t want to just write the same but possibly worse version of someone else’s code, possibly in the same or another language
You can include Math to put all the methods of Math into your current module.
That’s probably fine for these challenges. Trying to understand the exact implications (or why you can’t get just one method like in Python) will take you down a deep ruby rabbit hole though.
Then I want to compute C_n, the sum of every third term starting from F_2, since that’s supposed to be all the even ones?
Except how do I know what n is here? Would I need a bound on when F_{3n+2}< N where N is given like in the problem as 4e6?
So if one solves the three coupled equations and one knows the right bound then one does C_n for that n?
Edit: Actually if I want C_n it looks like from equation 1 and 3 it’s just (f(3n+4)-1)/2 ? So I still need the right n and some way to compute f like the one @antonTobi mentioned maybe with powers and rounding of the golden ratio?
I was just thinking about this, but if we do a binary search where we have to evaluate f(n) at every step, isn’t that O(log n log n)? I guess we get around this by saving earlier computed values for f(n)?
So I don’t know what I’m supposed to take out of this, that is the key points. Possibly because I’m jumping between posts a bit.
I try do a fast exponential on matrix multiplication to get the f(n)’s that I want quickly?
I’m not really proficient with Big Oh notation and algorithms, but computing f(n)‘s by repeated squaring in matrix multiplication is faster than just generating a list of the f(n)’s (by ordinary addition)?
Is that in this particular case because I want every third term only or generally?
Yes, O(log n) is much faster than O(n) for large n.
Because f(n) is only the single Fibonacci terms, we want the sum of every third term up to a certain point, which is given by C_n (which happens to be expressible in terms of f(n)).
I meant more computationally, to figure out if an algorithm has a certain O(…). I know one can discard slower growing terms and things to simplify.
Right right, so now C_n might only need to be computed from one term, and now one just has to figure out what n is, which either has a nice closed form or it needs to be found in some way without computing more Fibonacci terms in the process right? Or not many anyway.
Edit:
I guess this is what this is saying
I just hone in by checking if f(3n+2) is above or below the given number, trying to halve the number of options to check at each point?
So what we want is to find the largest f(n) below 4 million.
We can square to get f(1), f(2), f(4), f(8)… etc. until we surpass 4 million with f(2^(k+1)).
Then we go back one step to f(2^k). Now we can compute f(2^k + 1), f(2^k + 2), f(2^k + 4)… etc. (reusing the f(2^k) values we just computed) until we again surpass 4 million.
Repeat as necessary.
But again, this feels a little O(log n log n)-ish to me…
@Glunkolin could you explain a little more how to locate n by binary search?
I meant the former But also I was just throwing a second log in there without too much thought (hence the -ish), I’m not sure what the actual complexity there is.
I’m also hazy on some of the details of Big Oh. I generally assume that a single multiplication step is O(1), but of course that doesn’t hold when the numbers get really big.