Commit 9f76244e by Andrew Dahl

Solved Problem 3

parent c18027f2
Showing with 38 additions and 0 deletions
Question:
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
Answer: 6857
#include <iostream>
#include <vector>
using namespace std;
int main() {
bool is_prime = false;
long int number = 600851475143;
int prime_factor = 3;
vector<int> primes;
primes.push_back(2);
primes.push_back(3);
for(; number > 1; prime_factor+=2)
{
is_prime = true;
for(int i = 0; i < primes.size() && is_prime; i++)
if(prime_factor % primes[i] == 0)
is_prime = false;
if(is_prime)
{
primes.push_back(prime_factor);
if(number % prime_factor == 0)
number = number / prime_factor;
}
}
cout << primes[primes.size()-1] << endl;
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment