2013-01-23 13:53:53 -08:00
|
|
|
package DDG::Goodie::Fibonacci;
|
2014-08-20 11:45:33 -07:00
|
|
|
# ABSTRACT: n-th Fibonacci number
|
2013-01-23 13:53:53 -08:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
use DDG::Goodie;
|
2013-01-24 01:20:34 -08:00
|
|
|
use Lingua::EN::Numbers::Ordinate qw(ordsuf);
|
2016-06-09 13:01:15 -07:00
|
|
|
use Math::NumSeq::Fibonacci;
|
2013-01-23 13:53:53 -08:00
|
|
|
|
|
|
|
triggers any => 'fib', 'fibonacci';
|
2014-10-14 09:21:34 -07:00
|
|
|
|
2013-01-23 13:53:53 -08:00
|
|
|
zci answer_type => 'fibonacci';
|
2014-10-14 09:21:34 -07:00
|
|
|
zci is_cached => 1;
|
2013-01-23 13:53:53 -08:00
|
|
|
|
2016-06-09 13:01:15 -07:00
|
|
|
handle remainder_lc => sub {
|
2013-01-23 13:53:53 -08:00
|
|
|
s/^\s+//;
|
|
|
|
s/\s+$//;
|
2016-06-09 13:01:15 -07:00
|
|
|
my $limit = 25000; # larger numbers degrade performance
|
|
|
|
|
|
|
|
return unless /^(?:what(?:'s| is) the )?(?<which>\d+)(?:th|rd|st|nd)?(?: number)?(?: in the (?:series|sequence))?\??$/ && $1 <= $limit;
|
2014-10-14 09:21:34 -07:00
|
|
|
my $n = $+{'which'};
|
2016-06-09 13:01:15 -07:00
|
|
|
my $fib_seq = Math::NumSeq::Fibonacci->new;
|
|
|
|
my $val = $fib_seq->ith($n);
|
2016-05-15 09:54:04 -07:00
|
|
|
|
2014-10-14 09:21:34 -07:00
|
|
|
my $suf = ordsuf($n);
|
2016-06-09 13:01:15 -07:00
|
|
|
my $text_answer ="The $n$suf fibonacci number is $val (assuming f(0) = 0).";
|
2016-05-15 09:54:04 -07:00
|
|
|
return $text_answer, structured_answer => {
|
|
|
|
data => {
|
2016-06-09 13:01:15 -07:00
|
|
|
title => $val,
|
2016-05-15 09:54:04 -07:00
|
|
|
subtitle => "$n$suf Fibonacci number"
|
|
|
|
},
|
|
|
|
templates => {
|
|
|
|
group => 'text'
|
|
|
|
}
|
|
|
|
};
|
2013-01-23 13:53:53 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
1;
|