Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: A script with a loop is running my computer Out of memory

by wiredrat (Acolyte)
on Feb 03, 2011 at 17:03 UTC ( [id://886024]=note: print w/replies, xml ) Need Help??


in reply to A script with a loop is running my computer Out of memory

You can also cut the number of 'concurrent' generations. Just keepeing 3 generations is a good approximation for human comunity. You can keep the number of offsprings per generation in a array and keeping the array 3 values long. So, borrowing ideas from previuss replies
my @generations = ( $generation ); for(2 .. $generations ) { push @generations, $generations[-1]*2.5; shift @generations if $#generations >= 3; }

Replies are listed 'Best First'.
Re^2: A script with a loop is running my computer Out of memory
by Lady_Aleena (Priest) on Feb 14, 2011 at 04:25 UTC

    wiredrat, you got me thinking about what I was really after and are right about me wanting the total population. Up in Re^2: A script with a loop is running my computer Out of memory I was talking about the number 250 billion. Well, that is the total population I am trying to get without something clunking out on me. So, I did a little finagling and came up with the following which returns the amount born into a generation and the total population which include the previous two generations. So, while I am still storing every generation generated, the current code is faster even with the new addition of total population.

    #!/usr/bin/perl -l use strict; use warnings; use feature qw(say); use List::Util qw(sum max); use Data::Dumper; use lib 'lib'; use Base::Nifty qw(commify); say "How many children are in the first generation?"; my $generation = <>; say "How many generations do you want to generate?"; my $generations = <>; chomp($generation,$generations); my %generations = ( 1 => { children => $generation, total_population => $generation, } ); my @distribution = (0,0,0,1,1,1,2,2,2,3,3,4,5,6); for my $gen (2..$generations) { my $children = 0; for (1..$generation) { $children += $distribution[rand(@distribution)]; } $generation = $children; last if ($children == 0); $generations{$gen}{children} = $generation; my $total_population = $generations{$gen}{children}; $total_population += $generations{$gen - 1}{children} if $generati +ons{$gen - 1}; $total_population += $generations{$gen - 2}{children} if $generati +ons{$gen - 2}; $generations{$gen}{total_population} = $total_population; } my @children; my @population; for (1..$generations) { push @children, $generations{$_}{children}; push @population, $generations{$_}{total_population}; } my $max_length_generation = length(commify(max(keys %generations))); my $max_length_children = length(commify(max(@children))); my $max_length_population = length(commify(max(@population))); for (sort {$a <=> $b} keys %generations) { printf "%${max_length_generation}s: %${max_length_children}s %${max_ +length_population}s\n", $_,commify($generations{$_}{children}),commify($generations{$_}{tota +l_population}); }

    Thanks again for the idea!

    Have a cookie and a very nice day!
    Lady Aleena

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://886024]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (9)
As of 2024-04-19 09:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found