求100以内的质数的函数,,,,为什么是错的?procedure TForm2.Button1Click(Sender: TObject);
var
    I,J:Integer;
begin
    for I :=1  to 100 do
    begin
        for J := 2 to Sqr(I)  do
        begin
            if (I div J=0) and (I<=J) then
                Memo1.Lines.Add(IntToStr(I));
        end; 
    end;
end;

解决方案 »

  1.   

    质数是只能被1和自己整除没有余数的数 例7和11>>if (I div J=0) and (I<=J) then不应该用DIV,用mod
      

  2.   

    经过改正,终于搞定了...谢各位...现公布答案....function TForm2.ZZZ(x,y:Integer):Integer ;
    var
        I,J:Integer;
        flag:BOOL;
    begin
        IF (X=0) or (X=1) or (X<0) then X:=2;  //检查纠正
        for I :=x  to y do
        begin
            flag:=true;
            for J := 2 to trunc(Sqrt(I))  do
            begin
                if (I mod J)=0 then
                begin
                    flag:= False;
                    Next;
                end;
            end;
            if flag then
                memo1.Lines.Add(IntToStr(I)); 
        end;
    end;调用
    ZZZ(0,10000000);
      

  3.   

    我正想着呢,怎是sqr(平方)而不是sqrt(平方根)?问题既然解决了就不多看了。至于质数的作用,数学家做研究时并没想到怎么实用,他们只管去探索。不过现在质数的一个最显著的作用就在加密上。现在的加密技术依赖于非常大的质数,因为算法是公开的。(所以某些人对科学家做一些没有实用的研究的质疑实在是眼光短浅,我们必须支持“不实用”的科学研究)。--
    http://www.agui.googlepages.com
    mailto: agui.cn(a)gmail.com
      

  4.   

    顶  agui(阿贵: 高级图形用户界面) ~~~~