如何检查德尔福的素数 [英] How to check for a prime number in Delphi

查看:148
本文介绍了如何检查德尔福的素数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将1到10000之间的所有素数读入动态数组,将所有非素数读入另一个动态数组,然后将素数数组读入 richedit1 我有:

  procedure primearrays; 
var
j,k,l,i,m:integer; // k是我正在测试素数
// j用于for循环来检查所有小于k的数字,以查看k是否可以被j
// l只是一个变量设置为k mod j,使得如果运行更顺畅
//我是数组的长度anotprime
// m用于设置数组的长度aprime
bflag:布尔值// bflag是要显示这个数字是否是素数
aprime,anotprime:整数数组;
// aprime是prime的数组,而anotprime是非数组数组的数组
begin
j:= 0;
i:= 0;
l:= 0;
richedit1.Lines.Clear;
bflag:= false;
for k:= 2到10000 do
begin
j:= 0;
while not(j =(k-1))do
begin
inc(j);
l:= k mod j;
if(l = 0)then
begin
bflag:= false;
inc(i);
setlength(anotprime,i);
anotprime [i - 1]:= k;
j:= k - 1;
end
else
begin
bflag:= true;
结束
结束
m:= -1;
if(bflag)then
begin
inc(m);
setlength(aprime,m);
aprime [m - 1]:= k;
richedit1.Lines.Add(inttostr(aprime [l-1]));
结束
结束
结束

但这似乎不起作用。它将所有整数放入 anotprime 中。

解决方案

也许主要问题你是将所有不同的方面(循环候选,主要检查,显示等)组合成一个功能。



如果将任务分解成只执行单个任务的小块,生活将变得更加容易。从一个函数开始测试一个数字是否为素数。

  function IsPrime(N:Integer):Boolean; 
var
M:整数;
begin
Assert(N> 0);
如果N = 1则//烦人的特殊情况
begin
结果:= False;
退出;
结束
对于M:= 2到(N div 2)do
如果N mod M = 0,那么
begin
结果:= False;
退出;
结束
结束
结果:= True;
结束

现在您可能希望列出一个包含素数的列表:

  var 
Primes:TList< Integer> ;;
N:整数;
....
//创建Primes
N:= 1到10000 do
如果IsPrime(N)然后
Primes.Add(N);

这不是枚举素数的最有效的方法。但是这可能是您应该开始的地方,我主要编写这个答案,鼓励您将代码分解成具有特定重点任务的小型逻辑方法。


I want to read all prime numbers between 1 and 10000 into a dynamic array and all non-prime numbers into another dynamic array and then read the prime array into richedit1 so far I have:

procedure primearrays;
var
  j, k, l, i, m: integer; // k is the number I am testing for prime number
  // j is used in the for loop to check all numbers smaller than k to see if k is dividable by j
  // l is just a variable set to k mod j to make the if run more smoothly
  // i is the length of the array anotprime
  // m is used to set the length of the array aprime
  bflag: boolean; // bflag is to show if this number is a prime number
  aprime, anotprime: array of integer;
  // aprime is the array of prime and anotprime is the array of nonprime numbers
begin
  j := 0;
  i := 0;
  l := 0;
  richedit1.Lines.Clear;
  bflag := false;
  for k := 2 to 10000 do
  begin
    j := 0;
    while not(j = (k - 1)) do
    begin
      inc(j);
      l := k mod j;
      if (l = 0) then
      begin
        bflag := false;
        inc(i);
        setlength(anotprime, i);
        anotprime[i - 1] := k;
        j := k - 1;
      end
      else
      begin
        bflag := true;
      end;
    end;
    m := -1;
    if (bflag) then
    begin
      inc(m);
      setlength(aprime, m);
      aprime[m - 1] := k;
      richedit1.Lines.Add(inttostr(aprime[l-1]));
    end;
  end;
end;

but this doesn't seem to work. It puts all the integers into anotprime.

解决方案

Perhaps the main problem you have is that you mix all the different aspects (looping over candidates, prime checking, display etc.) into a single function.

Life becomes much easier if you decompose the task into small pieces that just perform a single task. Start with a function to test if a number is prime.

function IsPrime(N: Integer): Boolean;
var
  M: Integer;
begin
  Assert(N > 0);
  if N = 1 then // annoying special case
  begin
    Result := False;
    exit;
  end;
  for M := 2 to (N div 2) do
    if N mod M = 0 then
    begin
      Result := False;
      exit;
    end;
  end;
  Result := True;
end;

Now you might wish to make a list containing primes:

var 
  Primes: TList<Integer>;
  N: Integer;
....
// create Primes
for N := 1 to 10000 do
  if IsPrime(N) then
    Primes.Add(N);

This is not the most efficient way to enumerate primes. But it's probably where you should start and I've mainly written this answer to encourage you to separate code into small logical methods that do specific focused tasks.

这篇关于如何检查德尔福的素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆