My solution is recursive. I start with a pair of numbers (0,1). On each recursive call, I'm generating a new pair such that the old second value is the new first value, and the new second value is the sum of the old pair. On each recursive call, I also pass an n value that is one less.
If n is zero, the conditional statement would fail, and I'd return the first value in the pair instead of doing the next calculation.
I'm aware that this approach would actually generate one more pair than necessary, however, it also allows it to be correct for n=0.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Here's my own first pass. I love one-liners. (Python 3)
Unpacking that, the logic is basically:
My solution is recursive. I start with a pair of numbers
(0,1)
. On each recursive call, I'm generating a new pair such that the old second value is the new first value, and the new second value is the sum of the old pair. On each recursive call, I also pass ann
value that is one less.If
n
is zero, the conditional statement would fail, and I'd return the first value in the pair instead of doing the next calculation.I'm aware that this approach would actually generate one more pair than necessary, however, it also allows it to be correct for
n=0
.