Monday, March 26, 2012

What is the largest prime factor of the number 600851475143 ?

We solve this program in two stages first we find the factors then we check whether the factor is prime. If its prime then we print the same as a output. Run the loop till all factors have been checked.

The solution given below is running for integers but is not giving any output for really long digits. Still trying to figure out a solution.


/*
What is the largest prime factor of the number 600851475143 ?

*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Check4Prime
{
public bool checkprime(long a)
{
bool prime = true;
for (long i = a - 1; i > 1; i--)
{
if (a % i == 0)
{
prime = false;
}
}
return prime;
}
}
class Program
{

static void Main(string[] args)
{
long number = 600851475143;
int j=1;

Check4Prime c = new Check4Prime();

for (long factor = number - 1; factor > 1; factor--)
{
if (number % factor == 0)
{
if (c.checkprime(factor))
{
Console.WriteLine(" Prime factor number " + j++ + "of " + number + " is " + factor);
}
}
}

}
}
}

No comments: