final (maybe) 3 algorithms - naive, optimised, optimal