Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
37
Task/Hailstone-sequence/Perl/hailstone-sequence-1.pl
Normal file
37
Task/Hailstone-sequence/Perl/hailstone-sequence-1.pl
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#!/usr/bin/perl
|
||||
|
||||
use warnings;
|
||||
use strict;
|
||||
|
||||
my @h = hailstone(27);
|
||||
print "Length of hailstone(27) = " . scalar @h . "\n";
|
||||
print "[" . join(", ", @h[0 .. 3], "...", @h[-4 .. -1]) . "]\n";
|
||||
|
||||
my ($max, $n) = (0, 0);
|
||||
for my $x (1 .. 99_999) {
|
||||
@h = hailstone($x);
|
||||
if (scalar @h > $max) {
|
||||
($max, $n) = (scalar @h, $x);
|
||||
}
|
||||
}
|
||||
|
||||
print "Max length $max was found for hailstone($n) for numbers < 100_000\n";
|
||||
|
||||
|
||||
sub hailstone {
|
||||
my ($n) = @_;
|
||||
|
||||
my @sequence = ($n);
|
||||
|
||||
while ($n > 1) {
|
||||
if ($n % 2 == 0) {
|
||||
$n = int($n / 2);
|
||||
} else {
|
||||
$n = $n * 3 + 1;
|
||||
}
|
||||
|
||||
push @sequence, $n;
|
||||
}
|
||||
|
||||
return @sequence;
|
||||
}
|
||||
15
Task/Hailstone-sequence/Perl/hailstone-sequence-2.pl
Normal file
15
Task/Hailstone-sequence/Perl/hailstone-sequence-2.pl
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#!/usr/bin/perl
|
||||
use strict;
|
||||
|
||||
sub hailstone {
|
||||
@_ = local $_ = shift;
|
||||
push @_, $_ = $_ % 2 ? 3 * $_ + 1 : $_ / 2 while $_ > 1;
|
||||
@_;
|
||||
}
|
||||
|
||||
my @h = hailstone($_ = 27);
|
||||
print "$_: @h[0 .. 3] ... @h[-4 .. -1] (".@h.")\n";
|
||||
|
||||
@h = ();
|
||||
for (1 .. 99_999) { @h = ($_, $h[2]) if ($h[2] = hailstone($_)) > $h[1] }
|
||||
printf "%d: (%d)\n", @h;
|
||||
Loading…
Add table
Add a link
Reference in a new issue