PROGRAM:
- mamy tablice wypełnioną liczbami naturalnymi > 1
- startujemy z pola o indeksie 1
- cel dotrzeć do pola o indeksie MAX
*RUCHY:- gdy stoimy na polu gdzie jest L.PIERWSZA - cofamy sie o 1 pole
- gdy L.ZLOZONA to możemy skoczyć w prawo o liczbę pól będącą czynnikiem pierwszym tej liczby
- mamy k ruchow
uses crt;
const
max = 10;
type
taba = array [1..max] of integer;
var
i:integer;
tab: taba;
function czy_pierwsza(n:integer):boolean;
var
i:integer;
flaga : boolean;
begin
i:=2;
flaga:=true; //zakladamy ze jest l.pierwsza
if n=1 then flaga:=false;
while (i<=sqrt(n)) and (flaga=true) do begin
if n mod i = 0 then flaga:=false;
i:=i+1;
end;
czy_pierwsza:=flaga;
end;
function gra(var t:taba; ind,k:integer):boolean;
var a,b:integer;
begin
if (k=0) and (ind<>max) then gra:=false {NIE MAMY RUCHOW I NIE JESTESMY NA KONCY}
else if ind=max then gra:=true {DOSZLISMY DO KONCA}
else if (ind>max) or (ind<1) then gra:=false {PRZEKROCZYLISMY TABLICE}
else if (czy_pierwsza(ind)=true) then gra:=gra(t,ind-1,k-1) {L.PIERWSZA -> COFAMY SIE}
else
begin
{liczba jest zlozona}
a:=t[ind];
b:=2;
while a>0 do begin
if a mod b=0 then
begin
while (a mod b = 0) do a:=a div b;
gra:=gra(t, ind+b,k-1)
end;
b:=b+1;
end;
end;
end;
begin
{LOSOWANIE I WYPISANIE TABLICY DO GRY}
randomize;
for i:=1 to max do
tab[i]:=random(6)+2;
for i:=1 to max do write(i, ' ');
writeln;
for i:=1 to max do write(tab[i], ' ');
if gra(tab,1,5)=true then writeln('da sie') else writeln('nie da sie');
end.
Gdzie popełniłem błąd? Jak zmienić program, aby wypisywał rozwiązanie?
Z góry dziękuję za pomoc.