Finds fibonacci number by given index using iteration
(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)))))
(ns fibonacci-test (:require [clojure.test :refer :all] [fibonacci :refer [get-fibonacci-number]])) (deftest get-fibonacci-number-test (is (= (get-fibonacci-number (- 10)) 0)) (is (= (get-fibonacci-number 0) 0)) (is (= (get-fibonacci-number 1) 1)) (is (= (get-fibonacci-number 2) 1)) (is (= (get-fibonacci-number 3) 2)) (is (= (get-fibonacci-number 4) 3)) (is (= (get-fibonacci-number 5) 5)) (is (= (get-fibonacci-number 10) 55)) (is (= (get-fibonacci-number 30) 832040)) (is (= (get-fibonacci-number 50) 12586269025)))
- (ns fibonacci-test
- (:require [clojure.test :refer :all]
- [fibonacci :refer [get-fibonacci-number]]))
- (deftest get-fibonacci-number-test
(is (get-fibonacci-number (- 10)) 0)(is (get-fibonacci-number 0) 0)(is (get-fibonacci-number 1) 1)(is (get-fibonacci-number 2) 1)(is (get-fibonacci-number 3) 2)(is (get-fibonacci-number 4) 3)(is (get-fibonacci-number 5) 5)(is (get-fibonacci-number 10) 55)(is (get-fibonacci-number 30) 832040))- (is (= (get-fibonacci-number (- 10)) 0))
- (is (= (get-fibonacci-number 0) 0))
- (is (= (get-fibonacci-number 1) 1))
- (is (= (get-fibonacci-number 2) 1))
- (is (= (get-fibonacci-number 3) 2))
- (is (= (get-fibonacci-number 4) 3))
- (is (= (get-fibonacci-number 5) 5))
- (is (= (get-fibonacci-number 10) 55))
- (is (= (get-fibonacci-number 30) 832040))
- (is (= (get-fibonacci-number 50) 12586269025)))
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.
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
Test.describe("Tests") rand = randu_seq(1) Test.assert_equals(str(type(rand))[7:-2], "generator", "randu_seq should be a generator") for i in xrange(3, 20, 2): Test.assert_equals(randu_seq(i).next(), i, "The first value returned must be the seed") Test.assert_equals([rand.next() for _ in xrange(21)], [1, 65539, 393225, 1769499, 7077969, 26542323, 95552217, 334432395, 1146624417, 1722371299, 14608041, 1766175739, 1875647473, 1800754131, 366148473, 1022489195, 692115265, 1392739779, 2127401289, 229749723, 1559239569], "randu_seq(1) doesn't match OEIS A096555!")
(ns randu-test(:require [clojure.test :refer :all][randu :refer [randu-seq]]))- Test.describe("Tests")
(deftest test-oeis-a096555(is(=(take 21 (randu-seq 1))[1 65539 393225 1769499 7077969 2654232395552217 334432395 1146624417 172237129914608041 1766175739 1875647473 1800754131366148473 1022489195 692115265 13927397792127401289 229749723 1559239569])"(randu-seq 1) doesn't match OEIS A096555!"))- rand = randu_seq(1)
- Test.assert_equals(str(type(rand))[7:-2], "generator", "randu_seq should be a generator")
- for i in xrange(3, 20, 2):
- Test.assert_equals(randu_seq(i).next(), i, "The first value returned must be the seed")
- Test.assert_equals([rand.next() for _ in xrange(21)], [1, 65539, 393225, 1769499, 7077969, 26542323, 95552217, 334432395, 1146624417, 1722371299, 14608041, 1766175739, 1875647473, 1800754131, 366148473, 1022489195, 692115265, 1392739779, 2127401289, 229749723, 1559239569], "randu_seq(1) doesn't match OEIS A096555!")