Please tell me where is the error? The task sounds so: "to all the Prime factors of a number"?

var n,i,k,count: integer;
begin
writeln('Enter a natural number: ');
phony(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.
July 2nd 19 at 13:33
4 answers
July 2nd 19 at 13:35
Solution
Think the mistake at least here, k:=1 - any number is divisible by 1 without remainder.
That is, you need to write k:=2? - Narciso_Bogisich commented on July 2nd 19 at 13:38
: of course, you're in the previous cycle begin to look for divisors starting with two. - Daphne_Roob commented on July 2nd 19 at 13:41
k need in order to check whether the divisor is simple - Narciso_Bogisich commented on July 2nd 19 at 13:44
In the first cycle of looking for just dividers
Second check: are these factors Prime numbers - Narciso_Bogisich commented on July 2nd 19 at 13:47
Therefore, starting from 1 - Narciso_Bogisich commented on July 2nd 19 at 13:50
But I tried. Not Robit - Narciso_Bogisich commented on July 2nd 19 at 13:53
Although.... you "bypassed" the next divider in the condition of count<=2, so watch what advises. If you set k:=2, then count<=2 it will be necessary to replace the count=0 (no dividers). - Daphne_Roob commented on July 2nd 19 at 13:56
: Try this code:
var n,i,k,count: integer;
begin
writeln('Enter a natural number: ');
phony(n);
for i:=2 to n-1 do begin
 if n mod i = 0 then begin
count:=0;
 for k:=2 to i-1 do
 if (i mod k = 0) then begin
count+=1;
end;
 if count=0 then
 writeln(i, ' ');
end;
end;

end.
- Daphne_Roob commented on July 2nd 19 at 13:59
July 2nd 19 at 13:37
count=0 should be after begin
No, but if for example to enter the number 16, the program outputs 2 and 4, and 4 should not appear - Narciso_Bogisich commented on July 2nd 19 at 13:40
July 2nd 19 at 13:39
Error - count what not to clean before the cycle k, and k, as said above, you need to start with 2, since any number is divisible by 1.

Still i can start with 2 and up to (n/2) - since the divisor will not continue
Well, k, respectively, with 2 to (i/2)
var 
 n,i,k: integer;
 flagSimple: boolean;
begin
 writeln('Enter a natural number: ');
phony(n);
 for i:=2 to (n/2) do 
begin
 if n mod i = 0 then 
begin
 flagSimple := true; //while consider, that the i - simple (or count := 0, if used as number)
 for k:=2 to (i/2) do
 if (i mod k = 0) then
begin
 flagSimple := false;
 break; //exit the for loop for k, as found that i - not easy
end;
 if (flagSimple = true) then
 writeln(i, ' ');
end;
end;

end.
July 2nd 19 at 13:41
var n, i, k, count: integer;
begin
writeln('Enter a natural number: ');
phony(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 // search for divisors of the divider
 if (i mod k = 0) then begin //found another
 count += 1; //counter is increased (and Yes, 2 divider we begin not with 0)
 if count <= 2 then //and immediately a very strange test 
 //we haven't finished the search, the count is not final
 //why 2, search then to i - 1
 writeln(i, ' ');
end;
end;
end;

end.

Find more questions by tags PascalDelphi