%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 max(palindromes) %Cleanup your variables clear outerCounter; clear innerCounter; clear answer; clear numbers; clear 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 %}