Posted By

cmm7825 on 05/20/10


Tagged


Versions (?)

Problem 14


 / Published in: Perl
 

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. my $i=999999;
  6. my $max=0;
  7. my $maxchain=0;
  8.  
  9. sub collatz
  10. {
  11. my $n = shift;
  12. my $count = 1;
  13. while($n > 1){
  14. if($n % 2 == 0){
  15. $n = $n/2;
  16. } else{
  17. $n = 3*$n + 1;
  18. }
  19. $count++;
  20. }
  21. return $count;
  22. }
  23.  
  24. while($i > 1){
  25. my $value = collatz($i);
  26. if ($value > $maxchain){
  27. $max = $i;
  28. $maxchain = $value;
  29. }
  30. $i--;
  31. }
  32.  
  33. print "$max\n";

Report this snippet  

You need to login to post a comment.