From 7c04e9043cf1b1b962d4de0175240fe16171ce3f Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Thu, 27 Sep 2018 13:33:45 -0400 Subject: [PATCH] Added program for problem 3 --- ProjectEuler/C++/Problem3.cpp | 104 ++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 ProjectEuler/C++/Problem3.cpp diff --git a/ProjectEuler/C++/Problem3.cpp b/ProjectEuler/C++/Problem3.cpp new file mode 100644 index 0000000..16728f9 --- /dev/null +++ b/ProjectEuler/C++/Problem3.cpp @@ -0,0 +1,104 @@ +//ProjectEuler/C++/Problem3.cpp +//Matthew Ellison +// Created: 9-28-18 +//Modified: 9-28-18 +//The largest prime factor of 600851475143 + + +#include +#include +#include +#include + +std::vector generatePrimes(const unsigned long long number); +unsigned long long findLargest(const std::vector list); + + +int main(){ + const unsigned long long goalNumber = 600851475143; //The number you are trying to find the factors of + std::vector primes; //Holds the list of prime numbers + std::vector 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(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 generatePrimes(const unsigned long long goalNumber){ + std::vector 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 list){ + unsigned long long maxNum = 0; + for(unsigned long long num : list){ + if(num > maxNum){ + maxNum = num; + } + } + + return maxNum; +}