Skip to content

Instantly share code, notes, and snippets.

@onlyshk
Created May 12, 2012 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onlyshk/2666996 to your computer and use it in GitHub Desktop.
Save onlyshk/2666996 to your computer and use it in GitHub Desktop.
package proj_euler
/*
* The prime factors of 13195 are 5, 7, 13 and 29.
* What is the largest prime factor of the number 600851475143 ?
*/
object Problem3 {
def solve3(num1 : Long) : Long = {
var num3 = 2
var num2 = num1 / num3
while ((num2 > 2) == true)
{
if ((num1 % num2 == 0) )
{
var check_prime = is_prime(num2)
if (check_prime == true)
return num2
else
{
num2 = num2 - 1
}
}
else
{
num3 = num3 + 1
num2 = num1 / num3
}
}
return num2
}
def is_prime(n: BigInt) : Boolean ={
var c = n / 2
var d : BigInt = 2
while (d < c == true){
if (n % d == 0)
return false
else
d = d + 1
}
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment