A
A
Alexander Sitnik2016-11-28 20:00:08
Delphi
Alexander Sitnik, 2016-11-28 20:00:08

Please tell me where is the mistake? The task sounds like this: “Get all the prime divisors of a number”?

var n,i,k,count: integer;
begin
writeln('Введите натуральное число: ');
readln(n);
count:=0;
for i:=2 to n-1 do begin
                    if n mod i = 0 then begin
                                            for k:=1 to i-1 do
                                            if (i mod k = 0) then begin
                                              count+=1;
                                              if count<=2 then
                                                            writeln(i, ' ');
                                            end;
                                      end;
                         end;
                    
end.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey, 2016-11-28
@SitnikKsl

There seems to be an error at least here - k:=1 - any number is divisible by 1 without a remainder.

A
Armenian Radio, 2016-11-28
@gbg

count=0 must be after begin

M
Maria Maltseva, 2016-11-29
@Moriam

The error is that count is not cleared for before looping with k; well, k, as mentioned above, you need to start with 2, since any number is divisible by 1.
You can also start with i from 2 to (n / 2) - since there will be no further divisors
Well and k, respectively, from 2 to ( i/2)

var 
  n,i,k: integer;
  flagSimple: boolean;
begin
  writeln('Введите натуральное число: ');
  readln(n);
  for i:=2 to (n/2) do 
  begin
    if n mod i = 0 then 
    begin
      flagSimple := true;		//считаем пока, что i - простое (ну или count := 0, если использовать число)
      for k:=2 to (i/2) do
        if (i mod k = 0) then
        begin
          flagSimple := false;
          break;		//выходим из цикла for для k, так как обнаружили, что i - не простое
        end;
      if (flagSimple = true) then
        writeln(i, ' ');
    end;
  end;
            
end.

T
tsarevfs, 2016-11-28
@tsarevfs

var n, i, k, count: integer;
begin
writeln('Введите натуральное число: ');
readln(n);
count := 0;
for i := 2 to n - 1 do begin
    if n mod i = 0 then begin
        for k := 1 to i - 1 do // ищем делители делителя
            if (i mod k = 0) then begin //нашли очередной
                count += 1; //увеличили счетчик (и да, для 2 делителя начинать будем уже не с 0)
                if count <= 2 then //и сразу очень странная проверка 
                                   //мы не закончили поиск, значение счетчика не окончательное
                                   //да и почему 2, поиск то до i - 1
                    writeln(i, ' ');
            end;
    end;
end;

end.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question