Files
ProjectEulerOctave/Problem29.m

106 lines
3.3 KiB
Matlab

function [] = Problem29()
%ProjectEuler/Octave/Problem29.m
%Matthew Ellison
% Created: 10-16-19
%Modified: 10-20-19
%How many distinct terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100?
%{
Copyright (C) 2019 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 your variables
BOTTOM_A = 2; %The lowest possible value for A
TOP_A = 100; %The highest possible value for A
BOTTOM_B = 2; %The lowest possible value for B
TOP_B = 100; %The highest possible value for B
uniq = {}; %A table to hold all of the unique answers for the equation
currentNum = [];%Holds the answer to the equation for a particular loop
%Start the timer
startTime = clock();
%Start with the lowest A and move towards the largest
for currentA = BOTTOM_A : TOP_A
currentA
%Start with the lowest B and move towards the largest
for currentB = BOTTOM_B : TOP_B
%Get the number
%Start with the base number and multiply until you reach the correct power
currentPower = 0;
carry = 0;
currentNum = [1];
while(currentPower < currentB)
counter = 1;
%Loop through every element in the list and multiply it by the current A
while(counter <= size(currentNum)(2))
currentNum(counter) = (currentNum(counter) * currentA) + carry;
carry = 0;
%If one fo the elements is too large you need to carry that to the next element
while(currentNum(counter) >= 10)
currentNum(counter) -= 10;
++carry;
end
++counter;
end
%If you ahve something to carry after everything has been multiplied you need to add a new column
while(carry > 0)
currentNum(end + 1) = carry;
carry = 0;
%If one fo the elements is too large you need to carry that to the next element
while(currentNum(end) >= 10)
currentNum(end) -= 10;
++carry;
end
end
++currentPower;
end
%If the number isn't in the list add it
if(~isFound(uniq, currentNum))
uniq(end + 1) = currentNum;
end
end
end
%Stop the timer
endTime = clock();
%Print the results
printf("The number of unique values generated by a^b for %d <= a <= %d and %d <= b <= %d is %d\n", BOTTOM_A, TOP_A, BOTTOM_B, TOP_B, size(uniq)(2));
printf("It took %f seconds to run this algorithm\n", etime(endTime, startTime))
end
function [found] = isFound(array, key)
found = false; %Start with a false. It only turns true if you find key in array
for location = 1 : size(array)(2)
if(size(key)(2) != size(array{location})(2))
continue;
elseif(key == array{location})
found = true;
return;
end
end
end
%{
This has not run to completion because it would take an insane amount of time
but it got the correct results on several subsets (compared with my python code)
so it should come up with the same answer.
%}