Inventory sequence: Difference between revisions
Content added Content deleted
m (Added phix output/image) |
(Created Nim solution.) |
||
Line 231: | Line 231: | ||
</syntaxhighlight>{{out}} Similar to Python output. |
</syntaxhighlight>{{out}} Similar to Python output. |
||
[[File:Inventory Sequence (julia).svg.svg|thumb|center]] |
[[File:Inventory Sequence (julia).svg.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|Phix}}== |
=={{header|Phix}}== |