Files
OctaveFunctions/ProjectEuler/C++/Problem3.cpp

105 lines
3.3 KiB
C++

//ProjectEuler/C++/Problem3.cpp
//Matthew Ellison
// Created: 9-28-18
//Modified: 9-28-18
//The largest prime factor of 600851475143
#include <iostream>
#include <cmath>
#include <vector>
#include <chrono>
std::vector<unsigned long long> generatePrimes(const unsigned long long number);
unsigned long long findLargest(const std::vector<unsigned long long> list);
int main(){
const unsigned long long goalNumber = 600851475143; //The number you are trying to find the factors of
std::vector<unsigned long long> primes; //Holds the list of prime numbers
std::vector<unsigned long long> factors; //Holds the factors of goalNumber
unsigned long long number = goalNumber; //The number that is left after taking the prime numbers out
bool found = false;
std::chrono::high_resolution_clock::time_point startTime = std::chrono::high_resolution_clock::now();
//Generate the primes
primes = generatePrimes(ceil(sqrt(goalNumber)));
//Check the primes against the number
//62113 numbers at this point
while(!found){
//Loop through the list of primes
for(int cnt = 0;cnt < primes.size();){
//See if after dividing a prime out it is left with a whole number
if((number % primes.at(cnt)) == 0){
number /= primes.at(cnt);
factors.push_back(primes.at(cnt));
}
//If you didn't find a prime then advance to the next possible number
//If you did find a prime then stay where you are in the list incase it occurs more than once
else{
++cnt;
}
}
//If the remaining number is 1 then you just added the last number to the factors
if(number == 1){
found = true;
}
}
//Look for the largest number in the vector
unsigned long long maxNum = findLargest(factors);
//Calculate the amount of time it took to run the algorithm
std::chrono::high_resolution_clock::time_point endTime = std::chrono::high_resolution_clock::now();
std::chrono::high_resolution_clock::duration dur = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime);
//Print the results
std::cout << "The largest factor of the number " << goalNumber << " is " << maxNum
<< "\nIt took " << dur.count() << " milliseconds to run this algorith" << std::endl;
//Pause before exiting
std::cin.get();
return 0;
}
std::vector<unsigned long long> generatePrimes(const unsigned long long goalNumber){
std::vector<unsigned long long> primes;
bool foundFactor = false;
//Start at 2 so we can skip 1
if(goalNumber >= 2){
primes.push_back(2);
}
//We can skip all of the even numbers
for(unsigned long long possiblePrime = 3;possiblePrime < goalNumber;possiblePrime += 2){
//Step through every element in the current primes. If you don't find anything that divides it, it must be a prime itself
for(int cnt = 0;(cnt < primes.size()) && ((primes.at(cnt) * primes.at(cnt)) < goalNumber);++cnt){
if(fmod(((double)possiblePrime / primes.at(cnt)), 1) == 0){
foundFactor = true;
break;
}
}
//If you didn't find a factor then it must be prime
if(!foundFactor){
primes.push_back(possiblePrime);
}
//If you did find a factor you need to reset the flag
else{
foundFactor = false;
}
}
return primes;
}
unsigned long long findLargest(const std::vector<unsigned long long> list){
unsigned long long maxNum = 0;
for(unsigned long long num : list){
if(num > maxNum){
maxNum = num;
}
}
return maxNum;
}