Skip to content

Commit 7f7a7c5

Browse files
author
Filip Sergot
committed
hailstone-sequence.pl works.
1 parent bef4145 commit 7f7a7c5

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed
Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
=head1 Description
2-
The Hailstone sequence of numbers can be generated from a starting positive integer, n by:
3-
If n is 1 then the sequence ends.
4-
If n is even then the next n of the sequence = n/2
5-
If n is odd then the next n of the sequence = (3 * n) + 1
6-
The (unproven), Collatz conjecture is that the hailstone sequence for any starting number always terminates.
7-
Task Description:
8-
Create a routine to generate the hailstone sequence for a number.
9-
Use the routine to show that the hailstone sequence for the number 27 has 112 elements starting with 27, 82, 41, 124 and ending with 8, 4, 2, 1
10-
Show the number less than 100,000 which has the longest hailstone sequence together with that sequences length.
11-
(But don't show the actual sequence)!
1+
#=head1 Description
2+
# The Hailstone sequence of numbers can be generated from a starting positive integer, n by:
3+
# If n is 1 then the sequence ends.
4+
# If n is even then the next n of the sequence = n/2
5+
# If n is odd then the next n of the sequence = (3 * n) + 1
6+
# The (unproven), Collatz conjecture is that the hailstone sequence for any starting number always terminates.
7+
# Task Description:
8+
# Create a routine to generate the hailstone sequence for a number.
9+
# Use the routine to show that the hailstone sequence for the number 27 has 112 elements starting with 27, 82, 41, 124 and ending with 8, 4, 2, 1
10+
# Show the number less than 100,000 which has the longest hailstone sequence together with that sequences length.
11+
# (But don't show the actual sequence)!
1212

13-
=head1 Code
13+
#=head1 Code
1414
sub hailstone($n) { $n, { $_ %% 2 ?? $_ div 2 !! $_ * 3 + 1 } ... 1 }
1515

1616
my @h = hailstone(27);
1717
say "Length of hailstone(27) = {+@h}";
1818
say ~@h;
1919

20-
my $m max= +hailstone($_) => $_ for 1..99_999;
20+
my $m = 0 => 0;
21+
$m max= +hailstone($_) => $_ for 1..99_999;
2122
say "Max length $m.key() was found for hailstone($m.value()) for numbers < 100_000";
2223

23-
=head2 More
24-
http://rosettacode.org/wiki/Hailstone_sequence#Perl_6
24+
#=head2 More
25+
# http://rosettacode.org/wiki/Hailstone_sequence#Perl_6

0 commit comments

Comments
 (0)