Prime words: Difference between revisions

Content added Content deleted
m (→‎{{header|Raku}}: typos, tweaks)
(→‎{{header|Raku}}: DRY, more concise)
Line 253: Line 253:


<lang perl6>my @words = 'unixdict.txt'.IO.words».fc;
<lang perl6>my @words = 'unixdict.txt'.IO.words».fc;

sub display ($n, @n, $s = "First 20: ") {"$n;\n{$s}{@n.join: ', '}"}


say 'Number of words whose ords are all prime: ',
say 'Number of words whose ords are all prime: ',
@words.hyper.map({ next unless all(.comb.».ord).is-prime; $_ }).&{"{+$_};\n{.join: ', '}"};
@words.hyper.grep({ all(.comb.».ord).is-prime }).&{display +$_, $_, ''};


say "\nNumber of words whose ordinal sum is prime: ",
say "\nNumber of words whose ordinal sum is prime: ",
@words.map({ $_ if .comb».ord.sum.is-prime }).&{"{+$_};\nFirst 20: {.head(20).join: ', '} ..."};
@words.grep({ .comb».ord.sum.is-prime }).&{display +$_,.head(20)};


say "\n\nInterpreting the words as if they were base 36 numbers:";
say "\n\nIterpreting the words as if they were base 36 numbers:";


say "\nNumber of words that are prime in base 36: ",
say "\nNumber of words that are prime in base 36: ",
@words.map({ next if .contains(/\W/); $_ if :36($_).is-prime }).&{"{+$_};\nFirst 20: {.head(20).join: ', '} ..."};
@words.grep({ !.contains(/\W/) && :36($_).is-prime }).&{display +$_,.head(20)};


say "\nNumber of words whose base 36 digital sum is prime: ",
say "\nNumber of words whose base 36 digital sum is prime: ",
@words.map({ next if .contains(/\W/); $_ if .comb».parse-base(36).sum.is-prime }).&{"{+$_};\nFirst 20: {.head(20).join: ', '} ..."};
@words.grep({ !.contains(/\W/) && .comb».parse-base(36).sum.is-prime }).&{display +$_,.head(20)};


use Base::Any:ver<0.1.2>;
use Base::Any:ver<0.1.2>;
Line 274: Line 276:


say "\nNumber of words whose 'digits' are all prime using a custom base: ",
say "\nNumber of words whose 'digits' are all prime using a custom base: ",
@words.hyper.map({ next if .contains(/<-alpha>/); $_ if all(.comb».&from-base(26)).is-prime }).&{"{+$_};\n{.join: ', '}"};
@words.hyper.grep({ !.contains(/<-alpha>/) && all(.comb».&from-base(26)).is-prime }).&{display +$_, $_, ''};


say "\nNumber of words that are prime using a custom base: ",
say "\nNumber of words that are prime using a custom base: ",
@words.map({ next if .contains(/<-alpha>/); $_ if .&from-base(26).is-prime }).&{"{+$_};\nFirst 20: {.head(20).join: ', '} ..."};
@words.grep({ !.contains(/<-alpha>/) && .&from-base(26).is-prime }).&{display +$_,.head(20)};


say "\nNumber of words whose digital sum is prime using a custom base: ",
say "\nNumber of words whose digital sum is prime using a custom base: ",
@words.map({ next if .contains(/<-alpha>/); $_ if .comb».&from-base(26).sum.is-prime }).&{"{+$_};\nFirst 20: {.head(20).join: ', '} ..."};</lang>
@words.grep({ !.contains(/<-alpha>/) && .comb».&from-base(26).sum.is-prime }).&{display +$_,.head(20)};</lang>
{{out}}
{{out}}
<pre>Number of words whose ords are all prime: 36;
<pre>Number of words whose ords are all prime: 36;