Skip to content

Instantly share code, notes, and snippets.

@feliperazeek
Created September 28, 2015 05:31
Show Gist options
  • Save feliperazeek/52995d513a0c8f950117 to your computer and use it in GitHub Desktop.
Save feliperazeek/52995d513a0c8f950117 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