This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns orbits.core | |
(:require [clojure.core.async :as async :refer [<! >! <!! chan go-loop]])) | |
(defn print-primes [ch] | |
(dorun (repeatedly 35 #(println (<!! ch))))) | |
(defn gen-candidates [ch] | |
(go-loop [i 2] | |
(>! ch i) | |
(recur (inc i))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(use 'clojure.core.logic) | |
;;;; Library Functions...not my code ;;;; | |
(defn build-num [n] | |
(cond | |
(zero? n) () | |
(and (even? n) (not (zero? n))) (cons 0 (build-num (/ n 2))) | |
(odd? n) (cons 1 (build-num (/ (dec n) 2))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(fn [xs] | |
(let [n (count xs) | |
m (Math/pow 2 n)] | |
(for [i (range m)] | |
(for [j (range n) :when (bit-test i j)] | |
(nth xs j))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import collection.breakOut | |
val s = (0 until 1000000).toSet | |
def func(i: Int) = "" + i + i | |
Profiling.timed(Profiling.printTime("Jesper (original): ")){ | |
(s map { i => i -> func(i) }).toMap | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn palindrome? [x] | |
(= x (apply str (reverse x)))) | |
(defn palindromic-2-10? [x] | |
(let [dec (Integer/toString x) | |
bin (Integer/toString x 2)] | |
(and (palindrome? dec) (palindrome? bin)))) | |
(with-test | |
(defn prob-36 [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fibs = 1 : 2 : zipWith (+) fibs (tail fibs) | |
prob2 = sum $ takeWhile (< 4000000) $ filter even fibs | |
main = putStrLn (show prob2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
multiples :: [Int] -> [Int] | |
multiples = filter (\x -> (mod x 3) == 0 || (mod x 5) == 0) | |
main = putStrLn $ (show . sum . multiples) [3..999] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val divisors = List(11, 12, 13, 14, 15, 16, 17, 18, 19, 20) | |
def divisibleByAll(x:Int, divs:List[Int]):Boolean = divs match { | |
case Nil => true | |
case _ => if (x % divs.head == 0) { | |
return divisibleByAll(x, divs.tail) | |
} else { | |
return false | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fib(v1:Int, v2:Int): Stream[Int] = v1 match { | |
case 0 => Stream.cons(1,fib(1,0)) | |
case 1 => Stream.cons(2,fib(2,1)) | |
case _ => Stream.cons(v1+v2,fib(v1+v2,v1)) | |
} | |
val fibStream = fib(0,0) | |
fibStream.filter(_%2 == 0).takeWhile(_ <= 4000000).foldLeft(0)(_+_) //returns 4613732 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def digits (sorted-set 0 1 2 3 4 5 6 7 8 9)) | |
(defn nextLex [substr digits] | |
(let [lex (apply str substr digits)] | |
(lazy-cat | |
[lex] | |
(loop [s (vec lex) | |
d (sorted-set)] | |
(let [i (- (int (peek s)) 48) | |
d (conj d i)] |
NewerOlder