Euler Problems - #4

Posted Wednesday, April 09, 2008 11:23 PM by amarsan

I thought I'd join the Euler problem bandwagon. In true mechanical engineering fashion, I'm using Matlab to solve them. Actually, just for an added twist, I'm using Octave, an open source math interpreter that is mostly compatible with Matlab, on my dog-slow Mac notebook, forcing me to not use brute-force approaches if they can be avoided.

I had already seen solutions to problems 1-3, so I started with 4.  So here goes:

function euler4

max_result = 0;
for ii = 999:-1:100
  for jj = 999:-1:100

    % Don't bother to check if this number is smaller than the max found
    result = ii*jj;
    if (result < max_result)
      continue;
    end
     
    % Check for palindrome
    result_string = int2str(result);
    success = 1;
    rlength = length(result_string);
    for kk = 1:rlength/2
      if result_string(kk) ~= result_string(rlength-kk+1)
        success = 0;
        break;
      end
    end
    if success && result > max_result
      max_result = result;
    end
   
  end
end

max_result

Not pretty, but it gets the job done.

 

Filed under:

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required)