|
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)! |
12 | 12 |
|
13 |
| -=head1 Code |
| 13 | +#=head1 Code |
14 | 14 | sub hailstone($n) { $n, { $_ %% 2 ?? $_ div 2 !! $_ * 3 + 1 } ... 1 }
|
15 | 15 |
|
16 | 16 | my @h = hailstone(27);
|
17 | 17 | say "Length of hailstone(27) = {+@h}";
|
18 | 18 | say ~@h;
|
19 | 19 |
|
20 |
| - my $m max= +hailstone($_) => $_ for 1..99_999; |
| 20 | + my $m = 0 => 0; |
| 21 | + $m max= +hailstone($_) => $_ for 1..99_999; |
21 | 22 | say "Max length $m.key() was found for hailstone($m.value()) for numbers < 100_000";
|
22 | 23 |
|
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