Programming interviews are insane. At Meta my friend was asked to print out the n-th number in the Fibonacci sequence. We already know how not to do it. But how do we do it? My solution would be

a, b = 0, 1
for _ in range(n):
    a, b = b, a + b
print(a)

I can recall how hard it was for me to solve such problems in basic, pascal, c. There wasn't such a beautiful tuple assignment. So I had to juggle values in several variables. Definitely it was educational as hell. But the quiet elegance of the program above strikes me.

The template is quite remarkable. You move through a sequence of numbers. At any given moment a and b are the last numbers in the sequence so far. You calculate one more number and shift this pair one step forward. Fascinating.

I like to introduce Euclidean algorithm as "Start a sequence with two numbers a, b whose GCD you want to find. Then build the sequence by calculating remainder of the two last numbers and writing down this remainder as a new element of the sequence. Stop when you reach 0. The element just before the last is your answer" Using this template gives you


def gcd(a, b):
    while b:
        a, b = b, a%b
    return a

I deviated from my initial plan. I wanted to practice implementing a very fast algorithm for calculating Fibonacci numbers. Next time then.