0

我正在 FreePascal 中制作一个素数程序。

如何输出数字除以 I1,I2 用逗号而不是 2 行分隔?

var
  P:Integer;
  I:Integer;
  J:Integer;
  A:Integer;

  begin

    writeln('Prime number program');
    writeln;
    writeln('Insert number');
    readln(P);

    for I:=2 to P-1 do
    begin
      J:=P Mod I;
      if (J=0) then
      begin
        writeln(P,' divides with ',I);
        a:=a+1
      end;
    end;

    if a=0 then
      begin
      writeln(P,' is prime number')
      end;
  end.
4

3 回答 3

2

我知道这不是教你一些东西的好方法,但这里是:

program Project1;
uses
  SysUtils;
var
  S: string;
  I: Integer;
  Count: Integer;
  Input: Integer;
begin
  Writeln('Prime number program');
  Writeln;
  Writeln('Insert a number:');
  Readln(Input);

  for I := 2 to Input-1 do
  begin
    if (Input mod I = 0) then
    begin
      Count := Count + 1;
      if (S <> '') then
        S := S + ', ';
      S := S + IntToStr(I);
    end;
  end;

  if (Count = 0) then
    Writeln(Input, ' is a prime number.')
  else
    Writeln(Input, ' is not a prime number. It divides with ', S, '.');
  Readln;
end.
于 2013-02-03T14:48:36.867 回答
0
prime := true;
for i := 2 to p - 1 do
if(p mod i = 0) then prime := false;
if prime then writeln("prime");

所有程序:

program Prime;
var
  prime: boolean;
  I, P: Integer;
begin
  Writeln('Prime number program');
  Writeln;
  Writeln('Insert a number:');
  Readln(P);
  prime := true;


  for i := 2 to p - 1 do
  if(p mod i = 0) then prime := false;
  if prime then writeln("prime");
           else writeln("not prime");
  Readln;
end.
于 2013-02-03T15:47:51.187 回答
0

不需要检查所有低于 N 的数字,低于 sqrt(N) 的奇数就足够了。

program PrimeTest;

var N, I, Divisor: LongInt;
    Prime: Boolean;

begin
   writeln('Prime number program');
   writeln;
   write('Insert number: ');
   readln(N);

   Divisor := 1;
   Prime := True;
   if N < 5 then
   begin
      Prime := (N = 2) or (N = 3);
      if N = 4 then Divisor := 2;
   end else if Odd(N) then
   begin
      I := 3;
      while I*I <= N do
      begin
         if N mod I = 0 then
         begin
            Prime := False;
            Divisor := I;
            break;
         end;
         I := I + 2;
      end;
   end else begin
      Prime := False;
      Divisor := 2;
   end;

   if Prime then writeln(N, ' is a prime number')
            else writeln(N, ' is divisible by ', Divisor);
end.

当然,如果你想找到所有的质因数,或者所有的除数(不是同一件事),你需要一些更复杂的东西,但最好还是尽可能少做除数。

这是一个给出整个素数分解的解决方案。

program PrimeTest;

var N, I, J, Index, M: LongInt;
    Factor, Power: array[1 .. 32] of LongInt;

begin
   writeln('Prime number program');
   writeln;
   write('Insert number: ');
   readln(N);

   M := N;
   Index := 0;

   if N mod 2 = 0 then
   begin
      J := 0;
      while N mod 2 = 0 do
      begin
         Inc(J);
         N := N div 2;
      end;
      Inc(Index);
      Factor[Index] := 2;
      Power[Index] := J;
   end;

   I := 3;
   while I*I <= N do
   begin
      if N mod I = 0 then
      begin
         J := 0;
         while N mod I = 0 do
         begin
            Inc(J);
            N := N div I;
         end;
         Inc(Index);
         Factor[Index] := I;
         Power[Index] := J;
      end;
      I := I + 2;
   end;

   if (N > 1) or (Index = 0) then
   begin
      Inc(Index);
      Factor[Index] := N;
      Power[Index] := 1;
   end;

   if M = 1 then writeln('1 is not prime')
   else if (Index > 1) or ((Index > 0) and (Power[1] > 1)) then
   begin
      if Power[1] = 1 then write(M, ' = ', Factor[1])
                      else write(M, ' = ', Factor[1], '^', Power[1]);
      for I := 2 to Index do
      begin
         if Power[I] = 1 then write(' * ', Factor[I])
                         else write(' * ', Factor[I], '^', Power[I]);
      end;
      writeln;
   end else writeln(M, ' is prime');
end.
于 2013-02-10T13:02:31.437 回答