Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) 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!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (14)
    As of 2014-12-18 12:50 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (51 votes), past polls