Thursday, November 3, 2016

Repeated subtraction among two numbers - GeeksforGeeks

Repeated subtraction among two numbers - GeeksforGeeks
Given a pair of positive numbers x and y. We repeatedly subtract the smaller of the two integers from greater one until one of the integers becomes 0. The task is to count number of steps to before we stop (one of the numbers become 0).

A simple solution is to actually follow the process and count the number of steps.

A better solution is to use below steps. Let y be the smaller of two numbers1) if y divides x then return (x/y)2) else return ( (x/y) + solve(y, x%y) )