(这是A065825。)序列默认值适用,因此您可以选择除此格式之外的另一种格式。
给定一个输入整数n,找到最小的数字k,以便存在一个{1,...,k}的n项子集,其中没有三个项构成算术级数。
在这里,我们计算A065825(9)。
我们假设您已经从1循环到19,并且k=20 (这只是一个例子)。
的范围。
[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]n项。[1 2 6 7 9 14 15 18 20]如果序列有算术级数,它基本上意味着序列在每两个连续项之间都有相同的步骤。
例如,正偶数序列([2 4 6 8 ...])具有一致的步长(即4-2=2和6-4=2等),因此它具有算术级数。
Fibonacci序列(Fibonacci sequence,[1 1 2 3 5 8 13 21 ...])没有算术级数,因为它没有一致的步骤。(3-2=1,5-3=2,8-5=3等)
例如,让我们从生成的序列中选择3个项。
[1 2 6 [7 9 14] 15 18 20]选择的3项序列没有算术级数,因为它们分别是9-7=2和14-9=5。
这必须适用于每一对3项:
[[1 2 6] 7 9 14 15 18 20] (2 -1 =1, 6 -2 =4)
[1 [2 6 7] 9 14 15 18 20] (6 -2 =4, 7 -6 =1)
[1 2 [6 7 9] 14 15 18 20] (7 -6 =1, 9 -7 =2)
[1 2 6 [7 9 14] 15 18 20] (9 -7 =2, 14-9 =5)
[1 2 6 7 [9 14 15] 18 20] (14-9 =5, 15-14=1)
[1 2 6 7 9 [14 15 18] 20] (15-14=1, 18-15=3)
[1 2 6 7 9 14 [15 18 20]] (18-15=3, 20-18=2)下面是从输出序列中选择非连续项的一些示例:
[1 [2] 6 [7] 9 [14] 15 18 20] (7-2=5,14-7=7)
[[1] 2 6 [7] [9] 14 15 18 20] (7-1=6,9 -7=2)如果对k满足上述条件,那么k是A065825(9)的有效输出。
这里是我用来检查测试用例的参考程序。
n a(n)
1 1
2 2
3 4
4 5
5 9
6 11
7 13
8 14
9 20发布于 2020-04-29 19:26:28
œcœc3IEƇƊÐḟð1#一种接受非负整数的一元链接,产生非负整数.
在网上试试! (对于60年代内的n=9来说太低效了)。或者去看测试套件。
œcœc3IEƇƊÐḟð1# - Link: integer, n
1# - let k=n and count up to find the first k, for which this is truthy:
ð - dyadic chain - i.e. f(k, n):
œc - combinations of length (n) of (implicit [1..k])
Ðḟ - filter discard those n-tuples which are truthy under:
Ɗ - last three links as a monad:
œc3 - combinations of length three of (the n-tuple)
I - incremental differences - e.g. [3,6,8]->[6-3,8-6]->[3,2]
Ƈ - filter keep those diffence-pairs which are truthy under:
E - all equal?发布于 2020-04-29 18:52:01
发布于 2020-04-29 13:06:00
∞.ΔLI.Æε3.Æε¥Ë≠}P}à输出n^{th}值k。
∞.Δ # Find the first positive integer `k`
L # for which its list in the range [1,k]
I.Æ # with combinations of the input amount of elements
ε }à # contains any combination-list which is truthy for:
3.Æ # When taking all 3-element combinations of the current list
ε }P # they are all truthy for:
¥ # When taking the forward differences of both pairs in this triplet
Ë≠ # they are NOT the same
# (after which the resulting `k` is output implicitly)https://codegolf.stackexchange.com/questions/204149
复制相似问题