Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use v5.12;
- use strict;
- use warnings;
- my @max = qw/0 0/;
- my %results = {
- 1 => 0,
- 2 => 1,
- };
- sub chainLength {
- my $arg = shift;
- my $num = $arg;
- my $count = 0;
- while ( $num > 2 ) {
- if ( exists $results{$num} ) {
- return ( $results{$arg} = $count + int( $results{$num} ) );
- }
- ( ( $num % 2 ) == 0 ) ? ( $num /= 2 ) : ( $num = 3 * $num + 1 );
- $count++;
- }
- return ( $results{$arg} = $count );
- }
- for my $i ( 2 .. 1000000 ) {
- my $res = chainLength($i);
- if ( $res > $max[1] ) {
- $max[0] = $i;
- $max[1] = $res;
- }
- }
- print "@max";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement