Inventory sequence: Difference between revisions

Added Perl
(Added Perl)
Line 359:
</pre>
[[File:Inventory Sequence (Nim).png|thumb|center]]
 
=={{header|Perl}}==
{{libheader|ntheory}}
{{trans|Raku}}
<syntaxhighlight lang="perl" line>
use strict;
use warnings;
use feature 'say';
 
use List::AllUtils <max firstidx>;
use GD::Graph::bars;
 
sub comma { reverse ((reverse shift) =~ s/.{3}\K/,/gr) =~ s/^,//r }
sub table { my $t = 20 * (my $c = 1 + length max @_); ( sprintf( ('%'.$c.'d')x@_, @_) ) =~ s/.{1,$t}\K/\n/gr }
 
my($i, @inventory, %i) = 0;
do {
my $count = $i{$i} // 0;
$i = $count ? $i+1 : 0;
++$i{$count};
push @inventory, $count
} until $inventory[-1] > 10_000;
 
say "Inventory sequence, first 100 elements:\n" . table @inventory[0..99]; say '';
 
for my $n (map { $_ * 1000 } 1..10) {
my $i = firstidx { $_ >= $n } @inventory;
printf "First element >= %6s is %6s in position: %s\n", comma($n), comma($inventory[$i]), comma $i;
}
 
# graph
my @data = ( [0..5000], [@inventory[0..5000]] );
my $graph = GD::Graph::bars->new(800, 600);
$graph->set(
title => 'Inventory sequence',
y_max_value => 250,
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, '>', 'Perl-inventory-sequence.png';
binmode $fh;
print $fh $gd->png();
close $fh;
</syntaxhighlight>
{{out}}
<pre>
Inventory sequence, first 100 elements:
0 1 1 0 2 2 2 0 3 2 4 1 1 0 4 4 4 1 4 0
5 5 4 1 6 2 1 0 6 7 5 1 6 3 3 1 0 7 9 5
3 6 4 4 2 0 8 9 6 4 9 4 5 2 1 3 0 9 10 7
5 10 6 6 3 1 4 2 0 10 11 8 6 11 6 9 3 2 5 3
2 0 11 11 10 8 11 7 9 4 3 6 4 5 0 12 11 10 9 13
 
First element >= 1,000 is 1,001 in position: 24,255
First element >= 2,000 is 2,009 in position: 43,301
First element >= 3,000 is 3,001 in position: 61,708
First element >= 4,000 is 4,003 in position: 81,456
First element >= 5,000 is 5,021 in position: 98,704
First element >= 6,000 is 6,009 in position: 121,342
First element >= 7,000 is 7,035 in position: 151,756
First element >= 8,000 is 8,036 in position: 168,804
First element >= 9,000 is 9,014 in position: 184,428
First element >= 10,000 is 10,007 in position: 201,788
</pre>
[[File:Perl-inventory-sequence.png|thumb|center]]
 
=={{header|Phix}}==
2,392

edits