顺序可整


24

有时要入睡,我会尽力而为,而跳过那些不是平方的数字。当我连续跳过几个数字时,我会有些激动-例如,48,49,50并非全部都是无平方的(48被2 ^ 2整除,49被7 ^ 2整除,而50被5 ^ 2整除)。

这使我想知道相邻数被任意除数序列整除的最早示例。

输入项

输入是a = [a_0, a_1, ...]包含至少1个元素的严格正整数的有序列表。

输出量

输出是最小的正整数,n具有可a_0划分na_1划分n+1,更一般地a_k划分的属性n+k。如果不n存在,则功能/程序的行为未定义。

测试用例

[15] -> 15
[3,4,5] -> 3
[5,4,3] -> 55
[2,3,5,7] -> 158
[4,9,25,49] -> 29348
[11,7,5,3,2] -> 1518

计分

这是;最短的结果(每种语言)赢得吹牛的权利。通常的漏洞被排除在外。


Answers:



6

外壳,7个字节

VδΛ¦⁰ṫN

在线尝试!

说明

VδΛ¦⁰ṫN  Input is a list x.
      N  The list [1,2,3...
     ṫ   Tails: [[1,2,3...],[2,3,4...],[3,4,5...]...
V        Index of first tail y satisfying this:
  Λ       Every element
    ⁰     of x
   ¦      divides
 δ        the corresponding element of y.

5

MATL,11个字节

`Gf@q+G\a}@

在线尝试!

`           % Do ....
 Gf         %   Convert input to [1,2,...,]
   @q+      %   Add current iteration index minus one, to get [n, n+1, ....]
      G\    %   Elementwise mod([n,n+1,...],[a_0,a_1,...])
        a   % ...while any of the modular remainders is nonzero.
         }  % Finally:
          @ %   Output the iteration index.

尚未完全针对速度进行优化...使用MATL进行最大的测试用例需要一整分钟,而在MATLAB上则需要大约0.03s。MATL的开销很小。


啊,我有n:q`QtG\a]1)12个字节,但n:显然与f这里相同。我总是忘记了这一点,因此您可以将其添加为11替代。
朱塞佩

1
@Giuseppe太差了,fq`QtG\a}@返回了输入的多余副本。
Sanchises

5

JavaScript,42 40字节

如果没有解决方案(或解决方案太大),将引发递归错误。

a=>(g=y=>a.some(x=>y++%x)?g(++n):n)(n=1)

使用Rick Hitchcock的指针保存了2个字节


试试吧

输入逗号分隔的数字列表。

o.innerText=(f=
a=>(g=y=>a.some(x=>y++%x)?g(++n):n)(n=1)
)(i.value=[5,4,3]);oninput=_=>o.innerText=f(i.value.split`,`.map(eval))
<input id=i><pre id=o>


不错的方法,但是由于使用超过了递归限制而失败了[4,9,25,49]
Chas Brown

1
@ChasBrown,出于代码高尔夫的目的,我们可以假设内存是无限的。
粗野的

我认为这将(a,y=n=0)=>a.some(x=>y++%x)?f(a,++n):n
Rick Hitchcock

噢,很好,@ RickHitchcock-谢谢。不过,请不要忘记f=
粗野的

嗯当然了 40字节。
里克·希区柯克


3

05AB1E,9个字节

Xµā<N+sÖP

在线尝试!

说明

Xµ          # loop until counter equals 1
  ā         # push range [1 ... len(input)]
   <        # decrement
    N+      # add current iteration index N (starts at 1)
      sÖ    # elementwise evenly divisible by
        P   # product
            # if true, increase counter
            # output N

哎呀,拆除了我的17个字节的答案。
魔术章鱼缸



3

Pyth,11个字节

f!s.e%+kTbQ 

在线尝试!


f!s.e%+kTbQ         Full program - inputs list from stdin and outputs to stdout
f                   First number T such that
   .e     Q         The enumerated mapping over the Input Q
      +kT           by the function (elem_value+T)
     %   b          mod (elem_index)
 !s                 has a false sum, i.e. has all elements 0

您最后需要2吗?我确定还有更多要保存的地方,但我不知道Pyth。
粗野的

@Shaggy和@Giuseppe,你们俩都是正确的,删除​​结尾即可2解决问题
Dave

2

J,23个字节

[:I.0=]+/@:|"1#]\[:i.*/

在线尝试!


真好 什么数学事实允许您仅检查输入乘积?我们怎么知道没有其他解决方案?另外,我们怎么知道I.只会返回1个结果?可能有多个吗?
乔纳

1
@Jonah-我不知道它是否总是有效;我所做的所有测试都在这些限制范围内。
Galen Ivanov

2

R,51字节

function(l){while(any((F+1:sum(l|1))%%l))F=F+1
F+1}

在线尝试!

使用有关anyk隐式转换为的引发警告logical,其中k返回值为。



我考虑过@plannapus,但不幸的是l=c(15)seq(l)==1:l对于这种情况,它失败了。seq真讨厌
朱塞佩

arf确实然后强制seq_along太长了。
plannapus

因此,仅供参考,而sum不是使用any摆脱这些警告的方法。
plannapus


2

APL(Dyalog Unicode)24 23 22字节

{∨/×⍺|⍵+⍳⍴⍺:⍺∇⍵+1⋄⍵}∘1

在线尝试!

从技术上讲,这是默认功能。我必须这样做,因为允许的唯一输入是整数列表。用途⎕IO←0(0索引)

值得注意的是,该功能如果n不存在就会超时。

感谢@ngn和@ H.PWiz每个1字节。

怎么样?

{∨/×⍺|⍵+⍳≢⍺:⍺∇⍵+1⋄⍵}∘1  Main function. ⍺=input; ⍵=1.
{                   }∘1  Using 1 as right argument and input as left argument:
           :             If
        ⍳≢⍺              The range [0..length(⍺)]
      ⍵+                 +⍵ (this generates the vector ⍵+0, ⍵+1,..., ⍵+length(⍺))
    ⍺|                   Modulo 
   ×                     Signum; returns 1 for positive integers, ¯1 for negative and 0 for 0.
 ∨/                      Logical OR reduction. Yields falsy iff the elements of the previous vector are all falsy.
            ⍺∇⍵+1        Call the function recursively with ⍵+1.
                 ⋄⍵      Else return ⍵.


1

Japt,10个字节

undefined如果不存在任何解决方案,并且最终没有使您的浏览器崩溃,它将最终输出。

@e_X°vZÃ}a

试试吧


说明

               :Implicit input of array U
@       }a     :Loop and output the first integer X that returns true.
 e_    Ã       :For every element Z in U
   X°          :X, postfix increcemnted
     vZ        :Is it divisible by Z?



1

标准ML(MLton),96字节

open List;fun$n% =if all hd(tabulate(length%,fn i=>[1>(n+i)mod nth(%,i)]))then n else$(n+1)%;$1;

在线尝试!

取消高尔夫:

open List
fun f n l = 
    if all (fn x=>x)
           (tabulate ( length l
                     , fn i => (n+i) mod nth(l,i) = 0))
    then n 
    else f (n+1) l
val g = f 1

在线尝试!从开始n=1,函数f递增n直到all满足-condition为止,在这种情况下n返回。

tabulate(m,g)用一些整数m和函数g构建列表[g 0, g 1, ..., g m]。在我们的条件tabulate被称为与输入列表的长度l和一个函数,它检查是否i的第i个元素l划分n+i。这会产生一个布尔值列表,因此all使用identity函数fn x=>x检查所有元素是否为真。

我发现了一个很好的高尔夫技巧,可以在这种情况下将标识函数缩短四个字节:代替lambda (fn x=>x),使用了内置函数hd,该函数返回列表的第一个元素,并且将生成的布尔值tabulate包装在[]中创建单例列表。


1

PowerShell65 62字节

for(){$o=1;$i=++$j;$args[0]|%{$o*=!($i++%$_)};if($o){$j;exit}}

在线尝试!

PowerShell没有等价的anyor some或类似物,因此我们需要一种略有不同的方法。

这将输入$args[0]作为数组,然后进入无限for循环。我们将每个迭代设置$o1(稍后说明),并设置$i++$j。递增$j会保留关于拟议解决方案的第一个数字的标签,而$i将在其余拟议解决方案上递增。

然后,我们将输入的每个元素发送$args[0]ForEach-Object循环中。在内部循环中,我们将布尔$o结果乘以运算结果。这样一来,如果计算失败,$o则将变为0。计算为!($i++%$_),或取模运算的布尔值-非。由于在PowerShell中任何非零值都是真实的,因此这会将所有余数转换为伪值,从而$o变为0

在内部循环之外,它if $o为非零值,我们发现一个递增的解决方案有效,因此我们输出$jexit


1

tinylisp,108字节

(load library
(d ?(q((L N)(i L(i(mod N(h L))0(?(t L)(inc N)))1
(d S(q((L N)(i(? L N)N(S L(inc N
(q((L)(S L 1

最后一行是一个未命名的lambda函数,该函数接受一个列表并返回一个整数。在线尝试!

不打高尔夫球

(load library)

(comment Function to check a candidate n)
(def sequentially-divisible?
 (lambda (divisors start-num)
  (if divisors
   (if (divides? (head divisors) start-num)
    (sequentially-divisible? (tail divisors) (inc start-num))
    0)
   1)))

(comment Function to check successive candidates for n until one works)
(def search
 (lambda (divisors start-num)
  (if (sequentially-divisible? divisors start-num)
   start-num
   (search divisors (inc start-num)))))

(comment Solution function: search for candidates for n starting from 1)
(def f
 (lambda (divisors)
  (search divisors 1)))


1

Python 2,78个字节

def f(a,c=0):
 while [j for i,j in enumerate(a) if(c+i)%j<1]!=a:c+=1
 return c

编辑:-26感谢@Chas Brown


真好!我改变了您的循环退出条件,可以将您的想法改进为78个字节
Chas Brown

@ChasBrown谢谢,我没想到那样做。变了!
sonrad10


0

APL NARS,140字节,70个字符

r←f w;i;k
i←r←1⊃,w⋄k←¯1+⍴w⋄→0×⍳k=0
A:→0×⍳0=+/(1↓w)∣(k⍴r)+⍳k⋄r+←i⋄→A

测试

  f 15
15
  f 3 4 5
3
  f 5 4 3
55
  f 2 3 5 7
158
  f 4 9 25 49
29348
  f 11 7 5 3 2 
1518

0

Java 8,82 75字节

a->{for(int r=1,i,f;;r++){i=f=0;for(int b:a)f+=(r+i++)%b;if(f<1)return r;}}

说明:

在线尝试。

a->{                 // Method with integer-array parameter and integer return-type
  for(int r=1,       //  Return-integer, starting at 1
          i,         //  Index-integer
          f;         //  Flag-integer
      ;r++){         //  Loop indefinitely, increasing `r` by 1 after every iteration
    i=f=0;           //   Reset both `i` and `f` to 0
    for(int b:a)     //   Inner loop over the input-array
      f+=(r+i++)%b;  //    Increase the flag-integer by `r+i` modulo the current item
    if(f<1)          //   If the flag-integer is still 0 at the end of the inner loop
      return r;}}    //    Return `r` as result

0

Ruby47 46 43 42字节

->a{(1..).find{|i|a.all?{|v|i%v<1&&i+=1}}}

在线尝试!

注意:该(1..)语法仅在ruby 2.6中受支持,目前TIO仅支持2.5,因此链接指向较旧的版本(43字节)。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.