Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 项目4_Matlab - Fatal编程技术网

Matlab 项目4

Matlab 项目4,matlab,Matlab,我已经编写了下面的代码,并投入了大量的时间。但它本身就是有问题的,如果有人能善意地指导我提高效率,我将不胜感激 它目前不产生任何输出 % 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

我已经编写了下面的代码,并投入了大量的时间。但它本身就是有问题的,如果有人能善意地指导我提高效率,我将不胜感激

它目前不产生任何输出

% 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 )
条件:

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

只有当
n2==100
时才是真的,而当
n1
除以
number
时,n2才是整数,你的
循环可能永远不会结束。

因为这是一个Euler项目,我只给出一些一般性的建议

  • 要比较字符串,请使用strcmp,而不是您的方法(它有助于 清洁剂代码)

  • 见艾萨克的评论。添加
    floor
    和检查数字是否为整数的条件(log10不这样做)

  • 即使您输入if语句,您也永远不会真正退出它,因为while循环只是继续使用相同的两个数字循环。考虑通过修改while循环来终止while,然后在中断的情况下。

  • 虽然您的解决方案提供了一个结果,但它不是正确的结果,原因是根据您的代码,数字总是999的倍数,这很可能是错误的。更改构造编号的方式。为此,您必须至少添加一行定义数字。 您的解决方案是90909。正确的解决方案接近100000(至少是我发现的最高值)


  • Euler项目的要点是,你应该自己解决问题,而不是让别人替你解决。使用内置的探查器查看问题所在。在我的机器上短时间运行您的代码表示
    log10
    正在消耗您的时间。想办法简化重复进行的计算。例如,如果您想知道一个编号
    n2
    有三位数字,并且您知道它以三位数字开始并在减少,那么您真的需要记录该编号吗?不