• + 0 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))
        }