2012-07-21 126 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循環可能永遠都不會結束。