Cactus BAMF
Cactus BAMF

Reputation: 333

Project Euler 4

I have written the following code and put a lot of time into it. But there is just something intrinsically wrong with it, if someone could kindly direct me in making it more efficient, I would be infinitely grateful.

It currently produces NO output.

% A palindromic number reads the same both ways.
% The largest palindrome made from the product of
% two 2-digit numbers is 9009 = 91  99.

% Find the largest palindrome made from the
% product of two 3-digit numbers.

% 1) Find palindromes below 999x999 (product of two 3 digit #s)
% 2) For each palindrome found, find the greatest Factors.
% 3) The first palindrome to have two 3 digit factors is the
%    Solution



%============== Variables ===============================
%
% number = a product of 2 3 digit numbers, less than 100x100. The
% Palindrome we are looking for.
%
% n1, n2 = integers; possible factors of number.
%
% F1, F2 = two largest of factors of number. multiplied
% together they == number.
%
% finish = boolean variable, decides when the program terminates


% ================= Find Palindrome ================================

% The maximum product of two 3 digit numbers

number = 999*999;
finish = false;
count = 0;

while ( finish == false)

%
% Check to see if number is a Palindrome by comparing
% String versions of the number
%
% NOTE: comparing num2string vectors compares each element
% individually. ie, if both strings are identical, the output will be
% a vector of ONES whose size is equal to that of the two num2string
% vectors.
%
if ( mean(num2str( number ) == fliplr( num2str ( number ) ) ) == 1  )

    % fprintf(1, 'You have a palindrome %d', number);









    % Now find the greatest two factors of the discovered number ==========

    n1 = 100;
    n2 = 100; % temporary value to enter loop



    % While n2 has 3 digits in front of the decimal, continue
    % Searching for n1 and n2. In this loop, n1 increases by one
    % each iteration, and so n2 decreases by some amount. When n2
    % is no longer within the 3 digit range, we stop searching
    while( 1 + floor( log10( n2 ) ) == 3 )


        n2 = number/n1;



        % If n2 is EXACTLY a 3 digit integer,
        % n1 and n2 are 3 digit factors of Palindrome 'number'
        if( 1 + log10( n2 )  == 3 )

            finish = true;

            Fact1 = n1;
            Fact2 = n2;


        else
            % increment n1 so as to check for all possible
            % 3 digit factors ( n1 = [100,999] )
            n1 = n1 + 1;

        end

    end




    % if number = n1*n2 is not a palindrome, we must decrease one of the
    % Factors of number and restart the search
else

    count = count + 1;

    number = 999 * (999 - count);



end

end



fprintf(1, 'The largest factors of the palindrome %i \n', number )
fprintf(1, ' are %i and %i', Fact1, Fact2 )

Upvotes: 1

Views: 1050

Answers (2)

Rasman
Rasman

Reputation: 5359

Since this is an Euler projct, I will only give some general words of advice.

  1. To compare strings, use strcmp, and not your method (it makes for cleaner code)

  2. See Isaac's comment. add floor and a condition to check if the number is an integer (log10 doesn't do that)

  3. even if you enter that if statement, you never really exit it, as the while loop just keeps on looping around with the same two numbers. consider terminating the while then and there with break by modifying your while loop.

  4. While your solution provides a result, it is not the right one, the reason being number is always a multiple of 999 based on your code, which most likely in incorrect. Change how you construct number. you will have to add at least another line defining number to do so. Your solution is 90909. The correct solution is closer to 100000 (at least that is the highest I found)

Upvotes: 1

Isaac
Isaac

Reputation: 3616

The condition:

if( 1 + log10( n2 )  == 3 )

will only be true when n2 == 100, and seeing as n2 will only be an integer when n1 divides number, your while loop will likely never finish.

Upvotes: 1

Related Questions