Lucky Numbers
March 18, 2014
We store the sieve in a list, initialized with only the odd positive integers to save the first step in the iteration:
(define (lucky n)
(let loop ((i 1) (sieve (range 1 n 2)))
(let ((p (list-ref sieve i)))
(if (< (length sieve) p) sieve
(loop (+ i 1) (del sieve p))))))
The del
function removes every k‘th item from a list:
(define (del xs k)
(let loop ((i 1) (xs xs) (zs (list)))
(if (null? xs) (reverse zs)
(if (zero? (modulo i k))
(loop (+ i 1) (cdr xs) zs)
(loop (+ i 1) (cdr xs) (cons (car xs) zs))))))
Here’s the list from oeis.org:
> (lucky 304)
(1 3 7 9 13 15 21 25 31 33 37 43 49 51 63 67 69 73 75 79 87
93 99 105 111 115 127 129 133 135 141 151 159 163 169 171
189 193 195 201 205 211 219 223 231 235 237 241 259 261 267
273 283 285 289 297 303)
We used range
from the Standard Prelude. You can run the program at http://programmingpraxis.codepad.org/Dtz9yq59.
Can anybody give me link for the proof for probability that a given number ‘n’ is prime is (1 / loge n)?
@Tejas: That’s the Prime Number Theorem, first conjectured by Gauss when he was fourteen years old and proved later. Wikipedia and MathWorld are good places to start your study.
Thanks a lot..
I’m almost ashamed at showing my convoluted Haskell versions but here is one anyway (it computes all lucky numbers):
Are you sure that Haskell version is right? This is my one: