Ad

Finds fibonacci number by given index using iteration

Code
Diff
  • (ns fibonacci)
    
    (defn get-fibonacci-number [index]
      "Finds fibonacci number by given index using iteration"
      (loop [x 0 y 1 z index]
        (if (< z 1) x (recur y (+ x y) (dec z)))))
    • (ns fibonacci)
    • (defn get-fibonacci-number [index]
    • "Finds fibonacci number by given index"
    • (if (<= index 0) 0)
    • (if (<= index 2) 1
    • (+ (get-fibonacci-number (- index 1))
    • (get-fibonacci-number (- index 2)))))
    • "Finds fibonacci number by given index using iteration"
    • (loop [x 0 y 1 z index]
    • (if (< z 1) x (recur y (+ x y) (dec z)))))

Write a generator that given a starting seed (which must be odd), will generate "random" numbers using the infamous RANDU generator, first value should be the seed itself.

Code
Diff
  • def randu_seq(x):
        while True:
            yield x
            x = x * 65539 % 0x80000000
    • (ns randu)
    • (defn randu-seq [x] (iterate #(mod (* 65539 %) 0x80000000) x))
    • def randu_seq(x):
    • while True:
    • yield x
    • x = x * 65539 % 0x80000000