Skip to content

Instantly share code, notes, and snippets.

@tabdelmaguid
tabdelmaguid / farmers-weights.scala
Created February 23, 2012 05:28
Farmer's weights
import scala.math._
def stone_weights(w: Int) : List[Int] = w match {
case 1 => List(1)
case _ =>
val one_third : Int = ceil((w-1).toDouble/3).toInt
w - one_third :: stone_weights (one_third)
}
println(stone_weights(40));
@tabdelmaguid
tabdelmaguid / primes.clj
Last active October 30, 2015 00:12
Primes Lazy Sequence in Clojure
(defn divisible-by? [n factor]
(= (mod n factor) 0))
(defn primes []
(letfn
[(next-primes [rest-of-the-numbers]
(let [[prime & rest] rest-of-the-numbers]
(cons prime
(lazy-seq
(next-primes (filter #(not (divisible-by? % prime)) rest))))))]