Things to know:
First, lucky numbers.
Lucky numbers are generated like so:
Take all the natural numbers:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20...
Then, remove each second number.
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39...
Now, 3
is safe.
Remove every 3rd number:
1, 3, 7, 9, 13, 15, 19, 21, 25, 27, 31, 33, 37, 39, 43, 45, 49, 51, 55, 59...
Now, 7
is safe.
Remove every 7th number.
Continue, and remove every n
th number, where n
is the first safe number after an elimination.
The final list of safe numbers is the lucky numbers.
The unlucky numbers is composed of separate lists of numbers, which are [U1, U2, U3... Un]
.
U1
is the first set of numbers removed from the lucky "candidates", so they are:
2, 4, 6, 8, 10, 12, 14, 16, 18, 20...
U2
is the second set of numbers removed:
5, 11, 17, 23, 29, 35, 41, 47, 53, 59...
And so on and so forth (U3
is the third list, U4
is the fourth, etc.)
Challenge:
Your task is, when given two inputs m
and n
, generate the m
th number in the list Un
.
Example inputs and outputs:
(5, 2) -> 29
(10, 1) -> 20
Specs:
- Your program must work for
m
up to1e6
, andn
up to100
.- You are guaranteed that both
m
andn
are positive integers. - If you're curious,
U(1e6, 100)
=5,333,213,163
. (Thank you @pacholik!)
- You are guaranteed that both
- Your program must compute that within 1 day on a reasonable modern computer.
This is code-golf, so shortest code in bytes wins!
PS: It would be nice if someone came up with a general formula for generating these. If you do have a formula, please put it in your answer!
(1e6,1e6)
? \$\endgroup\$n=1
case? As this is special-- for all other cases, the 0-based index of the next lucky number isn-1
. \$\endgroup\$