mirror of
https://bitbucket.org/Mattrixwv/projecteuleroctave.git
synced 2025-12-06 17:43:57 -05:00
Added solution for problem 35
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
function [] = Problem34()
|
function [] = Problem34()
|
||||||
%ProjectEuler/ProjectEulerOctave/Problem34.lua
|
%ProjectEuler/ProjectEulerOctave/Problem34.lua
|
||||||
%Matthew Ellison
|
%Matthew Ellison
|
||||||
% Createe: 06-01-21
|
% Created: 06-01-21
|
||||||
%Modified: 06-01-21
|
%Modified: 06-01-21
|
||||||
%Find the sum of all numbers which are equal to the sum of the factorial of their digits
|
%Find the sum of all numbers which are equal to the sum of the factorial of their digits
|
||||||
%{
|
%{
|
||||||
@@ -22,13 +22,13 @@ function [] = Problem34()
|
|||||||
%}
|
%}
|
||||||
|
|
||||||
%Setup the variables
|
%Setup the variables
|
||||||
|
|
||||||
%Start the timer
|
|
||||||
startTime = clock();
|
|
||||||
MAX_NUM = 1499999; %The largest num that can be the sum of its own digits
|
MAX_NUM = 1499999; %The largest num that can be the sum of its own digits
|
||||||
numberFactorials = []; %Holds the pre-computed factorials of the numbers 0-9
|
numberFactorials = []; %Holds the pre-computed factorials of the numbers 0-9
|
||||||
totalSum = 0; %Holds the sum of all numbers equal to the sum of their digit's factorials
|
totalSum = 0; %Holds the sum of all numbers equal to the sum of their digit's factorials
|
||||||
|
|
||||||
|
%Start the timer
|
||||||
|
startTime = clock();
|
||||||
|
|
||||||
%Pre-compute the possible factorials from 0! to 9!
|
%Pre-compute the possible factorials from 0! to 9!
|
||||||
for cnt = 1 : 9
|
for cnt = 1 : 9
|
||||||
numberFactorials(cnt) = factorial(cnt);
|
numberFactorials(cnt) = factorial(cnt);
|
||||||
|
|||||||
89
Problem35.m
Normal file
89
Problem35.m
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
function [] = Problem35()
|
||||||
|
%ProjectEuler/ProjectEulerOctave/Problem35.lua
|
||||||
|
%Matthew Ellison
|
||||||
|
% Created: 06-05-21
|
||||||
|
%Modified: 06-05-21
|
||||||
|
%Find the sum of all numbers which are equal to the sum of the factorial of their digits
|
||||||
|
%{
|
||||||
|
Copyright (C) 2021 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
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
%}
|
||||||
|
|
||||||
|
%Setup the variables
|
||||||
|
MAX_NUM = 999999;
|
||||||
|
%MAX_NUM = 100;
|
||||||
|
circularPrimes = [];
|
||||||
|
|
||||||
|
%Start the timer
|
||||||
|
startTime = clock();
|
||||||
|
|
||||||
|
%Get all primes under 1,000,000
|
||||||
|
primeList = primes(MAX_NUM);
|
||||||
|
%Go through all primes, get all their rotations, and check if those numbers are also prime
|
||||||
|
for cnt = 1 : size(primeList)(2)
|
||||||
|
prime = primeList(cnt)
|
||||||
|
allRotationsPrime = true;
|
||||||
|
%Get all of the rotations of the pirme and see if they are also prime
|
||||||
|
rotations = getRotations(num2str(prime));
|
||||||
|
for rotCnt = 1 : size(rotations)(2)
|
||||||
|
rotation = rotations(rotCnt);
|
||||||
|
p = rotation;
|
||||||
|
if(~isprime(p))
|
||||||
|
allRotationsPrime = false;
|
||||||
|
break;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
%If all rotations are prime add it to the list of circular primes
|
||||||
|
if(allRotationsPrime)
|
||||||
|
circularPrimes(end + 1) = prime;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
%Stop the timer
|
||||||
|
endTime = clock();
|
||||||
|
|
||||||
|
%Print the results
|
||||||
|
printf("The number of all circular prime numbers under %d is %d\n", MAX_NUM, size(circularPrimes)(2));
|
||||||
|
printf("It took %f seconds to run this algorithm\n", etime(endTime, startTime))
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
%Returns a list of all rotations of a string passed to it
|
||||||
|
function [rotations] = getRotations(str)
|
||||||
|
rotations = [];
|
||||||
|
rotations(end + 1) = str2num(str);
|
||||||
|
for cnt = 2 : size(str)(2)
|
||||||
|
temp = [substr(str, 2, size(str)(2) - 1) str(1)(1)];
|
||||||
|
num = str2num(temp);
|
||||||
|
rotations(end + 1) = num;
|
||||||
|
str = temp;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function [found] = isFound(array, key)
|
||||||
|
found = false;
|
||||||
|
for location = 1 : size(array)(2)
|
||||||
|
if(key == array(location))
|
||||||
|
found = true;
|
||||||
|
return;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
%{
|
||||||
|
Results:
|
||||||
|
The number of all circular prime numbers under 999999 is 55
|
||||||
|
It took 712.060379 seconds to run this algorithm
|
||||||
|
%}
|
||||||
Reference in New Issue
Block a user