[Tutor] How inefficient is this code?

Neil D. Cerutti neilc at norwich.edu
Thu May 8 15:59:05 CEST 2014


On 5/7/2014 8:39 PM, C Smith wrote:
> I see. Thanks that is exactly what I was looking for. Would your
> example be an instance of an object-oriented approach,
> compartmentalizing things into functions?

One of the fun parts of Project Euler is using mathematical analysis and 
other tricks to improve your algorithm.

You have a nice, general Fibonacci number generator, but you can use a 
less general function to increase efficiency [1]. In this case, it 
doesn't matter--your initial solution was fast enough--but you'll soon 
be solving problems where the most general solution fails to be fast enough.

[1] Your code is calculating about twice as many Fibonacci numbers as it 
needs to. See if you can create a way to calculate Fib(n+2) from Fib(n).

-- 
Neil Cerutti



More information about the Tutor mailing list