diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-12-22 02:23:00 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-12-22 02:23:00 +0000 |
| commit | b4d03121426826be23eeae72bf6ff53efd111dc2 (patch) | |
| tree | c50b301517936f32b509f86db629123680f1cc64 /challenge-144 | |
| parent | 9b6eb33a2212ec55e534b407a5d275032b1822a7 (diff) | |
| download | perlweeklychallenge-club-b4d03121426826be23eeae72bf6ff53efd111dc2.tar.gz perlweeklychallenge-club-b4d03121426826be23eeae72bf6ff53efd111dc2.tar.bz2 perlweeklychallenge-club-b4d03121426826be23eeae72bf6ff53efd111dc2.zip | |
- Added solution by David Santiago.
Diffstat (limited to 'challenge-144')
| -rwxr-xr-x | challenge-144/david-santiago/raku/ch-1.raku | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/challenge-144/david-santiago/raku/ch-1.raku b/challenge-144/david-santiago/raku/ch-1.raku new file mode 100755 index 0000000000..422fb4955f --- /dev/null +++ b/challenge-144/david-santiago/raku/ch-1.raku @@ -0,0 +1,64 @@ +#!/usr/bin/env raku + +sub sieve-of-atkin(Int $limit --> List){ + my @sieve; + my @prime-list = (2,3); + + my $x = 1; + while $x ** 2 < $limit { + my $y = 1; + while $y ** 2 < $limit { + + my $n = 4 * $x ** 2 + $y ** 2; + + if $n <= $limit && ($n % 12 == 1 || $n % 12 == 5) { + @sieve[$n] = @sieve[$n] ?? False !! True; + } + + $n = 3 * $x ** 2 + $y ** 2; + if $n <= $limit && $n % 12 == 7 { + @sieve[$n] = @sieve[$n] ?? False !! True; + } + + $n = (3 * $x ** 2 ) - $y ** 2; + if $n <= $limit && $x > $y && $n % 12 == 11 { + @sieve[$n] = @sieve[$n] ?? False !! True; + } + $y++; + } + $x++; + } + # Mark all multiples of squares as non-prime + my $r = 5; + while $r ** 2 < $limit { + if @sieve[$r] { + loop ( my $i = $r**2; $i < $limit; $i += $r**2 ) { + @sieve[$i] = False; + } + } + $r++; + } + + for @sieve.kv -> $index, $is-prime { + @prime-list.append($index) if $is-prime; + } + + return @prime-list; +} + + +sub MAIN() { + my @prime-list := sieve-of-atkin(100); + my @combinations = (|@prime-list xx 2 ).flat.combinations(2); + + my %semi-primes; + for @combinations -> @comb { + my $candidate = @comb[0]*@comb[1]; + %semi-primes{$candidate} = (@comb[0],@comb[1]) if $candidate < 100; + } + + for %semi-primes.keys.sort { + say "$_ is Semiprime as $_ = {%semi-primes{$_}.join(' x ')}"; + } + +} |
