18
0
Definition
A positive integer n
is a practical number (OEIS sequence A005153) iff all smaller positive integers can be represented as sums of distinct divisors of n
.
For example, 18
is a practical number: its divisors are 1, 2, 3, 6, 9, and 18, and the other positive integers smaller than 18 can be formed as follows:
4 = 1 + 3 5 = 2 + 3 7 = 1 + 6
8 = 2 + 6 10 = 1 + 9 11 = 2 + 9
12 = 3 + 9 = 1 + 2 + 9 = 1 + 2 + 3 + 6
13 = 1 + 3 + 9 14 = 2 + 3 + 9 15 = 6 + 9
16 = 1 + 6 + 9 17 = 2 + 6 + 9
But 14
is not a practical number: its divisors are 1, 2, 7, and 14, and there's no subset of these which adds to 4, 5, 6, 11, 12, or 13.
Challenge
Write a program, function, or verb which takes as input a positive integer x
and either returns or prints the xth practical number, indexed from 1 for consistency with OEIS. Your code must be sufficiently efficient that it can handle inputs up to 250000 in less than two minutes on a reasonable desktop computer. (NB my reference implementation in Java manages 250000 in less than 0.5 seconds, and my reference implementation in Python manages it in 12 seconds).
Test cases
Input Expected output
1 1
8 18
1000 6500
250000 2764000
1000000 12214770
3000000 39258256
(IMHO) this can be even move interesting if the fastest code (per language?) wins – Display Name – 2014-03-15T18:09:34.987
4@SargeBorsch So you'll see tables of 250K entries all over the answers – Dr. belisarius – 2014-03-15T22:37:00.303
@belisarius good point. but I think such cheating can be easily banned. Or the problem may require correct answers for any number, but then there would be difficulties when doing it in a language with no big integers in the standard library... :/ – Display Name – 2014-03-15T22:45:24.663
I have one algorithmic optimization in mind, but with current rules I'm too lazy to implement it :P – Display Name – 2014-03-15T22:46:32.117
4@SargeBorsch, if you don't want to golf your code feel free to upload it to something like gist.github.com and drop a link in a comment here or in chat. FWIW I prefer code golf with generous performance constraints to fastest code for two reasons: firstly, the length of the code is more objectively measurable; secondly, it introduces an element of tradeoff: which speed optimisations can be left out in order to shorten the code without ruining the performance? – Peter Taylor – 2014-03-15T23:19:38.227