Inventory sequence: Difference between revisions

→‎{{header|RPL}}: improved code
(→‎{{header|RPL}}: improved code)
 
(19 intermediate revisions by 10 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}}==
<syntaxhighlight lang="vb">Dim As Integer max = 10000
Dim As Integer inv()
Dim As Integer counts(max + 100)
counts(0) = 1
Dim As Integer lower = 100
Dim As Integer upper = 1000
Dim As Boolean done = False
Dim As Integer ix = 0
While Not done
Dim As Integer i = 0, c = 0
Do
Dim As Integer j = counts(i)
If Ubound(inv) < max Then
Redim Preserve inv(ix+1)
inv(ix+1) = j
End If
counts(j) += 1
ix += 1
If Ubound(inv) >= lower Then
Print "Inventory sequence, first 100 elements:"
For c = 0 To 99
Print Using "###"; inv(c);
If (c+1) Mod 20 = 0 Then Print
Next
lower = max + 1
End If
If j = 0 Then Exit Do
If j >= upper Then
Print Using !"\nFirst element >= ##,### is ##,### at index ###,###"; upper; j; ix;
If j >= max Then done = True: Exit Do
upper += 1000
End If
i += 1
Loop
Wend
 
Sleep</syntaxhighlight>
 
=={{header|J}}==
Line 191 ⟶ 293:
plot(toplot)
</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}}==
<syntaxhighlight lang="Nim>import std/[strformat, tables]
import gnuplot
 
iterator inventorySequence(): (int, int) =
var counts: CountTable[int]
var idx = -1
while true:
var i = 0
while true:
let n = counts[i]
inc idx
counts.inc(n)
yield (idx, n)
if n == 0: break
inc i
 
echo "First 100 elements:"
var x, y: seq[int]
var lim = 1000
for idx, n in inventorySequence():
if idx < 10000:
x.add idx
y.add n
if idx <= 100:
stdout.write &"{n:>2}"
stdout.write if idx mod 10 == 0: '\n' else: ' '
if idx == 100: echo()
elif n >= lim:
echo &"First element ⩾ {lim:>5} is {n:>5} at index {idx:>6}"
lim += 1000
if lim > 10000: break
 
withGnuPlot:
plot(x, y, "Inventory sequence", "with impulses lw 0.5")
png("inventory_sequence.png")
</syntaxhighlight>
 
{{out}}
<pre>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 8
 
First element ⩾ 1000 is 1001 at index 24255
First element ⩾ 2000 is 2009 at index 43301
First element ⩾ 3000 is 3001 at index 61708
First element ⩾ 4000 is 4003 at index 81456
First element ⩾ 5000 is 5021 at index 98704
First element ⩾ 6000 is 6009 at index 121342
First element ⩾ 7000 is 7035 at index 151756
First element ⩾ 8000 is 8036 at index 168804
First element ⩾ 9000 is 9014 at index 184428
First element ⩾ 10000 is 10007 at index 201788
</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}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Inventory_sequence.exw</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">inventory</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">limit</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">inv</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},</span> <span style="color: #000000;">counts</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">ix</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">thousands</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1000</span>
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">j</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">>=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">counts</span><span style="color: #0000FF;">)?</span><span style="color: #000000;">0</span><span style="color: #0000FF;">:</span><span style="color: #000000;">counts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
<span style="color: #000000;">inv</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">j</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">>=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">counts</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> <span style="color: #000000;">counts</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #000000;">counts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">ix</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">inv</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">100</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Inventory sequence, first 100 elements:\n%s\n"</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">{</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">inv</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">:=</span><span style="color: #008000;">"%3d"</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">thousands</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First element &gt;= %,6d is %,6d at index %,7d\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">thousands</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ix</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">limit</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">inv</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">thousands</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1000</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">lim</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1e4</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lim</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">y</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">inventory</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lim</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">include</span> <span style="color: #7060A8;">IupGraph</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">get_data</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000000;">graph</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">w</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetIntInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">graph</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"SIZE"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">graph</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"XTICK"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w</span><span style="color: #0000FF;"><</span><span style="color: #000000;">500</span><span style="color: #0000FF;">?</span><span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w</span><span style="color: #0000FF;"><</span><span style="color: #000000;">350</span><span style="color: #0000FF;">?</span><span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w</span><span style="color: #0000FF;"><</span><span style="color: #000000;">250</span><span style="color: #0000FF;">?</span><span style="color: #000000;">5000</span><span style="color: #0000FF;">:</span><span style="color: #000000;">2500</span><span style="color: #0000FF;">):</span><span style="color: #000000;">2000</span><span style="color: #0000FF;">):</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">IupSetInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">graph</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"YTICK"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h</span><span style="color: #0000FF;"><</span><span style="color: #000000;">350</span><span style="color: #0000FF;">?</span><span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h</span><span style="color: #0000FF;"><</span><span style="color: #000000;">200</span><span style="color: #0000FF;">?</span><span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h</span><span style="color: #0000FF;"><</span><span style="color: #000000;">150</span><span style="color: #0000FF;">?</span> <span style="color: #000000;">200</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">100</span><span style="color: #0000FF;">):</span> <span style="color: #000000;">80</span><span style="color: #0000FF;">):</span> <span style="color: #000000;">40</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #004600;">CD_BLUE</span><span style="color: #0000FF;">}}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">graph</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGraph</span><span style="color: #0000FF;">(</span><span style="color: #000000;">get_data</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"XMIN=0,XMAX=10000,YMIN=0,YMAX=400"</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">graph</span><span style="color: #0000FF;">,</span><span style="color: #008000;">`TITLE=gGraph,SIZE=320x240,MINSIZE=240x140`</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</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 at index 24,255
First element >= 2,000 is 2,009 at index 43,301
First element >= 3,000 is 3,001 at index 61,708
First element >= 4,000 is 4,003 at index 81,456
First element >= 5,000 is 5,021 at index 98,704
First element >= 6,000 is 6,009 at index 121,342
First element >= 7,000 is 7,035 at index 151,756
First element >= 8,000 is 8,036 at index 168,804
First element >= 9,000 is 9,014 at index 184,428
First element >= 10,000 is 10,007 at index 201,788
</pre>
[[File:Phix_inventory_sequence_graph.png|thumb|center]]
 
=={{header|Python}}==
Line 220 ⟶ 591:
 
plot(biglist[:10_000], linewidth=0.3)
plt.show()
</syntaxhighlight>{{out}}
<pre>
Line 312 ⟶ 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 317 ⟶ 767:
{{libheader|Wren-iterate}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "dome" for Window
import "graphics" for Canvas, Color
import "./plot" for Axes
Line 410 ⟶ 860:
[[File:Inventory-wren.png|500px|thumb|left]]
<br clear=all>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">include xpllib; \for Print
 
def Size = 201_790;
int Seq(Size), SeqEnd, Num, Count, N, Thresh;
[SeqEnd:= 0;
loop [Num:= 0;
repeat Count:= 0;
for N:= 0 to SeqEnd-1 do
if Num = Seq(N) then Count:= Count+1;
Seq(SeqEnd):= Count;
SeqEnd:= SeqEnd+1;
if SeqEnd >= Size then quit;
Num:= Num+1;
until Count = 0;
];
Thresh:= 1000;
for N:= 0 to SeqEnd-1 do
if N < 100 then
[Print("%3.0f", float(Seq(N)));
if rem(N/20) = 19 then CrLf(0);
]
else if Seq(N) >= Thresh then
[Print("First element >= %5.0f: %5.0f in position %6.0f\n",
float(Thresh), float(Seq(N)), float(N));
if Thresh >= 10000 then return;
Thresh:= Thresh + 1000;
];
]</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 in position 24255
First element >= 2000: 2009 in position 43301
First element >= 3000: 3001 in position 61708
First element >= 4000: 4003 in position 81456
First element >= 5000: 5021 in position 98704
First element >= 6000: 6009 in position 121342
First element >= 7000: 7035 in position 151756
First element >= 8000: 8036 in position 168804
First element >= 9000: 9014 in position 184428
First element >= 10000: 10007 in position 201788
</pre>
1,151

edits