Created
September 28, 2015 05:31
-
-
Save feliperazeek/52995d513a0c8f950117 to your computer and use it in GitHub Desktop.
Codility CountFactors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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