Skip to content

Instantly share code, notes, and snippets.

@dimdim2
Last active December 16, 2015 20:58
Show Gist options
  • Save dimdim2/5495864 to your computer and use it in GitHub Desktop.
Save dimdim2/5495864 to your computer and use it in GitHub Desktop.
Euler Problem3
@tailrec
def getMinFactor(num: Long, divisor: Long = 2L): Long = {
if(divisor > Math.sqrt(num)) num
else if(num % divisor == 0) divisor
else getMinFactor(num, divisor+1)
} //> smallestFactor: (num: Long, divisor: Long)Long
def getMaxPrimeFactor(num: Long): Long = {
@tailrec
def run(num: Long, minFactor: Long): Long = {
val next = num / minFactor
if(next <= 1) num else run(next, getMinFactor(next))
}
run(num, getMinFactor(num))
} //> greatestFactor: (num: Long, prime: Long)Long
getMaxPrimeFactor(600851475143L) //> 6857
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment