Here is the recursive GCD function: The entire algorithm is just one line of logic. If is , return . Otherwise, call . Trace : you get , then , then , which returns . Four calls total. Compare that to checking every number from to . The recursive version is both simpler and faster. Trace through a small example to verify your understanding before moving on.
Time complexity: because the remainder shrinks rapidly.
Space complexity: for the call stack.