Add some arithmetic fuctions
[TD_LISP.git] / exercices / arithmetic.lsp
diff --git a/exercices/arithmetic.lsp b/exercices/arithmetic.lsp
new file mode 100755 (executable)
index 0000000..85f3f01
--- /dev/null
@@ -0,0 +1,30 @@
+#!/usr/bin/env newlisp
+
+(define (Puissance P N)
+  (cond
+    ((= N 0) 1)
+    ((= N 1) P)
+    ((< N 0) (div 1 (Puissance P (- N))))
+    ((* P (Puissance P (- N 1))))))
+(println (Puissance 5 5))
+
+; https://fr.wikipedia.org/wiki/Algorithme_d%27Euclide
+(define (pgcd N P) 
+  (cond
+    ((< N P) (pgcd P N))
+    ((= N P) N)
+    ((= P 0) N)
+    ((pgcd (- N P) P))))
+(println (pgcd 12 4))
+(println (pgcd 25 5))
+(println (pgcd 21 7))
+
+; https://fr.wikipedia.org/wiki/Coefficient_binomial
+(define (comb N P) 
+  (cond
+    ((= P 0) 1)
+    ((= N P) 1)
+    ((+ (comb (- 1 N) P) (comb (- 1 N) (- 1 P))))))
+;(println (comb 5 4))
+
+(exit)