mirror of
https://bitbucket.org/Mattrixwv/projecteuleroctave.git
synced 2025-12-06 09:33:58 -05:00
Updated problem 3 algorithm
This commit is contained in:
70
Problem3.m
70
Problem3.m
@@ -1,10 +1,11 @@
|
||||
function [] = Problem3
|
||||
%ProjectEuler/Octave/Problem3.m
|
||||
%Matthew Ellison
|
||||
% Created:
|
||||
%Modified: 03-28-19
|
||||
% Created: 03-28-19
|
||||
%Modified: 10-28-20
|
||||
%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
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
@@ -21,63 +22,28 @@
|
||||
%}
|
||||
|
||||
|
||||
%Setup your variables
|
||||
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
|
||||
tempNum = number; %Used to track the current value if all of the factors were taken out of number
|
||||
%Setup your variables
|
||||
number = 600851475143; %The number we are trying to find the greatest prime factor of
|
||||
factors = []; %For the list of factors of number
|
||||
|
||||
%Get the prime numbers up to sqrt(number). If it is not prime there must be a value <= sqrt
|
||||
primeNums = primes(sqrt(number));
|
||||
%Get the prime numbers up to sqrt(number). If it is not prime there must be a value <= sqrt
|
||||
primeNums = primes(sqrt(number));
|
||||
|
||||
%Start the timer
|
||||
startTime = clock();
|
||||
%Start the timer
|
||||
startTime = clock();
|
||||
|
||||
%Setup the loop
|
||||
counter = 1;
|
||||
factors = factor(600851475143);
|
||||
|
||||
%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))
|
||||
%Stop the timer
|
||||
endTime = clock();
|
||||
|
||||
%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
|
||||
%Print the results
|
||||
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))
|
||||
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
|
||||
endTime = clock();
|
||||
|
||||
%Print the results
|
||||
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))
|
||||
|
||||
%Cleanup your variables
|
||||
clear counter;
|
||||
clear tempNum;
|
||||
clear answer;
|
||||
clear number;
|
||||
clear primeNums;
|
||||
clear factors;
|
||||
clear startTime;
|
||||
clear endTime;
|
||||
clear ans;
|
||||
|
||||
%{
|
||||
Results:
|
||||
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