펄 6 , 25 23 바이트
{(0,1,1,1,*+*/*...*)[$_]}
{(0,1,1,*+*/*...*)[$_]}
설명:
# bare block lambda with implicit parameter 「$_」
{
(
# initial set-up
# the 「0」 is for P(0) which isn't defined
0, 1, 1, 1,
# Whatever lambda implementing the algorithm
* + * / *
# { $^a + $^b / $^c }
# keep using the lambda to generate new values until
...
# Whatever (Forever)
*
# get the value indexed by the argument
)[ $_ ]
}
결과가 64 비트 정수에 들어갈 수있는 것보다 더 큰 분모를 갖기 시작할 때까지 3까지 시작하는 입력에 대해 Rat ( Rational )을 리턴합니다. 이 시점에서 Num (부동 소수점)을 리턴하기 시작합니다 .
마지막그것이 돌아올 쥐는P(11) == 8832072277617 / 2586200337022
부동 소수점 대신에 합리적인 숫자 를 반환 하려면 다음을 대신 하여 FatRat 을 반환합니다 .
{(0.FatRat,1,1,*+*/*...*)[$_]}
테스트:
#! /usr/bin/env perl6
use v6.c;
use Test;
my &piggyback = {(0,1,1,*+*/*...*)[$_]}
# */ # stupid highlighter no Perl will ever have C/C++ comments
my @test = (
1, 1, 1, 2,
3/2, 7/3, 37/14,
529 / 222,
38242 / 11109,
66065507 / 19809356,
8832072277617 / 2586200337022,
);
plan +@test;
for 1..* Z @test -> ($input,$expected) {
cmp-ok piggyback($input), &[==], $expected, $expected.perl;
}
P(0)=1...