85f3f0145e446732be437de2f02cb280541aa8f9
[TD_LISP.git] / exercices / arithmetic.lsp
1 #!/usr/bin/env newlisp
2
3 (define (Puissance P N)
4 (cond
5 ((= N 0) 1)
6 ((= N 1) P)
7 ((< N 0) (div 1 (Puissance P (- N))))
8 ((* P (Puissance P (- N 1))))))
9 (println (Puissance 5 5))
10
11 ; https://fr.wikipedia.org/wiki/Algorithme_d%27Euclide
12 (define (pgcd N P)
13 (cond
14 ((< N P) (pgcd P N))
15 ((= N P) N)
16 ((= P 0) N)
17 ((pgcd (- N P) P))))
18 (println (pgcd 12 4))
19 (println (pgcd 25 5))
20 (println (pgcd 21 7))
21
22 ; https://fr.wikipedia.org/wiki/Coefficient_binomial
23 (define (comb N P)
24 (cond
25 ((= P 0) 1)
26 ((= N P) 1)
27 ((+ (comb (- 1 N) P) (comb (- 1 N) (- 1 P))))))
28 ;(println (comb 5 4))
29
30 (exit)