Inventory sequence: Difference between revisions

→‎{{header|RPL}}: improved code
(Created Nim solution.)
(→‎{{header|RPL}}: improved code)
 
(9 intermediate revisions by 6 users not shown)
Line 27:
 
 
 
=={{header|ALGOL 68}}==
Calculates the sequence elements without storing them.
<syntaxhighlight lang="algol68">
BEGIN # find elements of the inventory sequence #
 
INT next to show := 1 000; # next value to show first element > #
INT max to show = 10 000; # last value to show first element > #
 
INT max number = max to show + 1 000; # max. element value to consider #
[ 0 : max number ]INT occurs; # number of times each number occurs #
FOR i FROM LWB occurs TO UPB occurs DO occurs[ i ] := 0 OD;
INT seq pos := 0; # current end of the sequence #
WHILE next to show <= max to show DO
FOR n FROM 0 WHILE next to show <= max to show
AND BEGIN
INT element := occurs[ n ];
seq pos +:= 1;
IF seq pos <= 100 THEN
print( ( " ", whole( element, -4 ) ) );
IF seq pos MOD 10 = 0 THEN print( ( newline ) ) FI
ELIF element > next to show THEN
print( ( "Element ", whole( seq pos, -8 )
, " (", whole( element, -8 )
, ") is first > ", whole( next to show, -6 )
, newline
)
);
next to show +:= 1 000
FI;
IF element < max number THEN
occurs[ element ] +:= 1
FI;
element /= 0
END
DO SKIP OD
OD
 
END
</syntaxhighlight>
{{out}}
<pre>
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
Element 24256 ( 1001) is first > 1000
Element 43302 ( 2009) is first > 2000
Element 61709 ( 3001) is first > 3000
Element 81457 ( 4003) is first > 4000
Element 98705 ( 5021) is first > 5000
Element 121343 ( 6009) is first > 6000
Element 151757 ( 7035) is first > 7000
Element 168805 ( 8036) is first > 8000
Element 184429 ( 9014) is first > 9000
Element 201789 ( 10007) is first > 10000
</pre>
 
=={{header|FreeBASIC}}==
Line 231 ⟶ 294:
</syntaxhighlight>{{out}} Similar to Python output.
[[File:Inventory Sequence (julia).svg.svg|thumb|center]]
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="Mathematica">
(*Function to generate the inventory sequence*)
InventorySequence[terms_] :=
Module[{num = 0, alst = {0}, inventory, c}, inventory = <|0 -> 1|>;
Table[c = Lookup[inventory, num, 0];
num = If[c == 0, 0, num + 1];
alst = Append[alst, c];
inventory[c] = Lookup[inventory, c, 0] + 1;, {n, 2, terms}];
alst]
 
(*Generate the inventory sequence*)
biglist = InventorySequence[201790];
 
(*Print first 100 elements of the sequence*)
partitioned = Partition[Take[biglist, 100], 10];
Do[Print[Row[partitioned[[i]], " "]], {i, Length[partitioned]}]
 
 
(*Find and print the first occurrences of elements>=thresholds*)
thresholds = 1000 Range[1, 10];
firstOccurrences =
Reap[Do[If[biglist[[i]] >= thresholds[[1]],
Sow[{thresholds[[1]], biglist[[i]], i}];
thresholds = Rest[thresholds];
If[Length[thresholds] == 0, Break[]];], {i, Length[biglist]}]][[
2, 1]];
 
(*Print the formatted results*)
Do[Print["First element \[GreaterEqual] ", firstOccurrence[[1]],
" is ", firstOccurrence[[2]], " at index ",
firstOccurrence[[3]]], {firstOccurrence, firstOccurrences}]
 
(*Plot the first 10,000 elements of the sequence*)
ListPlot[biglist[[1 ;; 10000]], Joined -> True,
PlotStyle -> {Thin, Blue}, PlotRange -> Full]
</syntaxhighlight>
{{out}}
<pre>
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 >= 1000 is 1001 at index 24256
First element >= 2000 is 2009 at index 43302
First element >= 3000 is 3001 at index 61709
First element >= 4000 is 4003 at index 81457
First element >= 5000 is 5021 at index 98705
First element >= 6000 is 6009 at index 121343
First element >= 7000 is 7035 at index 151757
First element >= 8000 is 8036 at index 168805
First element >= 9000 is 9014 at index 184429
First element >= 10000 is 10007 at index 201789
</pre>
 
[[File:Plot the first 10, 000 elements of the inventory sequence.svg|thumb|center]]
 
 
=={{header|Nim}}==
Line 296 ⟶ 423:
</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}}==
Line 396 ⟶ 591:
 
plot(biglist[:10_000], linewidth=0.3)
plt.show()
</syntaxhighlight>{{out}}
<pre>
Line 488 ⟶ 684:
[[File:Inventory-raku.png|400px|thumb|left|]]
<br clear=all>
=={{header|RPL}}==
For efficiency reasons, two different programs are needed to generate the sequence or search for the first high value.
« → max
« { 0 1 1 0 } <span style="color:grey">@ need to start with a non-null cycle to have ∑LIST work</span>
'''WHILE''' DUP SIZE max < '''REPEAT'''
0 max '''FOR''' j
DUP 1 « j == » DOLIST ∑LIST <span style="color:grey">@ count occurrences in the list</span>
'''IF''' DUP NOT '''THEN''' max 'j' STO '''END'''
+
'''NEXT'''
'''END'''
1 max SUB
» '<span style="color:blue">INVT</span>' STO <span style="color:grey">@ ''( n → { a(1)..a(n)} )''</span>
« DUP 1 + { } + 0 CON -1 → max counts j
« 2 CF 1
'''DO''' 'counts' 'j' INCR 1 +
'''IFERR''' GET THEN DROP2 0 '''END'''
'''IF''' DUP NOT THEN -1 'j' STO '''END'''
'''IF''' DUP max > '''THEN'''
"element" →TAG SWAP "position" →TAG 2 SF
'''ELSE'''
'counts' SWAP 1 + DUP2 GET 1 + PUT 1 +
'''END'''
'''UNTIL''' 2 FS? '''END'''
» » '<span style="color:blue">INVT1ST</span>' STO <span style="color:grey">@ ''( n → 1st_value_>_n pos )''</span>
 
100 <span style="color:blue">INVT</span>
1000 <span style="color:blue">INVT1ST</span>
{{out}}
<pre>
3: { 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 }
2: element: 1001
1: position: 24256
</pre>
 
=={{header|Ruby}}==
Not actually counting but keeping count in a hash:
<syntaxhighlight lang="ruby" line>n = 0
counter = Hash.new(0)
inventory = loop.lazy.map do
c = counter[n]
counter[c] += 1
c == 0 ? n = 0 : n += 1
c
end
inventory.first(100).each_slice(10){|s| puts "%4d"*s.size % s}
puts
 
(1000..10000).step(1000).each do |t|
counter.clear
puts "First element >= #{t} : %d index %d" % inventory.with_index.detect{|e,i| e > t}
end
</syntaxhighlight>
{{out}}
<pre> 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 >= 1000 : 1001 index 24255
First element >= 2000 : 2009 index 43301
First element >= 3000 : 3001 index 61708
First element >= 4000 : 4003 index 81456
First element >= 5000 : 5021 index 98704
First element >= 6000 : 6009 index 121342
First element >= 7000 : 7035 index 151756
First element >= 8000 : 8036 index 168804
First element >= 9000 : 9014 index 184428
First element >= 10000 : 10007 index 201788
</pre>
 
=={{header|Wren}}==
{{libheader|DOME}}
Line 493 ⟶ 767:
{{libheader|Wren-iterate}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "dome" for Window
import "graphics" for Canvas, Color
import "./plot" for Axes
1,151

edits