Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save royki/2fb53eaaadbcfab3ab6f0dfd1ca24091 to your computer and use it in GitHub Desktop.
Save royki/2fb53eaaadbcfab3ab6f0dfd1ca24091 to your computer and use it in GitHub Desktop.
Codility CountFactors
object Solution {
def solution(N: Int): Int = {
if (N < 1) sys.error(s"Invalid input: $N")
@scala.annotation.tailrec
def foo(i: Int, total: Int): (Int, Int) = {
if ((i * i) >= N) (total, i)
else if (N % i == 0) foo(i + 1, total + 2)
else foo(i + 1, total)
}
val (results, x) = foo( 1, total = 0)
if (x * x == N) results + 1
else results
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment