You are viewing a single comment's thread. Return to all comments →
I decided not to complicate and optimize, so I just used tail recursion:
def noOfCd(x: Int, y: Int): Int = { def noOfCdRec(x: Int, y: Int, divisor: Int, result: Int = 0): Int = { if (divisor == 0) result else if (x % divisor == 0 && y % divisor == 0) noOfCdRec(x, y, divisor - 1, result + 1) else noOfCdRec(x, y, divisor - 1, result) } noOfCdRec(x, y, min(x, y)) }
Seems like cookies are disabled on this browser, please enable them to open this website
Common Divisors
You are viewing a single comment's thread. Return to all comments →
I decided not to complicate and optimize, so I just used tail recursion: