3 Largest Prime Factor

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

What is the largest prime factor of the number 600851475143 ?

#include <iostream>
#include <limits>
#include <vector>

using namespace std;

void pause() { cin.ignore(numeric_limits<streamsize>::max(), '\n'); }

bool checkIfPrime(int number)
{
    for (int j = 3; j <= number - 1; j++)
    {
        // 2) Check if actually prime
        if (number % j == 0)
        {
            return false;
        }

        else if (j % number != 0)
        {
            return true;
        }
    }

}

int main()
{
    long long int number = 600851475143;
    vector<int> primes;

    for (int i = 3; i <= number; i++)
    {
        // 1) Check if potential prime
        if (number % i == 0)
        {
            if (checkIfPrime(i) == true)
            {
                primes.push_back(i);
                number = number / i;
            }
        }
        cout << i << endl;
    }

    for (int i = 0; i < primes.size(); i++)
    {
        cout << primes[i] << " ";
    }

    cout << endl << "the largest prime is: ";

    int max = primes[0];
    for (int i = 1; i < primes.size(); i++)
    {
        if (primes[i] > max)
        {
            max = primes[i];
        }
    }

    cout << max;
    pause();
}

Solution: 6857

Last updated