(ns fibonacci) (defn get-fibonacci-number [index] (cond (< index 10) (nth '(0 1 1 2 3 5 8 13 21 34) index 0) (odd? index) (let [k (quot index 2) fk (get-fibonacci-number k) fk1 (get-fibonacci-number (inc k))] (+' (*' fk fk) (*' fk1 fk1)) ) :else (let [k (quot index 2) fk (get-fibonacci-number k) fk1 (get-fibonacci-number (inc k))] (*' fk (-' (*' 2 fk1) fk)) ) ) )
- (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)))))- (cond (< index 10) (nth '(0 1 1 2 3 5 8 13 21 34) index 0)
- (odd? index) (let [k (quot index 2)
- fk (get-fibonacci-number k)
- fk1 (get-fibonacci-number (inc k))]
- (+' (*' fk fk) (*' fk1 fk1))
- )
- :else (let [k (quot index 2)
- fk (get-fibonacci-number k)
- fk1 (get-fibonacci-number (inc k))]
- (*' fk (-' (*' 2 fk1) fk))
- )
- )
- )
(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)) (is (= (get-fibonacci-number 500) 139423224561697880139724382870407283950070256587697307264108962948325571622863290691557658876222521294125N)) (is (= (get-fibonacci-number 5000) 3878968454388325633701916308325905312082127714646245106160597214895550139044037097010822916462210669479293452858882973813483102008954982940361430156911478938364216563944106910214505634133706558656238254656700712525929903854933813928836378347518908762970712033337052923107693008518093849801803847813996748881765554653788291644268912980384613778969021502293082475666346224923071883324803280375039130352903304505842701147635242270210934637699104006714174883298422891491273104054328753298044273676822977244987749874555691907703880637046832794811358973739993110106219308149018570815397854379195305617510761053075688783766033667355445258844886241619210553457493675897849027988234351023599844663934853256411952221859563060475364645470760330902420806382584929156452876291575759142343809142302917491088984155209854432486594079793571316841692868039545309545388698114665082066862897420639323438488465240988742395873801976993820317174208932265468879364002630797780058759129671389634214252579116872755600360311370547754724604639987588046985178408674382863125N)) (is (odd? (get-fibonacci-number 500000))) (is (odd? (get-fibonacci-number 5000000))) (is (odd? (get-fibonacci-number 7000001))))
- (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)))- (is (= (get-fibonacci-number 50) 12586269025))
- (is (= (get-fibonacci-number 500) 139423224561697880139724382870407283950070256587697307264108962948325571622863290691557658876222521294125N))
- (is (= (get-fibonacci-number 5000) 3878968454388325633701916308325905312082127714646245106160597214895550139044037097010822916462210669479293452858882973813483102008954982940361430156911478938364216563944106910214505634133706558656238254656700712525929903854933813928836378347518908762970712033337052923107693008518093849801803847813996748881765554653788291644268912980384613778969021502293082475666346224923071883324803280375039130352903304505842701147635242270210934637699104006714174883298422891491273104054328753298044273676822977244987749874555691907703880637046832794811358973739993110106219308149018570815397854379195305617510761053075688783766033667355445258844886241619210553457493675897849027988234351023599844663934853256411952221859563060475364645470760330902420806382584929156452876291575759142343809142302917491088984155209854432486594079793571316841692868039545309545388698114665082066862897420639323438488465240988742395873801976993820317174208932265468879364002630797780058759129671389634214252579116872755600360311370547754724604639987588046985178408674382863125N))
- (is (odd? (get-fibonacci-number 500000)))
- (is (odd? (get-fibonacci-number 5000000)))
- (is (odd? (get-fibonacci-number 7000001))))
(ns fizzbuzz.core) (defn fizz [_] "Fizz") (defn buzz [_] "Buzz") (defn fizz-buzz [_] "FizzBuzz") (defn self [x] x) (defn fizzbuzz [x] (map (comp eval list) (cycle (list self self fizz self buzz fizz self self fizz buzz self fizz self self fizz-buzz)) (range 1 (inc x)) ) )
- (ns fizzbuzz.core)
(defn divisible-by? [divisor number](zero? (mod number divisor)))- (defn fizz [_] "Fizz")
- (defn buzz [_] "Buzz")
- (defn fizz-buzz [_] "FizzBuzz")
- (defn self [x] x)
- (defn fizzbuzz [x]
(map #(cond(divisible-by? 15 %) "FizzBuzz"(divisible-by? 5 %) "Buzz"(divisible-by? 3 %) "Fizz":else %)(range 1 (inc x))))- (map (comp eval list)
- (cycle (list self self fizz self buzz fizz self self fizz buzz self fizz self self fizz-buzz))
- (range 1 (inc x))
- )
- )
(ns fizzbuzz.core-test (:require [clojure.test :refer :all] [fizzbuzz.core :refer :all])) (deftest a-test1 (testing "Test 1" (is (= (fizzbuzz 33) [ 1 2 "Fizz" 4 "Buzz" "Fizz" 7 8 "Fizz" "Buzz" 11 "Fizz" 13 14 "FizzBuzz" 16 17 "Fizz" 19 "Buzz" "Fizz" 22 23 "Fizz" "Buzz" 26 "Fizz" 28 29 "FizzBuzz" 31 32 "Fizz"])))) (deftest a-test2 (testing "Test 2" (is (= (fizzbuzz 133) [ 1 2 "Fizz" 4 "Buzz" "Fizz" 7 8 "Fizz" "Buzz" 11 "Fizz" 13 14 "FizzBuzz" 16 17 "Fizz" 19 "Buzz" "Fizz" 22 23 "Fizz" "Buzz" 26 "Fizz" 28 29 "FizzBuzz" 31 32 "Fizz" 34 "Buzz" "Fizz" 37 38 "Fizz" "Buzz" 41 "Fizz" 43 44 "FizzBuzz" 46 47 "Fizz" 49 "Buzz" "Fizz" 52 53 "Fizz" "Buzz" 56 "Fizz" 58 59 "FizzBuzz" 61 62 "Fizz" 64 "Buzz" "Fizz" 67 68 "Fizz" "Buzz" 71 "Fizz" 73 74 "FizzBuzz" 76 77 "Fizz" 79 "Buzz" "Fizz" 82 83 "Fizz" "Buzz" 86 "Fizz" 88 89 "FizzBuzz" 91 92 "Fizz" 94 "Buzz" "Fizz" 97 98 "Fizz" "Buzz" 101 "Fizz" 103 104 "FizzBuzz" 106 107 "Fizz" 109 "Buzz" "Fizz" 112 113 "Fizz" "Buzz" 116 "Fizz" 118 119 "FizzBuzz" 121 122 "Fizz" 124 "Buzz" "Fizz" 127 128 "Fizz" "Buzz" 131 "Fizz" 133])))) (deftest a-test3 (testing "Test 3" (is (= (fizzbuzz 555) [1 2 "Fizz" 4 "Buzz" "Fizz" 7 8 "Fizz" "Buzz" 11 "Fizz" 13 14 "FizzBuzz" 16 17 "Fizz" 19 "Buzz" "Fizz" 22 23 "Fizz" "Buzz" 26 "Fizz" 28 29 "FizzBuzz" 31 32 "Fizz" 34 "Buzz" "Fizz" 37 38 "Fizz" "Buzz" 41 "Fizz" 43 44 "FizzBuzz" 46 47 "Fizz" 49 "Buzz" "Fizz" 52 53 "Fizz" "Buzz" 56 "Fizz" 58 59 "FizzBuzz" 61 62 "Fizz" 64 "Buzz" "Fizz" 67 68 "Fizz" "Buzz" 71 "Fizz" 73 74 "FizzBuzz" 76 77 "Fizz" 79 "Buzz" "Fizz" 82 83 "Fizz" "Buzz" 86 "Fizz" 88 89 "FizzBuzz" 91 92 "Fizz" 94 "Buzz" "Fizz" 97 98 "Fizz" "Buzz" 101 "Fizz" 103 104 "FizzBuzz" 106 107 "Fizz" 109 "Buzz" "Fizz" 112 113 "Fizz" "Buzz" 116 "Fizz" 118 119 "FizzBuzz" 121 122 "Fizz" 124 "Buzz" "Fizz" 127 128 "Fizz" "Buzz" 131 "Fizz" 133 134 "FizzBuzz" 136 137 "Fizz" 139 "Buzz" "Fizz" 142 143 "Fizz" "Buzz" 146 "Fizz" 148 149 "FizzBuzz" 151 152 "Fizz" 154 "Buzz" "Fizz" 157 158 "Fizz" "Buzz" 161 "Fizz" 163 164 "FizzBuzz" 166 167 "Fizz" 169 "Buzz" "Fizz" 172 173 "Fizz" "Buzz" 176 "Fizz" 178 179 "FizzBuzz" 181 182 "Fizz" 184 "Buzz" "Fizz" 187 188 "Fizz" "Buzz" 191 "Fizz" 193 194 "FizzBuzz" 196 197 "Fizz" 199 "Buzz" "Fizz" 202 203 "Fizz" "Buzz" 206 "Fizz" 208 209 "FizzBuzz" 211 212 "Fizz" 214 "Buzz" "Fizz" 217 218 "Fizz" "Buzz" 221 "Fizz" 223 224 "FizzBuzz" 226 227 "Fizz" 229 "Buzz" "Fizz" 232 233 "Fizz" "Buzz" 236 "Fizz" 238 239 "FizzBuzz" 241 242 "Fizz" 244 "Buzz" "Fizz" 247 248 "Fizz" "Buzz" 251 "Fizz" 253 254 "FizzBuzz" 256 257 "Fizz" 259 "Buzz" "Fizz" 262 263 "Fizz" "Buzz" 266 "Fizz" 268 269 "FizzBuzz" 271 272 "Fizz" 274 "Buzz" "Fizz" 277 278 "Fizz" "Buzz" 281 "Fizz" 283 284 "FizzBuzz" 286 287 "Fizz" 289 "Buzz" "Fizz" 292 293 "Fizz" "Buzz" 296 "Fizz" 298 299 "FizzBuzz" 301 302 "Fizz" 304 "Buzz" "Fizz" 307 308 "Fizz" "Buzz" 311 "Fizz" 313 314 "FizzBuzz" 316 317 "Fizz" 319 "Buzz" "Fizz" 322 323 "Fizz" "Buzz" 326 "Fizz" 328 329 "FizzBuzz" 331 332 "Fizz" 334 "Buzz" "Fizz" 337 338 "Fizz" "Buzz" 341 "Fizz" 343 344 "FizzBuzz" 346 347 "Fizz" 349 "Buzz" "Fizz" 352 353 "Fizz" "Buzz" 356 "Fizz" 358 359 "FizzBuzz" 361 362 "Fizz" 364 "Buzz" "Fizz" 367 368 "Fizz" "Buzz" 371 "Fizz" 373 374 "FizzBuzz" 376 377 "Fizz" 379 "Buzz" "Fizz" 382 383 "Fizz" "Buzz" 386 "Fizz" 388 389 "FizzBuzz" 391 392 "Fizz" 394 "Buzz" "Fizz" 397 398 "Fizz" "Buzz" 401 "Fizz" 403 404 "FizzBuzz" 406 407 "Fizz" 409 "Buzz" "Fizz" 412 413 "Fizz" "Buzz" 416 "Fizz" 418 419 "FizzBuzz" 421 422 "Fizz" 424 "Buzz" "Fizz" 427 428 "Fizz" "Buzz" 431 "Fizz" 433 434 "FizzBuzz" 436 437 "Fizz" 439 "Buzz" "Fizz" 442 443 "Fizz" "Buzz" 446 "Fizz" 448 449 "FizzBuzz" 451 452 "Fizz" 454 "Buzz" "Fizz" 457 458 "Fizz" "Buzz" 461 "Fizz" 463 464 "FizzBuzz" 466 467 "Fizz" 469 "Buzz" "Fizz" 472 473 "Fizz" "Buzz" 476 "Fizz" 478 479 "FizzBuzz" 481 482 "Fizz" 484 "Buzz" "Fizz" 487 488 "Fizz" "Buzz" 491 "Fizz" 493 494 "FizzBuzz" 496 497 "Fizz" 499 "Buzz" "Fizz" 502 503 "Fizz" "Buzz" 506 "Fizz" 508 509 "FizzBuzz" 511 512 "Fizz" 514 "Buzz" "Fizz" 517 518 "Fizz" "Buzz" 521 "Fizz" 523 524 "FizzBuzz" 526 527 "Fizz" 529 "Buzz" "Fizz" 532 533 "Fizz" "Buzz" 536 "Fizz" 538 539 "FizzBuzz" 541 542 "Fizz" 544 "Buzz" "Fizz" 547 548 "Fizz" "Buzz" 551 "Fizz" 553 554 "FizzBuzz"]))))
- (ns fizzbuzz.core-test
- (:require [clojure.test :refer :all]
- [fizzbuzz.core :refer :all]))
- (deftest a-test1
- (testing "Test 1"
(is (= (fizzbuzz 16) [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14, "FizzBuzz", 16]))))- (is (= (fizzbuzz 33) [ 1 2 "Fizz" 4 "Buzz" "Fizz" 7 8 "Fizz" "Buzz" 11 "Fizz" 13 14 "FizzBuzz"
- 16 17 "Fizz" 19 "Buzz" "Fizz" 22 23 "Fizz" "Buzz" 26 "Fizz" 28 29 "FizzBuzz"
- 31 32 "Fizz"]))))
- (deftest a-test2
- (testing "Test 2"
- (is (= (fizzbuzz 133) [ 1 2 "Fizz" 4 "Buzz" "Fizz" 7 8 "Fizz" "Buzz" 11 "Fizz" 13 14 "FizzBuzz"
- 16 17 "Fizz" 19 "Buzz" "Fizz" 22 23 "Fizz" "Buzz" 26 "Fizz" 28 29 "FizzBuzz"
- 31 32 "Fizz" 34 "Buzz" "Fizz" 37 38 "Fizz" "Buzz" 41 "Fizz" 43 44 "FizzBuzz"
- 46 47 "Fizz" 49 "Buzz" "Fizz" 52 53 "Fizz" "Buzz" 56 "Fizz" 58 59 "FizzBuzz"
- 61 62 "Fizz" 64 "Buzz" "Fizz" 67 68 "Fizz" "Buzz" 71 "Fizz" 73 74 "FizzBuzz"
- 76 77 "Fizz" 79 "Buzz" "Fizz" 82 83 "Fizz" "Buzz" 86 "Fizz" 88 89 "FizzBuzz"
- 91 92 "Fizz" 94 "Buzz" "Fizz" 97 98 "Fizz" "Buzz" 101 "Fizz" 103 104 "FizzBuzz"
- 106 107 "Fizz" 109 "Buzz" "Fizz" 112 113 "Fizz" "Buzz" 116 "Fizz" 118 119 "FizzBuzz"
- 121 122 "Fizz" 124 "Buzz" "Fizz" 127 128 "Fizz" "Buzz" 131 "Fizz" 133]))))
- (deftest a-test3
- (testing "Test 3"
- (is (= (fizzbuzz 555) [1 2 "Fizz" 4 "Buzz" "Fizz" 7 8 "Fizz" "Buzz" 11 "Fizz" 13 14 "FizzBuzz"
- 16 17 "Fizz" 19 "Buzz" "Fizz" 22 23 "Fizz" "Buzz" 26 "Fizz" 28 29 "FizzBuzz"
- 31 32 "Fizz" 34 "Buzz" "Fizz" 37 38 "Fizz" "Buzz" 41 "Fizz" 43 44 "FizzBuzz"
- 46 47 "Fizz" 49 "Buzz" "Fizz" 52 53 "Fizz" "Buzz" 56 "Fizz" 58 59 "FizzBuzz"
- 61 62 "Fizz" 64 "Buzz" "Fizz" 67 68 "Fizz" "Buzz" 71 "Fizz" 73 74 "FizzBuzz"
- 76 77 "Fizz" 79 "Buzz" "Fizz" 82 83 "Fizz" "Buzz" 86 "Fizz" 88 89 "FizzBuzz"
- 91 92 "Fizz" 94 "Buzz" "Fizz" 97 98 "Fizz" "Buzz" 101 "Fizz" 103 104 "FizzBuzz"
- 106 107 "Fizz" 109 "Buzz" "Fizz" 112 113 "Fizz" "Buzz" 116 "Fizz" 118 119 "FizzBuzz"
- 121 122 "Fizz" 124 "Buzz" "Fizz" 127 128 "Fizz" "Buzz" 131 "Fizz" 133 134 "FizzBuzz"
- 136 137 "Fizz" 139 "Buzz" "Fizz" 142 143 "Fizz" "Buzz" 146 "Fizz" 148 149 "FizzBuzz"
- 151 152 "Fizz" 154 "Buzz" "Fizz" 157 158 "Fizz" "Buzz" 161 "Fizz" 163 164 "FizzBuzz"
- 166 167 "Fizz" 169 "Buzz" "Fizz" 172 173 "Fizz" "Buzz" 176 "Fizz" 178 179 "FizzBuzz"
- 181 182 "Fizz" 184 "Buzz" "Fizz" 187 188 "Fizz" "Buzz" 191 "Fizz" 193 194 "FizzBuzz"
- 196 197 "Fizz" 199 "Buzz" "Fizz" 202 203 "Fizz" "Buzz" 206 "Fizz" 208 209 "FizzBuzz"
- 211 212 "Fizz" 214 "Buzz" "Fizz" 217 218 "Fizz" "Buzz" 221 "Fizz" 223 224 "FizzBuzz"
- 226 227 "Fizz" 229 "Buzz" "Fizz" 232 233 "Fizz" "Buzz" 236 "Fizz" 238 239 "FizzBuzz"
- 241 242 "Fizz" 244 "Buzz" "Fizz" 247 248 "Fizz" "Buzz" 251 "Fizz" 253 254 "FizzBuzz"
- 256 257 "Fizz" 259 "Buzz" "Fizz" 262 263 "Fizz" "Buzz" 266 "Fizz" 268 269 "FizzBuzz"
- 271 272 "Fizz" 274 "Buzz" "Fizz" 277 278 "Fizz" "Buzz" 281 "Fizz" 283 284 "FizzBuzz"
- 286 287 "Fizz" 289 "Buzz" "Fizz" 292 293 "Fizz" "Buzz" 296 "Fizz" 298 299 "FizzBuzz"
- 301 302 "Fizz" 304 "Buzz" "Fizz" 307 308 "Fizz" "Buzz" 311 "Fizz" 313 314 "FizzBuzz"
- 316 317 "Fizz" 319 "Buzz" "Fizz" 322 323 "Fizz" "Buzz" 326 "Fizz" 328 329 "FizzBuzz"
- 331 332 "Fizz" 334 "Buzz" "Fizz" 337 338 "Fizz" "Buzz" 341 "Fizz" 343 344 "FizzBuzz"
- 346 347 "Fizz" 349 "Buzz" "Fizz" 352 353 "Fizz" "Buzz" 356 "Fizz" 358 359 "FizzBuzz"
- 361 362 "Fizz" 364 "Buzz" "Fizz" 367 368 "Fizz" "Buzz" 371 "Fizz" 373 374 "FizzBuzz"
- 376 377 "Fizz" 379 "Buzz" "Fizz" 382 383 "Fizz" "Buzz" 386 "Fizz" 388 389 "FizzBuzz"
- 391 392 "Fizz" 394 "Buzz" "Fizz" 397 398 "Fizz" "Buzz" 401 "Fizz" 403 404 "FizzBuzz"
- 406 407 "Fizz" 409 "Buzz" "Fizz" 412 413 "Fizz" "Buzz" 416 "Fizz" 418 419 "FizzBuzz"
- 421 422 "Fizz" 424 "Buzz" "Fizz" 427 428 "Fizz" "Buzz" 431 "Fizz" 433 434 "FizzBuzz"
- 436 437 "Fizz" 439 "Buzz" "Fizz" 442 443 "Fizz" "Buzz" 446 "Fizz" 448 449 "FizzBuzz"
- 451 452 "Fizz" 454 "Buzz" "Fizz" 457 458 "Fizz" "Buzz" 461 "Fizz" 463 464 "FizzBuzz"
- 466 467 "Fizz" 469 "Buzz" "Fizz" 472 473 "Fizz" "Buzz" 476 "Fizz" 478 479 "FizzBuzz"
- 481 482 "Fizz" 484 "Buzz" "Fizz" 487 488 "Fizz" "Buzz" 491 "Fizz" 493 494 "FizzBuzz"
- 496 497 "Fizz" 499 "Buzz" "Fizz" 502 503 "Fizz" "Buzz" 506 "Fizz" 508 509 "FizzBuzz"
- 511 512 "Fizz" 514 "Buzz" "Fizz" 517 518 "Fizz" "Buzz" 521 "Fizz" 523 524 "FizzBuzz"
- 526 527 "Fizz" 529 "Buzz" "Fizz" 532 533 "Fizz" "Buzz" 536 "Fizz" 538 539 "FizzBuzz"
- 541 542 "Fizz" 544 "Buzz" "Fizz" 547 548 "Fizz" "Buzz" 551 "Fizz" 553 554 "FizzBuzz"]))))