4 (define (Puissance1 P N)
8 ((< N 0) (div 1 (Puissance1 P (- N))))
9 ((* P (Puissance1 P (- N 1))))))
10 (println "Puissance1")
11 (println (Puissance1 5 5))
12 (println (Puissance1 2 12))
17 (define (Puissance2 P N)
23 ((= (mod N 2) 0) (Puissance2 (Puissance2 P 2) (/ N 2)))
24 ((* P (Puissance2 (Puissance2 P 2) (/ (- N 1) 2))))))))
25 (println "Puissance2")
26 (println (Puissance2 5 5))
27 (println (Puissance2 2 12))
31 ; https://fr.wikipedia.org/wiki/Algorithme_d%27Euclide
45 ; https://fr.wikipedia.org/wiki/Coefficient_binomial
46 ; relation de pascal commenté
51 ;((+ (comb (- N 1) P) (comb (- N 1) (- P 1))))))
52 ((/ (* N (comb (- N 1) (- P 1))) P))))
56 (println "(comb 12 8) = "(comb 12 8))
61 (setq L '(3 7 + 4 2 + *))
62 (setq M '(4 3 7 + * 2 -))
63 (setq N '(10 10 5 / +))
64 (define (calculExp P L)
67 ; all these conditions could probably be simplified
68 ((= (first L) '+) (calculExp (cons (+ (P 1) (first P)) (rest (rest P))) (rest L)))
69 ((= (first L) '-) (calculExp (cons (- (P 1) (first P)) (rest (rest P))) (rest L)))
70 ((= (first L) '*) (calculExp (cons (* (P 1) (first P)) (rest (rest P))) (rest L)))
71 ;FIXME: test for divide by zero
72 ((= (first L) '/) (calculExp (cons (/ (P 1) (first P)) (rest (rest P))) (rest L)))
73 ((number? (first L)) (calculExp (cons (first L) P) (rest L)))))
75 (println (calculExp '() L))
77 (println (calculExp '() M))
78 (println (calculExp '() N))
82 (setq Q '(+ (* x 0) (* 10 (+ y 0))))
83 (define (algsimplificator L)
86 ; I'm having hard time to find a way of escaping the '(' and ')' characters
87 ((= (first L) ) (rest L))
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 known 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.
93 (println "algsimplificator")
94 ;(println algsimplificator(Q))
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)))
109 (define (fibo:fibo n)
110 (if (not fibo:mem) (set 'fibo:mem '(0 1)))
112 ;this create a LIFO (or stack) of all previous fibonnaci serie result values
113 (push (+ (fibo:mem -1) (fibo:mem -2)) fibo:mem -1))
117 (println (time (fibo 20)))
123 (define (Somme3ou5 N)
124 (dolist (i (sequence 0 N))
126 ((= (mod i 3) 0) (setq S (cons i S)))
127 ((= (mod i 5) 0) (setq S (cons i S)))))
129 (println "Somme3ou5")
130 (println (Somme3ou5 100))