Skip to content

Instantly share code, notes, and snippets.

@Arnauld
Last active April 5, 2016 14:38
Show Gist options
  • Save Arnauld/f517059ea2e0482ed216e0da349484ed to your computer and use it in GitHub Desktop.
Save Arnauld/f517059ea2e0482ed216e0da349484ed to your computer and use it in GitHub Desktop.
(prime-numbers.core/breaz (* 25 1000000))
(ns prime-numbers.core)
; http://clj-me.cgrand.net/2009/07/30/everybody-loves-the-sieve-of-eratosthenes/
(defn primes3 [max]
(let [enqueue (fn [sieve n factor]
(let [m (+ n (+ factor factor))]
(if (sieve m)
(recur sieve m factor)
(assoc sieve m factor))))
next-sieve (fn [sieve candidate]
(if-let [factor (sieve candidate)]
(-> sieve
(dissoc candidate)
(enqueue candidate factor))
(enqueue sieve candidate candidate)))]
(cons 2 (vals (reduce next-sieve {} (range 3 max 2))))))
(defn keep-next [filter-fn list]
(let [[_ xs] (reduce (fn [[p ys] e]
(if (filter-fn p)
[e (conj ys e)]
[e ys]))
[(first list) []]
(drop 1 list))]
xs))
(defn breaz [max]
(let [freqs (->> (sort (primes3 max))
(keep-next #(= 9 (mod % 10)))
(map #(mod % 10))
(frequencies))
total (reduce (fn [acc [_ v]] (+ acc v)) 0 freqs)
probas (map (fn [[k v]]
[k (float (* 100 (/ v total)))]) freqs)]
(println ">" freqs)
(println ">" total)
(println ">" probas)
(doseq [[k v] (sort-by (fn [[k _]] k) probas)]
(println k ":" (format "%.2f" v)))))
@Arnauld
Copy link
Author

Arnauld commented Apr 5, 2016

(prime-numbers.core/breaz (* 25 1000000))
> {3 100471, 1 131556, 7 91650, 9 67751}
> 391428
> ([3 25.66781] [1 33.609245] [7 23.414268] [9 17.308676])
1 : 33,61
3 : 25,67
7 : 23,41
9 : 17,31

@Arnauld
Copy link
Author

Arnauld commented Apr 5, 2016

The news

Most mathematicians would have assumed that a prime should have an equal chance of being followed by a prime ending in 1, 3, 7 or 9. But the problem is: “Among the first billion prime numbers, for instance, a prime ending in 9 is almost 65 percent more likely to be followed by a prime ending in 1 than another prime ending in 9.” Prime numbers wouldn’t be distributed evenly and randomly…

Think that's cool? Let's play!

Give us the probability for a prime number ending by 9 to be followed by a prime ending by 1 / 3 / 7 / 9. Only consider here numbers lesser than 25*10^6.
Be among the first three people to send us the right answer at talent@breaz.io and win a goodies pack (Hired branded t-shirt, mug and sunglasses)!

Game starts now!

Information source: Quanta Magazine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment