So i have this code
program test;
var a, b, k, i, aux, j :integer;
ok :boolean;
begin
write('a=');
readln(a);
write('b=');
readln(b);
if a > b then
begin
aux := a;
a := b;
b := aux;
end;
for i := a to b do
begin
ok := true;
{ finding the first divizor }
k := 2;
while i mod k <> 0 do
k := k + 1;
if k*k = i then { if the number i is pp }
begin
for j := 2 to trunc(sqrt(k)) do { check if that k is prime }
if k mod j = 0 then
begin
ok := false;
break
end;
end
else
ok := false;
if ok then
writeln(i);
end;
writeln;
end.
And it should print out the number between a
and b
that are perfect squares of an prime number.
Example
a=1
b=40
The output should be :
4
9
25
25 = 5 * 5 -> is prime
9 = 3 *3 -> prime
4 = 2* 2 -> also prime
But i get this error :
Runtime error 216 at $0000000000400399
I know what it means ... and for some commenting and trying stuff I think it comes from this part of the code
begin
for j := 2 to trunc(sqrt(k)) do { check if that k is prime }
if k mod j = 0 then
begin
ok := false;
break
end;
end
But i don't get why ...
I really need a little help on this, I don't want a better algorithm I just wanna know why is this error occurring.
I'm using Free Pascal 2.6.2
Thanks!