Add Somme3ou5 function
[TD_LISP.git] / exercices / arithmetic.lsp
CommitLineData
ada8f303
JB
1#!/usr/bin/env newlisp
2
c4cb602c
JB
3;O(N)
4(define (Puissance1 P N)
ada8f303
JB
5 (cond
6 ((= N 0) 1)
7 ((= N 1) P)
c4cb602c
JB
8 ((< N 0) (div 1 (Puissance1 P (- N))))
9 ((* P (Puissance1 P (- N 1))))))
10(println "Puissance1")
11(println (Puissance1 5 5))
7c69bee5 12(println (Puissance1 2 12))
c4cb602c
JB
13
14;(trace true)
15
16;O(log N)
17(define (Puissance2 P N)
18 (cond
c4cb602c 19 ((= N 1) P)
7c69bee5
JB
20 ((= N 2) (* P P))
21 ((> N 2)
c4cb602c
JB
22 (cond
23 ((= (mod N 2) 0) (Puissance2 (Puissance2 P 2) (/ N 2)))
7c69bee5 24 ((* P (Puissance2 (Puissance2 P 2) (/ (- N 1) 2))))))))
c4cb602c 25(println "Puissance2")
7c69bee5
JB
26(println (Puissance2 5 5))
27(println (Puissance2 2 12))
c4cb602c
JB
28
29;(trace nil)
ada8f303
JB
30
31; https://fr.wikipedia.org/wiki/Algorithme_d%27Euclide
32(define (pgcd N P)
33 (cond
34 ((< N P) (pgcd P N))
35 ((= N P) N)
36 ((= P 0) N)
37 ((pgcd (- N P) P))))
c4cb602c 38(println "PGCD")
ada8f303
JB
39(println (pgcd 12 4))
40(println (pgcd 25 5))
41(println (pgcd 21 7))
42
c4cb602c
JB
43;(trace true)
44
ada8f303 45; https://fr.wikipedia.org/wiki/Coefficient_binomial
7c69bee5 46; relation de pascal commenté
ada8f303
JB
47(define (comb N P)
48 (cond
49 ((= P 0) 1)
50 ((= N P) 1)
7c69bee5
JB
51 ;((+ (comb (- N 1) P) (comb (- N 1) (- P 1))))))
52 ((/ (* N (comb (- N 1) (- P 1))) P))))
c4cb602c
JB
53(println "comb")
54(println (comb 5 4))
55(println (comb 60 4))
7c69bee5 56(println "(comb 12 8) = "(comb 12 8))
c4cb602c
JB
57
58;(trace nil)
57122701 59;(trace true)
c4cb602c
JB
60
61(setq L '(3 7 + 4 2 + *))
57122701 62(setq M '(4 3 7 + * 2 -))
dc802a3e 63(setq N '(10 10 5 / +))
c4cb602c
JB
64(define (calculExp P L)
65 (cond
25450b84
JB
66 ((null? L) (first P))
67 ; all these conditions could probably be simplified
68 ((= (first L) '+) (calculExp (cons (+ (P 1) (first P)) (rest (rest P))) (rest L)))
57122701 69 ((= (first L) '-) (calculExp (cons (- (P 1) (first P)) (rest (rest P))) (rest L)))
25450b84 70 ((= (first L) '*) (calculExp (cons (* (P 1) (first P)) (rest (rest P))) (rest L)))
c4cb602c 71 ;FIXME: test for divide by zero
57122701 72 ((= (first L) '/) (calculExp (cons (/ (P 1) (first P)) (rest (rest P))) (rest L)))
a54a070c 73 ((number? (first L)) (calculExp (cons (first L) P) (rest L)))))
57122701
JB
74(println "calculExp")
75(println (calculExp '() L))
76;(trace true)
77(println (calculExp '() M))
dc802a3e
JB
78(println (calculExp '() N))
79
80;(trace nil)
81
82(setq Q '(+ (* x 0) (* 10 (+ y 0))))
83(define (algsimplificator L)
84 (cond
85 ((null? L) '())
a54a070c 86 ; I'm having hard time to find a way of escaping the '(' and ')' characters
dc802a3e 87 ((= (first L) ) (rest L))
a54a070c
JB
88 ;here is the idea: detect the lower well formed expression: begin with (op and finish with ) where op = + - * / and have only two parameters that are atoms.
89 ;then if it match a know pattern, simplify it by following the matching rule.
90 ;do it again on the upper layer recursively until we only have (op A B) that just match no known simplication rules.
dc802a3e
JB
91
92 ))
93(println "algsimplificator")
94;(println algsimplificator(Q))
95
96(define (fibonacci N)
97 (cond
98 ((= N 0) 0)
99 ((= N 1) 1)
100 ((> N 1) (+ (fibonacci (- N 1)) (fibonacci (- N 2))))))
101(println "fibonacci")
102;(println (fibonacci 21))
103;(println (fibonacci 14))
104(println (fibonacci 20))
105(println (time (fibonacci 20)))
106
107;(trace true)
108
109(define (fibo:fibo n)
110 (if (not fibo:mem) (set 'fibo:mem '(0 1)))
111 (dotimes (i (- n 1))
a54a070c 112 ;this create a LIFO (or stack) of all previous fibonnaci serie result values
dc802a3e
JB
113 (push (+ (fibo:mem -1) (fibo:mem -2)) fibo:mem -1))
114 (last fibo:mem))
115(println "fibo")
116(println (fibo 20))
117(println (time (fibo 20)))
57122701 118
a54a070c
JB
119;(trace nil)
120;(trace true)
121
122(setq S '())
123(define (Somme3ou5 N)
124 (dolist (i (sequence 0 N))
125 (cond
126 ((= (mod i 3) 0) (setq S (cons i S)))
127 ((= (mod i 5) 0) (setq S (cons i S)))))
128 (apply + S))
129(println "Somme3ou5")
130(println (Somme3ou5 100))
131
57122701 132;(trace nil)
ada8f303
JB
133
134(exit)