Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
36
Task/Stern-Brocot-sequence/Perl/stern-brocot-sequence.pl
Normal file
36
Task/Stern-Brocot-sequence/Perl/stern-brocot-sequence.pl
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
|
||||
sub stern_brocot {
|
||||
my @list = (1, 1);
|
||||
sub {
|
||||
push @list, $list[0] + $list[1], $list[1];
|
||||
shift @list;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
my $generator = stern_brocot;
|
||||
print join ' ', map &$generator, 1 .. 15;
|
||||
print "\n";
|
||||
}
|
||||
|
||||
for (1 .. 10, 100) {
|
||||
my $index = 1;
|
||||
my $generator = stern_brocot;
|
||||
$index++ until $generator->() == $_;
|
||||
print "first occurrence of $_ is at index $index\n";
|
||||
}
|
||||
|
||||
{
|
||||
sub gcd {
|
||||
my ($u, $v) = @_;
|
||||
$v ? gcd($v, $u % $v) : abs($u);
|
||||
}
|
||||
my $generator = stern_brocot;
|
||||
my ($a, $b) = ($generator->(), $generator->());
|
||||
for (1 .. 1000) {
|
||||
die "unexpected GCD for $a and $b" unless gcd($a, $b) == 1;
|
||||
($a, $b) = ($b, $generator->());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue