Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
49
Task/Yellowstone-sequence/Perl/yellowstone-sequence.pl
Normal file
49
Task/Yellowstone-sequence/Perl/yellowstone-sequence.pl
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
use feature 'say';
|
||||
|
||||
use List::Util qw(first);
|
||||
use GD::Graph::bars;
|
||||
|
||||
use constant Inf => 1e5;
|
||||
|
||||
sub gcd {
|
||||
my ($u, $v) = @_;
|
||||
while ($v) {
|
||||
($u, $v) = ($v, $u % $v);
|
||||
}
|
||||
return abs($u);
|
||||
}
|
||||
|
||||
sub yellowstone {
|
||||
my($terms) = @_;
|
||||
my @s = (1, 2, 3);
|
||||
my @used = (1) x 4;
|
||||
my $min = 3;
|
||||
while (1) {
|
||||
my $index = first { not defined $used[$_] and gcd($_,$s[-2]) != 1 and gcd($_,$s[-1]) == 1 } $min .. Inf;
|
||||
$used[$index] = 1;
|
||||
$min = (first { not defined $used[$_] } 0..@used-1) || @used-1;
|
||||
push @s, $index;
|
||||
last if @s == $terms;
|
||||
}
|
||||
@s;
|
||||
}
|
||||
|
||||
say "The first 30 terms in the Yellowstone sequence:\n" . join ' ', yellowstone(30);
|
||||
|
||||
my @data = ( [1..500], [yellowstone(500)]);
|
||||
my $graph = GD::Graph::bars->new(800, 600);
|
||||
$graph->set(
|
||||
title => 'Yellowstone sequence',
|
||||
y_max_value => 1400,
|
||||
x_tick_number => 5,
|
||||
r_margin => 10,
|
||||
dclrs => [ 'blue' ],
|
||||
) or die $graph->error;
|
||||
my $gd = $graph->plot(\@data) or die $graph->error;
|
||||
|
||||
open my $fh, '>', 'yellowstone-sequence.png';
|
||||
binmode $fh;
|
||||
print $fh $gd->png();
|
||||
close $fh;
|
||||
Loading…
Add table
Add a link
Reference in a new issue