In a series of natural numbers, we must remove every second element in the 1st pass. Then, in the remaining elements, delete every third element in the second pass. Then, on the Kth pass, delete each (k + 1) th element from the remaining elements.
The series will look as follows
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, ...
After the first pass (after removing every second element)
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, ...
After the second passage (after the removal of every third element)
1, 3, 7, 9, 13, 15, 19, 21, 25, 27, ...
After the third pass (after removing every 4th element)
1, 3, 13, 15, 19, 25, 27, ...
So, after going through infinity, he will become
1, 3, 7, 13, 19, 27, 39, 49, 63, 79, ...
This series is also called the Flavius ββJoseph sieve.
The solution for this is to find the 6th element in a row:
- do 6 ^ 2 = 36
- go down to a multiple of 5, giving 35
- then to a multiple of 4 = 32
- then to a multiple of 3 = 30
- then to a multiple of 2 = 28
- then to a multiple of 1 = 27
- and therefore the sixth lucky number is 27.
Although it works, I donβt understand how the solution works?
Program C for this,
int calc(int n) { if (n == 1) return 1; return calc_rec(n*n, n-1); } int calc_rec(int nu, int level) { int tmp; if (level == 1) return (nu-1); tmp = nu % level; return calc_rec(nu - (tmp ? tmp : level), level-1); }
Link explaining this http://oeis.org/A000960