首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Project Euler 4

Project Euler 4
EN

Stack Overflow用户
提问于 2012-07-22 04:43:02
回答 2查看 1K关注 0票数 1

我已经写了下面的代码,并且花了很多时间。但它本质上是有问题的,如果有人能指导我使它更有效率,我将万分感激。

它目前不产生任何输出。

代码语言:javascript
运行
复制
% 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 )
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-22 22:58:07

由于这是一个Euler项目,我只会给出一些一般性的建议。

  1. 要比较字符串,请使用strcmp,而不是您的方法(这会使代码更整洁),
  2. 请参阅Isaac的评论。添加floor和一个条件来检查数字是否为整数(log10不会这样做)
  3. 即使你输入了if语句,你也从来没有真正退出过它,因为while循环只会循环两个相同的数字。考虑通过修改while循环来终止while then和there。
  4. 虽然您的解决方案提供了一个结果,但它不是正确的,原因是根据您的代码,数字始终是999的倍数,这很可能是不正确的。更改构造number的方式。要这样做,您必须添加至少另一行定义数字。您的解决方案是90909。正确的解决方案接近100000 (至少这是我发现的最高值)
票数 1
EN

Stack Overflow用户

发布于 2012-07-22 06:40:55

条件:

代码语言:javascript
运行
复制
if( 1 + log10( n2 )  == 3 )

仅当为n2 == 100时才为真,并且由于n2仅在n1除以number时为整数,因此您的while循环可能永远不会结束。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11595697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档