Commit 73543b93 by Vít Novotný

### added examples 1.3.1_18 and 1.3.2.19

parent 426fe2b7
1.3.1_18.pl 0 → 100644
 % nacteni: /* ['1.3.1_18.pl']. */ fib(0,0). fib(1,1). fib(X,Y) :- X1 is X-1, X2 is X-2, fib(X1,Y1), fib(X2,Y2), Y is Y1+Y2. % demonstracni vypis % abychom se vyhli varovanim "Redefined static procedure ..." :- dynamic write_all_X/3, start/0. write_all_X(Goal,X,Name):- call(Goal),write(' '),write(Name),write(' = '),write(X),nl,fail. write_all_X(_,_,_). start:- write('Program pro vypocet clenu fibonacciho posloupnosti, '),nl, write('mene efektivni varianta'),nl, write('Napr.:'),nl, write('Vypocet potrva nekolik vterin, '), write('Vysledek volani "fib(27, Vysledek)." je:'),nl, fib(27, Vysledek), write(' Vysledek = '), write(Vysledek),nl. ?-start. :- retractall(write_all_X/3). :- retractall(start/0).
1.3.1_18.py 0 → 100644
 #!/usr/bin/env python # encoding=utf-8 (pep 0263) def fib(x): if x == 0: return 0 if x == 1: return 1 return fib(x-1) + fib(x-2) # demonstracni vypis if __name__ == "__main__": print("Program pro vypocet clenu fibonacciho posloupnosti, " + \ "mene efektivni varianta (vypocet potrva nekolik vterin).") print('Vysledek volani fib(36) je %d.' % fib(36))
1.3.2_19.pl 0 → 100644
 % nacteni: /* ['1.3.2_19.pl']. */ % predikat fib musi byt dynamicky (kvuli asserta) :- dynamic fib/2. fib(0,0). fib(1,1). fib(X,Y) :- X1 is X-1, X2 is X-2, fib(X1,Y1), fib(X2,Y2), Y is Y1+Y2, asserta(fib(X,Y)). % demonstracni vypis % abychom se vyhli varovanim "Redefined static procedure ..." :- dynamic start/0. start:- write('Program pro vypocet clenu fibonacciho posloupnosti, '),nl, write('efektivnejsi varianta'),nl, write('Napr.:'),nl, write('Vysledek volani "fib(27, Vysledek)." je:'),nl, fib(27, Vysledek), write(' Vysledek = '), write(Vysledek),nl, write('Pomoci volani "listing." se mj. muzete podivat na fakty '),nl, write('ulozene do databaze pomoci asserta.'),nl. ?-start. :- retractall(start/0).
1.3.2_19.py 0 → 100644
 #!/usr/bin/env python # encoding=utf-8 (pep 0263) fib_mem = dict() def fib(x): if x == 0: return 0 if x == 1: return 1 if not x in fib_mem: fib_mem[x] = fib(x-1) + fib(x-2) return fib_mem[x] # demonstracni vypis if __name__ == "__main__": print("Program pro vypocet clenu fibonacciho posloupnosti, " + \ "efektivnejsi varianta.") print('Vysledek volani fib(36) je %d.' % fib(36))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment