1

私は次のコードを書き、それに多くの時間を費やしました。しかし、それは本質的に間違っているだけです。誰かが親切にそれをより効率的にするように私に指示することができれば、私は無限に感謝するでしょう。

現在、出力は生成されません。

% 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 )
4

2 に答える 2

1

条件:

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

がの場合にのみ真になりn2 == 100、除算n2の場合は整数のみになるため、ループは終了しない可能性があります。n1numberwhile

于 2012-07-21T22:40:54.787 に答える
1

これはオイラープロジェクトなので、一般的なアドバイスのみを提供します。

  1. 文字列を比較するには、メソッドではなくstrcmpを使用します(コードがよりクリーンになります)

  2. アイザックのコメントを参照してください。数値が整数であるかどうかを確認するための条件を追加floor します(log10はそれを行いません)

  3. そのifステートメントを入力しても、whileループは同じ2つの数値でループし続けるため、実際に終了することはありません。whileループを変更して、whileを中断して終了することを検討してください。

  4. あなたの解決策は結果を提供しますが、それは正しいものではありません。理由は、コードに基づいて常に999の倍数であり、これはおそらく正しくありません。作成方法を変更しますnumber。そのためには、少なくとも別の行定義番号を追加する必要があります。あなたの解決策は90909です。正しい解決策は100000に近いです(少なくともそれは私が見つけた最高です)

于 2012-07-22T14:58:06.887 に答える