Create a test case for Euler problem 3 The prime factors of

Create a test case for Euler problem 3:

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

Solution

#include <stdio.h>
#include <math.h>

// function to print all prime factors for a given number.
int primFact(long int n,int* arr)
{
   long int i;
   int j=0;
    // Print the number of 2s that divide n
    while (n%2 == 0)
    {
        arr[j++] = 2;
        n = n/2;
    }
    // n must be odd at this point. So we can skip one element (Note i = i +2)
    for (i = 3; i <= sqrt(n); i = i+2)
    {
        // While i divides n, print i and divide n
        while (n%i == 0)
        {
            arr[j++] = i;
            n = n/i;
        }
    }

    /*
    handle prime numbers;
    */
    if (n > 2){
        arr[j++] = n;
    }
    return j;
}

/* main function to test the prime factors functions.*/
int main()
{
    long int n = 600851475143;
    int arr[100];
    int len = primFact(n,&arr[0]);
    int i;
    int max = arr[0];
    for(i=1;i<len;i++){
        if(max<arr[i])
            max = arr[i];
    }
    printf(\"%d\ \",max);
    return 0;
}

/* sample output

6857

*/

Create a test case for Euler problem 3: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?Solution#in
Create a test case for Euler problem 3: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?Solution#in

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site