Hello!
I had an assignment in school last week to implement a function for calculating the n:th number in the fibonacci sequence. A 'sub-assignment' was to implement it using accumulation(Might not be a correct translation) in order to give the function O(n) time complexity. This all worked fine until I tried making the function (Int -> Integer). By experimenting a bit I realised that the time complexity was close to O(n^2) for really large numbers. It occurs to me that this must be because of the Integer implementation, which makes me a bit curious about how it works. I did a few Google searches and didn't find anything that seemed useful so I am turning to you guys in hope of either getting an explanation or a link that explains it thoroughly.
My code:
ackfib 0 = 0
ackfib 1 = 1
ackfib n = loop n 1 0 1
where
loop n n1 n2 i
| i < n = loop n (n1+n2) n1 (i+1)
| i == n = n1
| i > n = error "n must be greater than or equal to 0"
I am thankful for all answers
Viktor