mirror of
https://bitbucket.org/Mattrixwv/octavefunctions.git
synced 2025-12-06 18:53:57 -05:00
Added problems 4 and 5 and a helper function for 4 to Project Euler
This commit is contained in:
41
ProjectEuler/Problem4.m
Normal file
41
ProjectEuler/Problem4.m
Normal file
@@ -0,0 +1,41 @@
|
||||
%ProjectEuler/Problem4.m
|
||||
%This is a script to answer Problem 4 for Project Euler
|
||||
%Find the largest palindrome made from the product of two 3-digit numbers
|
||||
|
||||
%Make your variables
|
||||
answer = 0; %For the product of the two numbers
|
||||
numbers = [100:999]; %Create an array with a list of all 3 digit numbers
|
||||
palindromes = []; %Holds all the numbers that are palindromes
|
||||
%Create 2 counters for an inner loop and an outer loop
|
||||
%This allows you to multiply 2 numbers from the same array
|
||||
outerCounter = 1;
|
||||
innerCounter = 1;
|
||||
|
||||
while(outerCounter < size(numbers)(2))
|
||||
innerCounter = outerCounter; %Once you have multiplied 2 numbers there is no need to multiply them again, so skip what has already been done
|
||||
while(innerCounter < size(numbers)(2))
|
||||
%Multiply the two numbers
|
||||
answer = numbers(outerCounter) * numbers(innerCounter);
|
||||
|
||||
%See if the number is a palindromes
|
||||
%%WARNING - Ocatave does not have a Reverse function. I had to create one that reversed strings
|
||||
if(num2str(answer) == Reverse(num2str(answer)))
|
||||
%Add it to the palindromes list
|
||||
palindromes(end + 1) = answer;
|
||||
end
|
||||
++innerCounter; %Increment
|
||||
end
|
||||
++outerCounter; %Increment
|
||||
end
|
||||
|
||||
clear outerCounter;
|
||||
clear innerCounter;
|
||||
clear answer;
|
||||
clear numbers;
|
||||
|
||||
max(palindromes)
|
||||
|
||||
%This way is slow. I would like to find a faster way
|
||||
%{
|
||||
The palindrome can be written as: abccba Which then simpifies to: 100000a + 10000b + 1000c + 100c + 10b + a And then: 100001a + 10010b + 1100c Factoring out 11, you get: 11(9091a + 910b + 100c) So the palindrome must be divisible by 11. Seeing as 11 is prime, at least one of the numbers must be divisible by 11
|
||||
%}
|
||||
45
ProjectEuler/Problem5.m
Normal file
45
ProjectEuler/Problem5.m
Normal file
@@ -0,0 +1,45 @@
|
||||
%ProjectEuler/Problem5.m
|
||||
%This is a script to answer Problem 5 for Project Euler
|
||||
%What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
|
||||
|
||||
%Create your variables
|
||||
value = 0; %The value that is evenly divisible
|
||||
nums = [1:20];
|
||||
factors = [1]; %The factors that are already in the number
|
||||
list = []; %For a temperary list of the factors of the current number
|
||||
counter = 1;
|
||||
|
||||
%You need to find the factors of all the numbers from 1->20
|
||||
while(counter <= size(nums)(2))
|
||||
list = factor(nums(counter));
|
||||
|
||||
%Search factors and try to match all elements in list
|
||||
listCnt = 1;
|
||||
factorCnt = 1;
|
||||
while(listCnt <= size(list)(2))
|
||||
if((factorCnt > size(factors)(2)) || (factors(factorCnt) > list(listCnt)))
|
||||
%If it was not found add the factor to the list for the number and reset the counters
|
||||
factors(end + 1) = list(listCnt);
|
||||
factors = sort(factors);
|
||||
factorCnt = 1;
|
||||
listCnt = 1;
|
||||
elseif(factors(factorCnt) == list(listCnt))
|
||||
++listCnt;
|
||||
++factorCnt;
|
||||
else
|
||||
++factorCnt;
|
||||
end
|
||||
end
|
||||
++counter;
|
||||
end
|
||||
|
||||
%Cleanup your variables
|
||||
clear counter;
|
||||
clear factorCnt;
|
||||
clear listCnt;
|
||||
clear list;
|
||||
clear nums;
|
||||
clear ans; %Appears whenever you use increment
|
||||
|
||||
value = prod(factors);
|
||||
value
|
||||
19
ProjectEuler/Reverse.m
Normal file
19
ProjectEuler/Reverse.m
Normal file
@@ -0,0 +1,19 @@
|
||||
function [rString] = Reverse(str)
|
||||
%Reverse(string)
|
||||
%This function Reverse the order of the elements in an array
|
||||
%It was specifically designed for a string, but should work on other 1xX arrays
|
||||
%
|
||||
|
||||
if(nargin ~= 1)
|
||||
error('That is not a valid number of arguments')
|
||||
return;
|
||||
end
|
||||
|
||||
counter = size(str)(2); %Set the counter to the last element in string
|
||||
%Loop until the counter reaches 0
|
||||
while(counter > 0)
|
||||
%Add the current element of string to rString
|
||||
rString(end + 1) = str(counter);
|
||||
--counter;
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user