Problem 3

The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?

Solution:

The word "prime factor" may be confusing.

so now i am going to split the word to "prime" and "factor".

First factor, factors are nothing but if any numbers within N, divides excatly N leaving remainder 0 then 

they are the factors of N.

and the prime numbers in the factors are prime factors.

i.e first we want to find the factors , then we need to check whether the factors are prime or not.

    class Program
    {
        static void Main(string[] args)
        {

            long number = 600851475143;

            long iNum = 1;

            long largestPrimeFactor = 0;

            while (iNum <= number)
            {
                if (iNum != 1 && number % iNum == 0)
                {
                    long iPrime = 2;

                    bool isPrime = true;

                    while (iPrime < iNum)
                    {
                        if (iNum % iPrime == 0)
                        {
                            isPrime = false;
                        }
                        iPrime++;
                    }

                    if (isPrime)
                        largestPrimeFactor = iNum;
                }

                iNum++;
            }

            Console.WriteLine(largestPrimeFactor);
        }

        static bool IsPrime(long n)
        {
            for (long i = 2; i < n / 2; i++)
            {
                if (n % i == 0)
                    return false;
            }

            return true;
        }
    }

Share your ideas

ċ
Program.cs
(2k)
karthi p,
Aug 29, 2011, 4:41 PM
Comments