I have AC with such idea but I don't understand how to get a formula or faster solution

var

i,n,l,j:longword; found:boolean;

begin

readln(n);

repeat

found:=False;

for j:=3 to n div 2 do

begin

if n mod j=0 then

begin

found:=True;

break;

end;

end;

if found then n:=j;

until not found;

writeln(n-1);

readln;

end.

Please help me to optimize my code. It's 0.405 sec

Re: I have AC with such idea but I don't understand how to get a formula or faster solution

Posted by

c_pp 17 Dec 2016 23:35

for j := 3 to sqrt(n) do will help you :)

Re: I have AC with such idea but I don't understand how to get a formula or faster solution

Posted by

PO 18 Jan 2019 12:11

> for j := 3 to sqrt(n) do will help you :)

will it? for sqrt(8) = 2.8, but answer should be 4 - 1 = 3