The ad shows a conceiled URL that we need to guess by computing the first 10-digit prime that appears in the series of consecutive digits of the irrational e. It is an oldie but goodie.
I thought I could solve this in Clojure. Here is my solution.
First we need to compute the number e with as many digits as necessary.
For that we can implementan unbounded spigot algorithm for the number e. I googled about this and found out a blog with superb material for this exercise, so I implemented the ideas in Clojure.
(defn digitse [N n] (if (= n N) (.setScale 2.5M 200)
(if (= n 0)
(+ 2.0M (* (.divide
(.setScale 1.0M 200 BigDecimal/ROUND_HALF_UP) 2.0M BigDecimal/ROUND_HALF_UP)
(digitse N (inc n) )) )
(+ 1.0M (* (.divide
(.setScale 1.0M 200 BigDecimal/ROUND_HALF_UP) (+ n 2.0M) BigDecimal/ROUND_HALF_UP)
(digitse N (inc n)))) ) ))
We then create a subsequence with sufficient decimal places. It turs out that 150 digits is enough. We also convert it to string to improve the partition of our 10-digit (now characters) strings.
(def e (digitse 150 0))We need to define a function that tells us wheter a given number is a prime. As we know, we need only test up to its square root, but here a simple loop over the 10-digit numbers show us that none of the square roots exceeds 89000, so we take the first 8500 prime numbers, starting in 2, from a prime number generator, that I took from here.
(def se (.subSequence (.toString e) 0 150 ))
(defn gen-primes "Generates an infinite, lazy sequence of prime numbers"Please note that this generator is pretty fast, compared to the answers that I have read on the internet. Now we take the primes we need from this lazy sequence.
[]
(let [reinsert (fn [table x prime]
(update-in table [(+ prime x)] conj prime))]
(defn primes-step [table d]
(if-let [factors (get table d)]
(recur (reduce #(reinsert %1 d %2) (dissoc table d) factors)
(inc d))
(lazy-seq (cons d (primes-step (assoc table (* d d) (list d))
(inc d))))))
(primes-step {} 2)))
(def first-primes (take 8500 (gen-primes) ) )And define a function to test that the condition that some remainder of a given number between any of the prime denominators is zero does not happen, which means that our number is prime.
(defn prime-restricted? [n first-primes]
(= nil (some #(= 0 (rem n %)) first-primes) ))
Finally, we recursivelly (with no stack overhead) until either a prime is found or we exceed the 150 digits capacity (in which case we would just increase it, but we don't need to).
(defn find-first-prime-in-e [se init first-primes]
(if (<= (- (.length se) init) 10)
nil
(let [number (java.lang.Long/parseLong (.subSequence se init (+ init 10)) )]
(println init number)
(if (prime-restricted? number first-primes)
number
(recur se (inc init) first-primes)
)))
)
The last output lines and the returned value of the previous function are
96 6642742746
97 6427427466
98 4274274663
99 2742746639
100 7427466391
7427466391
So, it turns out that the first 10-digit prime number is in place 100, which makes the URL
7427466391.com
Upon connection, you would get another quizz, which having in mind the quantity you just computed is fairly straighforward.