Perl 6 33 30 27 바이트
{first *==$_,:k,(0,1,*+*...*>$_)}
{first *==$_,:k,(0,1,*+*...*)}
{first $_,:k,(0,1,*+*...*)}
시도 해봐
설명:
# lambda with implicit 「$_」 parameter
{
first # find the first element
$_, # where something is equal to the block's argument
:k, # return the key rather than the value
# of the Fibonacci sequence
( 0, 1, * + * ... * )
# ^--^ first two values
# ^---^ lambda used to generate the next in the series
# ^-^ generate until
# ^ Whatever
}
테스트:
#! /usr/bin/env perl6
use v6.c;
use Test;
# using the safer version that stops generating
# values bigger than the input
my &fib-index = {first $_,:k,(0,1,*+*...*>$_)}
my @tests = (
0 => 0,
2 => 3,
3 => 4,
5 => 5,
8 => 6,
13 => 7,
1836311903 => 46,
1836311904 => Nil, # this is why the safe version is used here
12200160415121876738 => 93,
19740274219868223167 => 94,
354224848179261915075 => 100,
);
plan +@tests + 1;
for @tests -> $_ ( :key($input), :value($expected) ) {
cmp-ok fib-index($input), &[eqv], $expected, .gist
}
cmp-ok fib-index((0,1,*+*...*)[1000]), &[eqv], 1000, 'works up to 1000th element of Fibonacci sequence'
1..13
ok 1 - 0 => 0
ok 2 - 2 => 3
ok 3 - 3 => 4
ok 4 - 5 => 5
ok 5 - 8 => 6
ok 6 - 13 => 7
ok 7 - 1836311903 => 46
ok 8 - 1836311904 => Nil
ok 9 - 12200160415121876738 => 93
ok 10 - 19740274219868223167 => 94
ok 11 - 354224848179261915075 => 100
ok 12 - works up to 1000th element of Fibonacci sequence