It's about to get math-y up in here.

This finds which number produces the most terms in the Collatz sequence, up to a value you specify.

For those who don't know, the iterative sequence in the Collatz sequence is:

If n is odd: Multiply by three and add one.

If n is even: Divide by two.

(n*3+1)or(n/2);

Continue until you end up at one.

It's believed every number will eventually end up at one.

```
#!/usr/bin/perl
use warnings; use strict;
my @longest;
print "Max value: ";
my $length = <STDIN>;
print "[Number::Terms]\n";
for(my $i=2; $i<$length; $i++) {
my $num = $i;
my @current;
while($num > 1) {
$num = $num % 2 ? (3*$num+1) : ($num/2);
push(@current, $num);
}
if(scalar(@current) > scalar(@longest)) {
@longest = undef;
@longest = $current;
print "$i:" . scalar(@longest) . "\n";
}
}
```

Sign in to comment.

Are you sure you want to unfollow this person?

Are you sure you want to delete this?

Click "Unsubscribe" to stop receiving notices pertaining to this post.

Click "Subscribe" to resume notices pertaining to this post.