|
| 1 | +#!/usr/bin/env raku |
| 2 | +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ |
| 3 | +use v6; |
| 4 | +INIT $*RAT-OVERFLOW = FatRat; |
| 5 | +use Test; |
| 6 | + |
| 7 | +=begin comment |
| 8 | +003-1 |
| 9 | +Create a script to generate 5-smooth numbers, whose prime divisors are less or equal to 5. |
| 10 | +They are also called Hamming/Regular/Ugly numbers. For more information, please check this |
| 11 | +wikipedia. |
| 12 | +=end comment |
| 13 | + |
| 14 | +my @Test = |
| 15 | + 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, |
| 16 | + 16, 18, 20, 24, 25, 27, 30, 32, 36, 40, |
| 17 | + 45, 48, 50, 54, 60, 64, 72, 75, 80, 81, |
| 18 | + 90, 96, 100, 108, 120, 125, 128, 135, 144, 150, |
| 19 | + 160, 162, 180, 192, 200, 216, 225, 240, 243, 250, |
| 20 | + 256, 270, 288, 300, 320, 324, 360, 375, 384, 400, |
| 21 | + 405, |
| 22 | +; |
| 23 | + |
| 24 | +sub is-five-smooth( Int $n -->Bool) { return not so prime-factors($n).any > 5; } |
| 25 | + |
| 26 | +constant @prime = 2,3, { $_+=2; $_+=2 until .is-prime; $_; } … ∞; |
| 27 | + |
| 28 | +constant @five-smooth = 2,3,4,5,6, { $_+=1; $_+=1 until is-five-smooth($_); $_ } … ∞; |
| 29 | + |
| 30 | +sub prime-factors( $int -->Array) is export { |
| 31 | + my $n = $int; |
| 32 | + my @ret; |
| 33 | + my $ix = 0; |
| 34 | + while $n > 1 { |
| 35 | + my $candi = @prime[$ix]; |
| 36 | + $ix++; |
| 37 | + next unless ( $n %% $candi ); |
| 38 | + $ix = 0; |
| 39 | + $n ÷= $candi; |
| 40 | + @ret.push: $candi; |
| 41 | + } |
| 42 | + return @ret; |
| 43 | +} |
| 44 | + |
| 45 | +plan 1; |
| 46 | +is @five-smooth[0..60], @Test, '@five-smooth[0..60]'; |
| 47 | +done-testing; |
| 48 | + |
| 49 | +exit; |
0 commit comments