## Lucky Numbers

### March 18, 2014

We calculated Josephus numbers in a previous exercise. In today’s exercise, we study Lucky Numbers, which are those positive integers that survive the Sieve of Josephus. It works like this:

Start with the numbers 1 through *n*, where *n* is the desired limit of the sieve; we’ll illustrate with *n* = 20: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20. At the first step, remove every second number from the sieve, starting from the 0’th: this leaves 1, 3, 5, 7, 9, 11, 13, 15, 17, 19. At the second step, the second number in the list is 3, so remove every third number from the sieve, starting from the 0’th: this leaves 1, 3, 7, 9, 13, 15, 19. At the third step, the third number in the list is 7, so remove every seventh number from the sieve, starting from the 0’th: this leaves 1, 3, 7, 9, 13, 15. And so on. The lucky numbers are listed at A000959: 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, …. Lucky numbers share many properties with prime numbers, including the same asymptotic behavior as the primes: any given number *n* is lucky with probability 1 / log_{e} *n*, the same as any given number *n* is prime with probability 1 / log_{e} *n*.

Your task is to write a program that lists the lucky numbers less than *n*. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

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: