20
A standard ruler of length n has distance marks at positions 0, 1, ..., n (in whichever units). A sparse ruler has a subset of those marks. A ruler can measure the distance k if it has marks at positions p and q with p−q=k.
The challenge
Given a positive integer n, output the minimum number of marks required in a sparse ruler of length n so that it can measure all distances 1, 2, ..., n.
This is OEIS A046693.
As an example, for input 6 the output is 4. Namely, a ruler with marks at 0, 1, 4, 6 works, as 1−0=1, 6−4=2, 4−1=3, 4−0=4, 6−1=5, and 6−0=6.
Additional rules
- The algorithm should be valid for arbitrarily large n. However, it is acceptable if the program is limited by memory, time or data type restrictions.
- Input/output can be taken/produced by any reasonable means.
- Programs or functions are allowed, in any programming language. Standard loopholes are forbidden.
- Shortest code in bytes wins.
Test cases
1 -> 2
2 -> 3
3 -> 3
4 -> 4
5 -> 4
6 -> 4
7 -> 5
8 -> 5
9 -> 5
10 -> 6
11 -> 6
12 -> 6
13 -> 6
14 -> 7
15 -> 7
16 -> 7
17 -> 7
18 -> 8
19 -> 8
20 -> 8
21 -> 8
22 -> 8
23 -> 8
24 -> 9
25 -> 9
26 -> 9
27 -> 9
28 -> 9
29 -> 9
30 -> 10
31 -> 10
32 -> 10
Related – Luis Mendo – 2017-11-26T18:25:56.627