1과 10000 사이의 모든 소수를 동적 배열로 읽고 모든 비 소수를 다른 동적 배열로 읽어 들이고 프라임 배열을 richedit1
으로 읽으려는 경우 :델파이에서 소수를 확인하는 방법
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;
그러나 이것은 작동하지 않는 것 같습니다. 모든 정수를 anotprime
에 저장합니다.
실제 시도한 코드는 아닙니다. 적어도, 당신은 거기에 오타가 있습니다 (변수'aprime'은'apriem'을 입력함으로써 참조 될 수 없습니다), 당신이 단순히 코드를 복사하여 붙여 넣지 않았다는 사실은 다른 오류가있을 가능성이 높습니다 거기도 원래 버전에서 잘못되지 않습니다. 그건 제쳐두고, "이것은 작동하지 않는 것"은 무엇을 의미합니까? 컴파일되지 않습니까? 잘못된 결과가 나옵니까? 예외가 발생합니까? 어떤 점에서 잘못 됐는지 알아 냈어? – hvd
코드에만 주석이 있다면. 최소한 * 접근법 (체 등)을 문서화하십시오. – Bathsheba
@hvd 사과드립니다.하지만 집에서 사용하는 언어로 프로그래밍해야하고 실수로 오타를 잊어 버렸지 만 즉시 수정 해줍니다. 프로그램은 정상적으로 모든 정수 값을 배열 ** anotprime **에 넣고 디버깅했지만 괜찮 았지만 실수가있는 곳을 알 수 없었다. – theunie