在斐波那契数字内


20

挑战

给定一个整数输入,返回包含其内部输入的第一个斐波那契数以及该斐波那契数的索引(索引从0或1开始-取决于您,但请在您的答案中提及)。例如,如果输入为12,则程序将返回,26: 121393因为在数字(12 1393)中找到12,并且该数字位于斐波那契数的索引26处。

例子

给定输入:

45

您的程序应输出:

33: 3524578

输入:

72

输出:

54: 86267571272

输入:

0

输出:

0: 0

输入:

144

输出:

12: 144

计分

这是,因此每种语言中最短的答案将获胜。


我们可以选择使用1索引吗?
Xcoder先生17年

1
不是重复的,但是非常接近这一挑战。
林恩

1
关于斐波那契数列是否正常(关于这个问题的假设是正常的)进行数学研究
AdmBorkBork

1
我们是否必须使用冒号作为分隔符?我们可以输出数组/列表吗?
毛茸茸的

Answers:


8

果冻,10字节

0ÆḞ©w¥1#;®

在线尝试!

怎么运行的

0ÆḞ©w¥1#;®  Main link. Argument: n

0           Set the return value to 0.
       #    Call the second link to the left with arguments k = 0, 1, 2, ... until
      1     one match has been found.
     ¥        Combine the two links to the left into a dyadich chain.
 ÆḞ             Compute the k-th Fibonacci number...
   ©              and copy it to the register.
    w           Yield 1 if n occurs inside the Fibonacci number, 0 otherwise.
         ®  Yield the value stored in the register.
        ;   Concatenate the index and the Fibonacci number.

您使用了与我相同的技巧。:)
暴民埃里克(Erik the Outgolfer)'17年

@EriktheOutgolfer和你一样吗?
丹尼斯

没有发布,但是通常我都没有使用D
Outgolfer的Erik



3

批处理,104字节

@set/an=x=0,y=1
:l
@call set t=%%x:%1=%%
@if "%t%"=="%x%" set/an+=1,x+=y,y=x-y&goto l
@echo %n%: %x%

n=0..45由于Batch整数运算的范围有限,因此适用。说明:Batch没有内置的匹配测试,但是它确实具有可以将文字字符串替换为其他文字字符串的运算符,因此例如if "%s:l=%"=="%s%",如果%s%不为空但不包含,则为true l。使用的call技巧是将%1(输入)替换为替换运算符,但是call不适用于控制流语句,因此需要进行中间临时赋值。



2

Javascript ES6,68个字符

n=>eval('for(q=x=0,y=1;!`${x}`.match(n);++q)[x,y]=[y,x+y];q+": "+x')

测试:

f=n=>eval('for(q=x=0,y=1;!`${x}`.match(n);++q)[x,y]=[y,x+y];q+": "+x')
console.log([45,72,0,144].map(f).join`
`)


2

Python 3,76个字节

f=lambda n,l=[1,0]:str(n)in str(l[1])and(len(l)-2,l[1])or f(n,[l[0]+l[1]]+l)


1

Dyalog APL,39个字节

{⍺←0⋄∨/(⍕⍵)⍷⍕x←1∧+∘÷/0,⍺/1:⍺,x⋄(1+⍺)∇⍵}

使用尾递归。不要尝试72,它会破坏您的计算机,因为它会在每次调用时重新计算斐波那契。

在线尝试!


1

Mathematica,119个字节

1索引

(T=ToString;If[(h=#)==0,"0:0",a=#&@@Select[k=T/@(Array[Fibonacci,9#]),StringContainsQ[#,T@h]&];Min@Position[k,a]":"a])&


在线尝试!


1

实际上,13个字节

╗1⌠F$╜@c⌡╓i;F

在线尝试!

说明:

╗1⌠F$╜@c⌡╓i;F
╗              save input in register 0
 1⌠F$╜@c⌡╓     smallest non-negative integer n where the following function returns truthy:
   F$            nth Fibonacci number, stringified
     ╜@c         count occurrences of input
          i;F  flatten the list, duplicate the index, and push the Fibonacci number at that index

1

R,65个字节

f=function(x,n=1,a=1,b=0)`if`(grepl(x,b),c(b,n-1),f(x,n+1,a+b,a))

用于生成Fibnums的标准递归,但不是基于终止,而是nb与regex匹配时终止x。这实际上出奇地好。我以为将正则表达式与数字一起使用将需要很多麻烦将它们转换为字符串,但这似乎没有必要:)

这还具有通过1步过冲递归,通过检查b,而不是a,然后从其减去1n。这是为了确保f(0)正常工作。

1001由于maxint,当input超过时,这对于大多数值都将失败。如果我们替换ab使用bigints,则此方法可用于较高的输入(当前测试为x = 11451

f=function(x,n=1,a=gmp::as.bigz(1),b=gmp::as.bigz(0))`if`(grepl(x,b),c(b,n-1),f(x,n+1,a+b,a))

1

的JavaScript ES6,79 78 75个字节

-1字节by 母鸡

尼尔 -3字节

i=>eval('d=a=b=1;while(!~(a+"").indexOf(i)){c=b;b=a+b;a=c;‌​d++};d+": "+a')

1
您可以使用eval()而不是{ return}保存一个字节,并且可以删除,t=因为您没有使用递归:i=>eval('d=a=b=1;while(!~(a+"").indexOf(i+""){c=b;b=a+b;a=c;d++};d+": "+a')
Stephen

1
String.prototype.indexOf自动将其参数转换为字符串,而无需显式执行。另外,您似乎复制了@StepHen的错字(您的(s多于)s)。
尼尔

@Neil惊呼我的坏人
Stephen



1

PHP,80字节

<?php for($a=1,$b=$n=0;strpos($a=-$a+$b=$a+$b,"$argv[1]")<-1;$n++);echo"$n: $a";

该脚本非常简单,只需将序列的当前和下一个术语存储在整个$ a和$ b中。为了允许第0个项为0,分别为$ a和$ b分配第-1个项(1)和第0个项(0)的值。

两个值都在一个表达式中重新计算,一个表达式中有两个赋值。有效:

$b = $a + $b; // The next term is the sum of the two previous terms
$a = $b - $a; // The current term is now recalculated from the next and the previous

如果输入值与项的开头匹配,则strpos()函数将返回0(这是false并给出错误的负数),但是在PHP的Wonderphul World中,虽然false == 0是true和false < 0false,但它false < -1是true!因此,与相比,使用此比较可节省五个字节!==false


1

Japt17 14字节

@JustinMariner节省了3个字节

_ŬøU}a@[XMgX]

在线尝试!

说明

_ŬøU}a@[XMgX]      Implicit: U = input integer
      a@            For each integer X in [0, 1, 2, ...]:
        [XMgX]        take [X, Fibonacci(X)].
_    }a             Return the first pair where
 Å                    all but the first item
  ¬                   joined on the empty string (simply returns Fibonacci(X) as a string)
   øU                 contains U.
                    Implicit: output result of last expression

14个字节:_ŬøU}a@[XMgX]。使用s1 q 获得最后一个项目,它允许丢弃<space>s
贾斯汀水手

@JustinMariner那是...那真是天才:-)
ETHproductions's

0

PHP163141字节

<?php $x=fgets(STDIN);$b=[0,1];if($x<1)$b=[0];for(;($c=count($b)-1)&&strpos($b[$c],$x)===false;){$b[]=$b[$c]+$b[$c-1];}die($c.': '.$b[$c]);?>

在线尝试!

使用$b[0] = 0;$b[1] = 1;作为Fib序列的开始



0

PHP,93字节

for($a[0]=$a[1]++;!strpos(" $a[$i]","$argv[1]");$a[$i+2]=$a[$i+1]+$a[$i++]);echo"$i: $a[$i]";

简单遍历斐波那契数列。检查我们的输入号码是在strpos(" $a[$i]","$argv[1]");中完成的。多余的空间是因为strpos如果在字符串的开头找到“ needle”,则将返回false-y。如果找到输入,我们结束并回显所需的字符串。

在线尝试!


0

Common Lisp,105个字节

(lambda(x)(do((a 0 b)(b 1(+ a b))(i 0(1+ i)))((search(#1=format()"~a"x)(#1#()"~a"a))(#1#()"~a: ~a"i a))))

在线尝试!

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.