Perl: the Markov chain saw PerlMonks

### Comment on

 Need Help??
I offer one last solution, which addresses an issue raised in Re: Functional perl please: "(we exclude "solutions" which must store the entire sequence in a large array and the like)". My previous solutions did store the array, because they returned it as one piece.

This sub is an iterator, returning the next in the sequence on each call. It maintains only the portion of the output stream that it needs to continue generating.

I used Math::BigInt so I could get into interesting numbers. It takes several minutes to find the 100000th or so in the series. But if you're interested, #100001 through 100005 are

```290237644800000000000000000000000000000
290420756304773155911401472000000000000
290469810882260083566182400000000000000
290565366750000000000000000000000000000
290748685015200298451950845000000000000
with 5446 elements in the queue.
```use strict;
use warnings;
use Math::BigInt;
{
my @history = (Math::BigInt->new('1'));
my @streams = map {base => \$_, index => 0}, (2, 3, 5);
sub print_status {
print "\$_->{base}: \$_->{index}\n" for @streams;
print "History is " . @history . " elements\n";
}
sub ham_iter {
# Make sure all stream indexes are > 0
while (\$streams[2]{index} == 0) {
# Find the next lowest value to push onto history
my @peeks = map {
\$history[\$_->{index}]->copy()->bmul(\$_->{base})
} @streams;
my (\$lowest) = sort {\$a->bcmp(\$b)} @peeks;
\$peeks[\$_]->bcmp(\$lowest) or \$streams[\$_]{index}++ for 0..
+\$#streams;
push(@history, \$lowest);
}
# Now adjust all the indexes for the element being removed
\$_->{index}-- for @streams;
shift @history;
}
}
# Skipping however many
ham_iter() for (1..10);
# Then print the next five
print ham_iter->bstr(), "\n" for (1..5);
# Then tell us about what's stored
print_status();

Caution: Contents may have been coded under pressure.

In reply to Re: Hamming Sequences and Lazy Lists by Roy Johnson
in thread Hamming Sequences and Lazy Lists by tall_man

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2017-10-23 13:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My fridge is mostly full of:

Results (279 votes). Check out past polls.

Notices?