Your multi
version runs in O(N) whereas russian_peasant
version runs in O(logN), which is far better than O(N).
To realize how fast your russian_peasant
version is, check this out
from math import log
print round(log(100000000, 2)) # 27.0
So, the loop has to be executed just 27 times, but your multi
version's while loop has to be executed 100000000 times, when y
is 100000000.
To answer your other question,
What I want you to answer is how do programming languages like python
multiply numbers ?
Python uses O(N^2) grade school multiplication algorithm for small numbers, but for big numbers it uses Karatsuba algorithm.
Basically multiplication is handled in C code, which can be compiled to machine code and executed faster.