mirror of
https://bitbucket.org/Mattrixwv/projecteuleroctave.git
synced 2025-12-06 17:43:57 -05:00
Updated problem 3 algorithm
This commit is contained in:
48
Problem3.m
48
Problem3.m
@@ -1,10 +1,11 @@
|
|||||||
|
function [] = Problem3
|
||||||
%ProjectEuler/Octave/Problem3.m
|
%ProjectEuler/Octave/Problem3.m
|
||||||
%Matthew Ellison
|
%Matthew Ellison
|
||||||
% Created:
|
% Created: 03-28-19
|
||||||
%Modified: 03-28-19
|
%Modified: 10-28-20
|
||||||
%The largest prime factor of 600851475143
|
%The largest prime factor of 600851475143
|
||||||
%{
|
%{
|
||||||
Copyright (C) 2019 Matthew Ellison
|
Copyright (C) 2020 Matthew Ellison
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
This program is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU Lesser General Public License as published by
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
@@ -23,9 +24,7 @@
|
|||||||
|
|
||||||
%Setup your variables
|
%Setup your variables
|
||||||
number = 600851475143; %The number we are trying to find the greatest prime factor of
|
number = 600851475143; %The number we are trying to find the greatest prime factor of
|
||||||
primeNums = []; %A list of prime numbers. Will include all prime numbers <= number
|
|
||||||
factors = []; %For the list of factors of number
|
factors = []; %For the list of factors of number
|
||||||
tempNum = number; %Used to track the current value if all of the factors were taken out of number
|
|
||||||
|
|
||||||
%Get the prime numbers up to sqrt(number). If it is not prime there must be a value <= sqrt
|
%Get the prime numbers up to sqrt(number). If it is not prime there must be a value <= sqrt
|
||||||
primeNums = primes(sqrt(number));
|
primeNums = primes(sqrt(number));
|
||||||
@@ -33,30 +32,7 @@ primeNums = primes(sqrt(number));
|
|||||||
%Start the timer
|
%Start the timer
|
||||||
startTime = clock();
|
startTime = clock();
|
||||||
|
|
||||||
%Setup the loop
|
factors = factor(600851475143);
|
||||||
counter = 1;
|
|
||||||
|
|
||||||
%Start with the lowest number and work your way up. When you reach a number > size(primeNums) you have found all of the factors
|
|
||||||
while(counter <= size(primeNums)(2))
|
|
||||||
|
|
||||||
%Divide the number by the next prime number in the list
|
|
||||||
answer = (tempNum/primeNums(counter));
|
|
||||||
|
|
||||||
%If it is a whole number add it to the factors
|
|
||||||
if(mod(answer,1) == 0)
|
|
||||||
factors(end + 1) = primeNums(counter);
|
|
||||||
%Set tempNum so that it reflects number/factors
|
|
||||||
tempNum = tempNum / primeNums(counter);
|
|
||||||
%Keep the counter where it is in case a factor appears more than once
|
|
||||||
%Get the new set of prime numbers
|
|
||||||
primeNums = primes(sqrt(tempNum));
|
|
||||||
else
|
|
||||||
%If it was not an integer increment the counter
|
|
||||||
++counter;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
%When the last number is not divisible by a prime number it must be a prime number
|
|
||||||
factors(end + 1) = tempNum;
|
|
||||||
|
|
||||||
%Stop the timer
|
%Stop the timer
|
||||||
endTime = clock();
|
endTime = clock();
|
||||||
@@ -64,20 +40,10 @@ endTime = clock();
|
|||||||
%Print the results
|
%Print the results
|
||||||
printf("The largest prime factor of 600851475143 is %d\n", max(factors))
|
printf("The largest prime factor of 600851475143 is %d\n", max(factors))
|
||||||
printf("It took %f seconds to run this algorithm\n", etime(endTime, startTime))
|
printf("It took %f seconds to run this algorithm\n", etime(endTime, startTime))
|
||||||
|
end
|
||||||
%Cleanup your variables
|
|
||||||
clear counter;
|
|
||||||
clear tempNum;
|
|
||||||
clear answer;
|
|
||||||
clear number;
|
|
||||||
clear primeNums;
|
|
||||||
clear factors;
|
|
||||||
clear startTime;
|
|
||||||
clear endTime;
|
|
||||||
clear ans;
|
|
||||||
|
|
||||||
%{
|
%{
|
||||||
Results:
|
Results:
|
||||||
The largest prime factor of 600851475143 is 6857
|
The largest prime factor of 600851475143 is 6857
|
||||||
It took 0.006256 seconds to run this algorithm
|
It took 0.005714 seconds to run this algorithm
|
||||||
%}
|
%}
|
||||||
|
|||||||
Reference in New Issue
Block a user