[Delphi (Object Pascal)] Delphi 检查指定数字是否是质数 →→→→→进入此内容的聊天室

来自 , 2021-04-20, 写在 Delphi (Object Pascal), 查看 142 次.
URL http://www.code666.cn/view/04e299e2
  1. function IsPrime(N: Integer): Boolean;
  2. var
  3.   Max: Integer;     // max divisor
  4.   Divisor: Integer; // different divisors to try
  5. begin
  6.   Result := False;
  7.   if N < 2 then
  8.     Exit; // not a prime
  9.   Result := True;
  10.   if N = 2 then
  11.     Exit; // 2 is prime
  12.   if N mod 2 = 0 then
  13.     Result := False; // even numbers > 2 are not prime
  14.   Max := Trunc(Sqrt(N)) + 1;
  15.   Divisor := 3;
  16.   // test odd numbers
  17.   while (Max > Divisor) and Result do
  18.   begin
  19.     if (N mod Divisor) = 0 then
  20.       Result := False;
  21.     Inc(Divisor, 2); // next odd number
  22.   end;
  23. end;
  24. //delphi/2278

回复 "Delphi 检查指定数字是否是质数"

这儿你可以回复上面这条便签

captcha