Help with Project Euler #2: undef inits, printing, multiplication by juxtaposition, and more

I am sorry but I do not see an edit icon at the top of the thread near my first post.

Ah, maybe it’s one of the options only unlocked with more time spent on the forum, my mistake. Seems like someone else with the option has chipped in already, though.

1 Like

Just for completeness, this is the version which uses Fibonacci formula to obtain closed form for this question (and it’s naturally faster):

sum_even_fib_limit = let α = inv(√5), φ = 0.5(1+√5), φ₋ = (1-φ), 
                         φ³ = φ^3, φ₋³ = φ₋^3
    (limit) -> begin
        n = floor(Int, log(limit/α)/log(φ)/3)
        return round(Int, 
          α*((φ³^(n+1)-1)/(φ³-1)-(φ₋³^(n+1)-1)/(φ₋³-1))
        )
    end
end;

For the limit in the thread:

julia> @btime sum_even_fib_limit(4e6)
  29.395 ns (1 allocation: 16 bytes)
4613732

WARNING label: This method may be limited by number representation inaccuracy in floating points.

1 Like