2012-07-21 132 views
1

我已经写了下面的代码,并投入了大量的时间。但是有一些本质上的错误,如果有人能够指导我提高效率,我会感激不尽。项目欧拉4

它目前产生NO输出。

% 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) 
+1

与项目欧拉的一点是,你应该自己弄清楚,不要让别人代劳为你。 – Guffa 2012-07-21 20:55:55

+0

使用内置分析器查看挂机位置。在我的机器上短时间运行你的代码说''log10'正在浪费你的时间。想想如何简化重复发生的计算。例如,如果你想知道一个数字'n2'有三位数字,并且你知道它以三位数字开始并且正在减少,你是否真的需要记录那个数字的日志?没有。 – zroth 2012-07-21 21:02:00

回答

1

既然这是一个欧拉计划,我只会给出一些建议的一般性词汇。

  1. 比较字符串,使用STRCMP,而不是你的方法(它使 清晰的代码)

  2. 见以撒的评论。添加floor条件检查的数量是一个整数(日志10没有做到这一点)

  3. 即使你输入if语句,你从来没有真正离开它,因为while循环只是不断循环与相同的两个数字在一起。考虑通过修改你的while循环来终止当时和之后的中断。

  4. 虽然你的解决方案提供了一个结果,但它不是正确的,原因是数字总是基于你的代码的999倍,这很可能是不正确的。改变你如何构建number。您将不得不至少添加另一行定义数字才能这样做。 您的解决方案是90909.正确的解决办法是更接近100000(至少这是我发现的最高)

1

条件:

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

只会是真实的时候n2 == 100,并且看到作为n2只会是一个整数,当n1number,你while循环可能永远都不会结束。