Padovan sequence: Difference between revisions

Added clojure version.
m (J: a few notes for the interested J novice)
(Added clojure version.)
Line 671:
 
The floor- and L-system-based functions match from P_0 to P_32.</pre>
 
=={{header|Clojure}}==
<lang clojure>(def padovan (map first (iterate (fn [[a b c]] [b c (+ a b)]) [1 1 1])))
 
(def pad-floor
(let [p 1.324717957244746025960908854
s 1.0453567932525329623]
(map (fn [n] (int (Math/floor (+ (/ (Math/pow p (dec n)) s) 0.5)))) (range))))
 
(def pad-l
(iterate (fn f [[c & s]]
(case c
\A (str "B" (f s))
\B (str "C" (f s))
\C (str "AB" (f s))
(str "")))
"A"))
 
(defn comp-seq [n seqa seqb]
(= (take n seqa) (take n seqb)))
 
(defn comp-all [n]
(= (map count (vec (take n pad-l)))
(take n padovan)
(take n pad-floor)))
 
(defn padovan-print [& args]
((print "The first 20 items with recursion relation are: ")
(println (take 20 padovan))
(println)
(println (str
"The recurrence and floor based algorithms "
(if (comp-seq 64 padovan pad-floor) "match" "not match")
" to n=64"))
(println)
(println "The first 10 L-system strings are:")
(println (take 10 pad-l))
(println)
(println (str
"The L-system, recurrence and floor based algorithms "
(if (comp-all 32) "match" "not match")
" to n=32"))))</lang>
 
{{out}}
 
<pre>The first 20 items with recursion relation are: (1 1 1 2 2 3 4 5 7 9 12 16 21 28 37 49 65 86 114 151)
 
The recurrence and floor based algorithms match to n=64
 
The first 10 L-system strings are:
(A B C AB BC CAB ABBC BCCAB CABABBC ABBCBCCAB)
 
The L-system, recurrence and floor based algorithms match to n=32</pre>
 
=={{header|Delphi}}==
Anonymous user