在斐波那契数列Mod K中找到缺失的数字


20

受此Math.SE问题的启发。

背景

斐波纳契数列(称为F)是序列,开始0, 1使得每个号码(F(n))(前两个后)是两个前(总和F(n) = F(n-1) + F(n-2))。

斐波那契数列mod K(称为M)是斐波那契数列mod K()的序列M(n) = F(n) % K

可以证明,斐波那契数列对所有K都是循环的,因为每个值都是由前一对确定的,并且只有K 2个可能的非负整数对都小于K。因为斐波那契数列K在第一个重复的术语对之后是循环的,在第一个重复的术语对之前从未出现在Fibonacci序列mod K中的数字将永远不会出现。

对于K = 4

0 1 1 2 3 1 0 1 ...

对于K = 8

0 1 1 2 3 5 0 5 5 2 7 1 0 1 ...

请注意,对于K = 8,4和6不会出现在重复出现之前0 1,因此4和6将永远不会出现在斐波那契数列mod 8中。

挑战

给定严格大于0的整数K,输出所有出现在Fibonacci序列mod K中的小于K的非负整数。

规则

  • 禁止默认漏洞

  • 默认的I / O

  • 程序或功能是可以接受的

  • 您可以假设K将适合您的本机整数类型(在reason内)。

  • 如果斐波那契序列模K中没有出现小于K的非负数,则您的程序/函数应以任何合理的方式输出所有此类负数。

  • 如果在Fibonacci Sequence mod K中没有出现不小于K的非负整数,则您的程序/函数可能会通过返回空列表,不打印任何内容,产生错误等来指示这一点。

  • 顺序无所谓。

  • 这是,因此每种语言的最短答案都会获胜。

测试用例

在线生成测试用例!

非空测试用例

  8 [4, 6]
 11 [4, 6, 7, 9]
 12 [6]
 13 [4, 6, 7, 9]
 16 [4, 6, 10, 12, 14]
 17 [6, 7, 10, 11]
 18 [4, 6, 7, 9, 11, 12, 14]
 19 [4, 6, 7, 9, 10, 12, 14]
 21 [4, 6, 7, 9, 10, 11, 12, 14, 15, 16, 17, 19]
 22 [4, 6, 7, 9, 15, 17, 18, 20]
 23 [4, 7, 16, 19]
 24 [4, 6, 9, 11, 12, 14, 15, 18, 19, 20, 22]
 26 [4, 6, 7, 9, 17, 19, 20, 22]
 28 [10, 12, 14, 16, 18, 19, 23]
 29 [4, 6, 7, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 25, 27]
 31 [4, 6, 9, 12, 14, 15, 17, 18, 19, 22, 25, 29]
 32 [4, 6, 10, 12, 14, 18, 20, 22, 26, 28, 30]
 33 [4, 6, 7, 9, 15, 17, 18, 20, 24, 26, 27, 28, 29, 31]
 34 [4, 6, 7, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 25, 27, 28, 30]
 36 [4, 6, 7, 9, 10, 11, 12, 14, 16, 18, 20, 22, 23, 24, 25, 26, 27, 29, 30, 31, 32]
 37 [9, 10, 14, 17, 20, 23, 27, 28]
 38 [4, 6, 7, 9, 10, 11, 12, 14, 15, 16, 18, 19, 20, 22, 23, 24, 25, 26, 27, 28, 29, 31, 32, 33, 36]
 39 [4, 6, 7, 9, 15, 17, 19, 20, 22, 24, 30, 32, 33, 35]
...
200 [4, 6, 12, 14, 20, 22, 28, 30, 36, 38, 44, 46, 52, 54, 60, 62, 68, 70, 76, 78, 84, 86, 92, 94, 100, 102, 108, 110, 116, 118, 124, 126, 132, 134, 140, 142, 148, 150, 156, 158, 164, 166, 172, 174, 180, 182, 188, 190, 196, 198]
...
300 [6, 18, 30, 42, 54, 66, 78, 90, 102, 114, 126, 138, 150, 162, 174, 186, 198, 210, 222, 234, 246, 258, 270, 282, 294]
...
400 [4, 6, 10, 12, 14, 20, 22, 26, 28, 30, 36, 38, 42, 44, 46, 52, 54, 58, 60, 62, 68, 70, 74, 76, 78, 84, 86, 90, 92, 94, 100, 102, 106, 108, 110, 116, 118, 122, 124, 126, 132, 134, 138, 140, 142, 148, 150, 154, 156, 158, 164, 166, 170, 172, 174, 180, 182, 186, 188, 190, 196, 198, 202, 204, 206, 212, 214, 218, 220, 222, 228, 230, 234, 236, 238, 244, 246, 250, 252, 254, 260, 262, 266, 268, 270, 276, 278, 282, 284, 286, 292, 294, 298, 300, 302, 308, 310, 314, 316, 318, 324, 326, 330, 332, 334, 340, 342, 346, 348, 350, 356, 358, 362, 364, 366, 372, 374, 378, 380, 382, 388, 390, 394, 396, 398]
...

空测试用例(无输出,错误,空列表等是可接受的输出)

1, 2, 3, 4, 5, 6, 7, 9, 10, 14, 15, 20, 25, 27, 30, 35 ... 100 ...

有关:

计数斐波纳契轨道

寻找皮萨诺时期


沙盒(已删除)。
pizzapant184 '18

Answers:



6

Haskell,70个字节

多亏了水果,节省了一些字节

Laikoni节省了8个字节

a=1:scanl(+)1a
f x=[u|u<-[2..x-1],and[mod b x/=u|(_,b)<-zip[1..x^2]a]]

在线尝试!


@EsolangingFruit啊,谢谢!我本人也刚刚得出类似的结论。
小麦巫师

read$show而不是fromInteger在这种情况下起作用,并节省了两个字节。
Laikoni '18

使用zip[1..x^2]用于截断节省一些字节:网上试试吧!
Laikoni '18

@Laikoni花了一段时间,但我进行了更改。谢谢,这是个好主意。
Wheat Wizard'2

5

Perl 6的 43个42 39  32字节

{^$_ (-)(1,1,(*+*)%$_...->\a,\b{!a&&b==1})}

测试一下

{^$_∖(1,1,(*+*)%$_...->\a,\b{!a&&b==1})}

测试一下

{^$_∖(1,1,(*+*)%$_...{!$^a&&$^b==1})}

测试一下

{^$_∖(1,1,(*+*)%$_...!*&*==1)}

测试一下

展开:

{  # bare block lambda with implicit parameter 「$_」

  ^$_               # Range upto and excluding the input

                   # set minus (U+2216)

  (                 # generate the Fibonacci sequence mod k

    1, 1,           # seed the sequece (can't be 0,1)

    ( * + * ) % $_  # add two values and modulus the input (lambda)

    ...             # keep doing that until

                    # it matches 0,1
    !*              #   negate the first param (1 when 0)
    &               #   and Junction
    *               #   second param
    == 1            #   both match 1

  )
}

3

> <>,48个字节

01\
?!\:&+{:}%:1$0p&$:
v0\~:1=?
>?!;1-::0g?!nao:

在线尝试!

通过-v标志获取输入。

打印很多多余的换行符,但可以完成工作。基本上,这是使用第一行来存储序列中到目前为止出现的一组数字。

怎么运行的:

01\    Input is already on the stack
...... Initialises the sequence with 1 and 0
...... Goes to the second line
......

......
..\:&+{:}% Gets the next number in the modded Fibonacci sequence while preserving the previous number
......
......

......
..........:1$0p&$: Puts a 1 at that cell number on the first line
.......
.......

......             If the number is a 0 go to the third line
?!\..............: Check if the next number is a 1, meaning we've reached the end of the sequence
v0\~:1=?           Go to the fourth line if so
>.....             Re-add the 0 and go back to the second line if not

......           While input:
......             Get the cell from the first line
......             If not 0: print the number
>?!;1-::0g?!nao:   Finally, print a newline and decrement the input


3

MATL19 18字节

0lbU:"yy+]vG\G:qX~

在线尝试!

-1字节感谢Guiseppe。

  bU:"   ]         % Do K^2 (>6K) times.
0l    yy+          %  Fibbonaci
                X~ % Set exclusive difference between
          vG\      %  the fibonacci numbers mod K
             G:q   %  and 0...K-1

18个字节 ; 重新排列将恢复对X~!的使用!
朱塞佩

@Giuseppe谢谢!仍然很长....
Sanchises


2

机壳13 12 10字节

感谢@Zgarb提供-2个字节!

-U2m%⁰İfŀ⁰

如果出现所有整数,则打印一个空列表, 则在线尝试!

说明

-U2m%⁰İfŀ⁰  -- named argument ⁰, example with: 8
-           -- difference of
        ŀ⁰  -- | lowered range: [0,1,2,3,4,5,6,7]
            -- and
      İf    -- | Fibonacci sequence: [1,1,2,3,5,8,13,21,34,55,89,144,233,377…
   m%⁰      -- | map (modulo ⁰): [1,1,2,3,5,0,5,5,2,7,1,0,1,1…
 U2         -- | keep longest prefix until 2 adjacent elements repeats: [1,1,2,3,5,0,5,5,2,7,1,0,1]
            -- : [4,6]

您可以使用U2来获得最长的前缀,其中没有相邻的对重复。
Zgarb


2

R,92 86字节

感谢@Giuseppe节省了6个字节!

function(k,n=!!0:2){while(any((z=tail(n,2))-n[1:2]))n=c(n,sum(z)%%k);setdiff(1:k-1,n)}

在线尝试!

非常简单的实现(以前的版本,但概念相同):

function(k,
         K=1:k-1,      #Uses default arguments to preset variables for legibility 
         n=c(0,1,1)){  #(wouldn't change byte-count to put them in the body of the function)
    while(any((z=tail(n,2))!=n[1:2])) #Do as long as first 2 elements are not identical to last 2 elements
        n=c(n,sum(z)%%k) #Built the fibonacci mod k sequence
    K[!K%in%n] #Outputs integers < k if not in sequence.
}


@Giuseppe啊setdiff,好主意!
plannapus

70字节移植了1:k^2其他人使用的方法
Giuseppe

2

Python 3中,173个 152 143 131字节

f=lambda n,m,a=0,b=1:a%m if n<=0else f(n-1,m,b,a+b)
p=lambda n,i=2,y={0}:y^{*range(n)}if f(i,n)==1>f(i-1,n)else p(n,i+1,y|{f(i,n)})

特别感谢@ovs。

在线试用

它是如何工作的?

第一个函数接受两个参数m和n,并返回第n个斐波那契数mod m。第二个函数遍历斐波那契数模k,并检查是否重复0和1。它将数字存储在列表中,并将其与包含数字1-n的列表进行比较。重复的数字将被删除,剩余的数字将被返回。


它是标头的一部分,不是必须包含在代码中。
Manish Kundu

好吧 @ovs感谢您告诉我,我没有意识到。
Manish Kundu

1
通过创建带有大括号而不是set()和链接比较的集来获得131个字节
ovs


2

Ruby,47个字节

->n{a=b=1;[*1...n]-(1..n*n).map{a,b=b,a+b;a%n}}

在线尝试!

尽管它使用了某些相同的逻辑,但这并不是基于 GB的答案

说明:

->n{
  a=b=1;   # start sequence with 1,1
  [*1...n] # all the numbers from 1 to n-1 as an array
           # 0 is excluded as it should never be in the final answer 
  -  # set operation; get all items in the first set and not in the second
  (1..n*n).map{ # n squared times
    a,b=b,a+b;  # assign next fibonacci numbers 
    a%n         # return a fibonacci number mod n
  }    # Map to an array
}

2

通用Lisp,106个字节

(lambda(k)(do((a 1 b)c(b 1(mod(+ a b)k)))((=(1- b)0 a)(dotimes(i k)(or(member i c)(print i))))(push a c)))

在线尝试!



1

Elixir148144字节

 fn x->Enum.to_list(1..x-1)--List.flatten Enum.take_while Stream.chunk(Stream.unfold({1,1},fn{p,n}->{rem(p,x),{n,p+n}}end),2),&Enum.sum(&1)!=1end

在线尝试!

这不是一个特别有竞争力的答案,但是打高尔夫球真的很有趣!Elixir是一种非常易读的语言,但是下面解释了中间的混乱字符。


该解释分为两部分,即mod-fibonacci及其操作

Mod-fib:

Stream.unfold({1,1},fn{p,n}->{rem(p,x),{n,p+n}}end)

这将返回无限的fibonacci mod流x。它以累加器开始{1,1},并无限地应用以下操作:给定累加器{p,n},输出p mod x到流。然后,将累加器设置为{n,p+n}

其余的部分:

fn x->                              Define a fxn f(x) that returns
  Enum.to_list(1..x-1)--            The numbers from 1..x-1 that are not in
  List.flatten                      The flattened list constructed by
    Enum.take_while                 Taking from mod-fib until
      Stream.chunk(                 A 2-size chunk
        Stream.unfold({1,1},fn{p,n}->{rem(p,x),{n,p+n}}end) (of mod fib)
        ,2)
      ,&Enum.sum(&1)!=1             sums to 1, representing [0,1] or [1,0]
end



1

JavaScript(ES6),84个字节

f=(n,a=0,b=1,q=[...Array(n).keys()])=>a*b+a-1?f(n,b,(a+b)%n,q,q[b]=0):q.filter(x=>x)

1

Python 3,76个字节

def t(n,r=[1]):
 while n*n>len(r):r+=[sum(r[-2:])%n]
 return{*range(n)}-{*r}

这只是简单地查看了斐波那契数字的最长周期(n ^ 2),并创建了该时间出现的所有数字的列表。为了简化逻辑,数字以n为模存储。

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.