/ Published in: Perl
See [blog post](http://highered.blogspot.com/2010/11/collatz-ecologies.html)
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#set up parameters.................................. $first = 3; $end = 100; # upper bound on the N to use $max_gen = 1000; # how many generations to run for each $max_pop = 100; # carrying capacity C # first add some column headings # main loop to generate summary stats and individual report files for each N for ($start = $first ;$start < $end; $start+=2) { #just do the odd numbers sim($start); } # run the simulation for the given N sub sim { $id = $_[0]; # the starting number N is the argument # you need to have a folder called 'sim' to dump the details in $generation = 0; #initialize for this run $all{$id} = $n{$id} = 1; # associative arrays will keep track of who was and is alive $last = $start; #keeps track of the biggest number reached for the summary report while($generation < $max_gen) { $generation++; $kids = $died = 0; #keeps track of reproduction and extinction for this generation if ($v % 2 == 0) { # if even divide by two $v /=2; if ($v == 1) { # kill it if it reaches 1 # it will vanish because it doesn't get copied to %n1 $died++; # keep track of how many we lost } else { $all{$v} = $n1{$v} = 1; # %n1 is the temp array for this work } } else { # it's odd, so do (3n+1)/2. Unless we're about to blow up our precision. # have to have a rule about overflow. I'll chose to kill it off as being a hog. if ($v > 1000000000) { # overflow problem $died++; } else { $v = (3*$v + 1)/2; $all{$v} = $n1{$v} = 1; copy($v); # make the mutant--see function below } } } %n = %n1; # reset for the next trial. Yes, I should use pointers instead. I know. #now add in the new generation last if $npop >= $max_pop; # stop when we get to the capacity next if $n{$num}; # already have this one in the population $n{$num} = 1; # add it in $npop++; # increment the total current population $kids++; } # this prints out the whole population for this generation and N into N.txt in the sim folder } } close START; # the sim is done. Print out what integers were missed between 2 and 1000 for($i =2;$i<1000;$i++) { } } sub copy { #reproduce a number with mutation $t = $_[0] - 2; $all{$t} = $n2{$t}=1; #keep the kids separate, so we can apply the carrying capacity $last = $t if $t > $last; #find the biggest one reproduced }
URL: http://highered.blogspot.com/2010/11/collatz-ecologies.html