Problem:
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
My Solution:
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
My Solution:
static void Main(string[] args)
{
const long constantInput = 600851475143;
long input = constantInput;
long divider = 2;
long multiplyList = 1;
bool largestPrimeFound = false;
while (largestPrimeFound == false)
{
if (input % divider == 0)
{
long val = input / divider;
input = val;
Console.WriteLine(divider);
multiplyList *= divider;
if (multiplyList == constantInput)
{
Console.WriteLine("The largest prime factor for the number " + constantInput + " is : " + divider);
largestPrimeFound = true;
}
}
else
{
divider += 1;
}
}
Console.ReadLine();
}
Note: You can simplifies the coding :)
No comments:
Post a Comment