Prime words: Difference between revisions

Content added Content deleted
(J)
m (syntax highlighting fixup automation)
Line 24: Line 24:
<br><br>
<br><br>
=={{header|11l}}==
=={{header|11l}}==
<lang 11l>F is_prime(Int a)
<syntaxhighlight lang="11l">F is_prime(Int a)
I a == 2
I a == 2
R 1B
R 1B
Line 47: Line 47:
L.break
L.break
L.was_no_break
L.was_no_break
print(word)</lang>
print(word)</syntaxhighlight>


{{out}}
{{out}}
Line 92: Line 92:
In the following solution the input file is loaded from H6 drive. Altirra emulator automatically converts CR/LF character from ASCII into 155 character in ATASCII charset used by Atari 8-bit computer when one from H6-H10 hard drive under DOS 2.5 is used.
In the following solution the input file is loaded from H6 drive. Altirra emulator automatically converts CR/LF character from ASCII into 155 character in ATASCII charset used by Atari 8-bit computer when one from H6-H10 hard drive under DOS 2.5 is used.
{{libheader|Action! Sieve of Eratosthenes}}
{{libheader|Action! Sieve of Eratosthenes}}
<lang Action!>INCLUDE "H6:SIEVE.ACT"
<syntaxhighlight lang="action!">INCLUDE "H6:SIEVE.ACT"


BYTE FUNC IsPrimeWord(CHAR ARRAY word BYTE ARRAY primes)
BYTE FUNC IsPrimeWord(CHAR ARRAY word BYTE ARRAY primes)
Line 136: Line 136:
Sieve(primes,MAX+1)
Sieve(primes,MAX+1)
FindPrimeWords(fname,primes)
FindPrimeWords(fname,primes)
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Prime_words.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Prime_words.png Screenshot from Atari 8-bit computer]
Line 146: Line 146:
Does not distinguish between letters and non-letter ASCII codes (as with the REXX sample).
Does not distinguish between letters and non-letter ASCII codes (as with the REXX sample).
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-primes}}
<lang algol68># find words whose character codes are primes #
<syntaxhighlight lang="algol68"># find words whose character codes are primes #
IF FILE input file;
IF FILE input file;
STRING file name = "unixdict.txt";
STRING file name = "unixdict.txt";
Line 183: Line 183:
OD;
OD;
close( input file )
close( input file )
FI</lang>
FI</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 226: Line 226:
=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>words: read.lines relative "unixdict.txt"
<syntaxhighlight lang="rebol">words: read.lines relative "unixdict.txt"


loop words 'word [
loop words 'word [
if every? split word 'ch -> prime? to :integer to :char ch ->
if every? split word 'ch -> prime? to :integer to :char ch ->
print word
print word
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 273: Line 273:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f PRIME_WORDS.AWK unixdict.txt
# syntax: GAWK -f PRIME_WORDS.AWK unixdict.txt
BEGIN {
BEGIN {
Line 302: Line 302:
return(1)
return(1)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 309: Line 309:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <algorithm>
<syntaxhighlight lang="cpp">#include <algorithm>
#include <cstdlib>
#include <cstdlib>
#include <fstream>
#include <fstream>
Line 338: Line 338:
}
}
return EXIT_SUCCESS;
return EXIT_SUCCESS;
}</lang>
}</syntaxhighlight>


Contents of prime_sieve.hpp:
Contents of prime_sieve.hpp:
<lang cpp>#ifndef PRIME_SIEVE_HPP
<syntaxhighlight lang="cpp">#ifndef PRIME_SIEVE_HPP
#define PRIME_SIEVE_HPP
#define PRIME_SIEVE_HPP


Line 392: Line 392:
}
}


#endif</lang>
#endif</syntaxhighlight>


{{out}}
{{out}}
Line 409: Line 409:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2020-08-14}}
{{works with|Factor|0.99 2020-08-14}}
<lang factor>USING: io.encodings.ascii io.files math.primes prettyprint sequences ;
<syntaxhighlight lang="factor">USING: io.encodings.ascii io.files math.primes prettyprint sequences ;


"unixdict.txt" ascii file-lines [ [ prime? ] all? ] filter .</lang>
"unixdict.txt" ascii file-lines [ [ prime? ] all? ] filter .</syntaxhighlight>
{{out}}
{{out}}
<pre style="height: 45ex">
<pre style="height: 45ex">
Line 455: Line 455:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>
<syntaxhighlight lang="freebasic">
dim shared as boolean prime(0 to 29) =_
dim shared as boolean prime(0 to 29) =_
{false, true, false, true, true, false, false, true, false, true, false, false, true, false,_
{false, true, false, true, true, false, false, true, false, true, false, false, true, false,_
Line 483: Line 483:
close #1
close #1
end
end
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 524: Line 524:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 587: Line 587:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 631: Line 631:


=={{header|J}}==
=={{header|J}}==
<lang J> ($~ _2 */\ q:@#)(#~ (1 */@p: 3 u: ])@>) cutLF fread'unixdict.txt'
<syntaxhighlight lang="j"> ($~ _2 */\ q:@#)(#~ (1 */@p: 3 u: ])@>) cutLF fread'unixdict.txt'
┌─────┬───┬─────┬────┬─────┬───┬────┬───┬────┐
┌─────┬───┬─────┬────┬─────┬───┬────┬───┬────┐
│a │aaa│age │agee│ak │am │ama │e │egg │
│a │aaa│age │agee│ak │am │ama │e │egg │
Line 640: Line 640:
├─────┼───┼─────┼────┼─────┼───┼────┼───┼────┤
├─────┼───┼─────┼────┼─────┼───┼────┼───┼────┤
│ma │mae│magma│make│mamma│me │meek│meg│q │
│ma │mae│magma│make│mamma│me │meek│meg│q │
└─────┴───┴─────┴────┴─────┴───┴────┴───┴────┘</lang>
└─────┴───┴─────┴────┴─────┴───┴────┴───┴────┘</syntaxhighlight>


=={{header|jq}}==
=={{header|jq}}==
Line 652: Line 652:
definition of `is_prime_word` should focus on the alphabetic characters,
definition of `is_prime_word` should focus on the alphabetic characters,
perhaps along the lines of:
perhaps along the lines of:
<lang jq>def is_prime_word: all(gsub("[^A-Za-z]";"") | explode[]; is_prime);
<syntaxhighlight lang="jq">def is_prime_word: all(gsub("[^A-Za-z]";"") | explode[]; is_prime);
</syntaxhighlight>
</lang>
All such variations are as easy to accommodate as to envision, but for unixdict.txt the following suffices:
All such variations are as easy to accommodate as to envision, but for unixdict.txt the following suffices:
<lang jq>def is_prime_word: all(explode[]; is_prime);</lang>
<syntaxhighlight lang="jq">def is_prime_word: all(explode[]; is_prime);</syntaxhighlight>
<syntaxhighlight lang="jq">
<lang jq>
inputs | select(is_prime_word)</lang>
inputs | select(is_prime_word)</syntaxhighlight>
{{out}}
{{out}}
Invocation (example): jq -Rrn -f prime-words.jq unixdict.txt
Invocation (example): jq -Rrn -f prime-words.jq unixdict.txt
Line 701: Line 701:
=={{header|Julia}}==
=={{header|Julia}}==
See [[Alternade_words#Julia]] for the foreachword function.
See [[Alternade_words#Julia]] for the foreachword function.
<lang julia>const primelettervalues = [67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113]
<syntaxhighlight lang="julia">const primelettervalues = [67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113]
isprimeword(w, _) = all(c -> Int(c) in primelettervalues, collect(w)) ? w : ""
isprimeword(w, _) = all(c -> Int(c) in primelettervalues, collect(w)) ? w : ""
foreachword("unixdict.txt", isprimeword, colwidth=10, numcols=9)
foreachword("unixdict.txt", isprimeword, colwidth=10, numcols=9)
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Word source: unixdict.txt
Word source: unixdict.txt
Line 715: Line 715:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>dict=Import["https://web.archive.org/web/20180611003215/http://www.puzzlers.org/pub/wordlists/unixdict.txt","String"];
<syntaxhighlight lang="mathematica">dict=Import["https://web.archive.org/web/20180611003215/http://www.puzzlers.org/pub/wordlists/unixdict.txt","String"];
dict//=StringSplit[#,"\n"]&;
dict//=StringSplit[#,"\n"]&;
chars=CharacterRange["A","Z"]~Join~CharacterRange["a","z"];
chars=CharacterRange["A","Z"]~Join~CharacterRange["a","z"];
chars//=Select[ToCharacterCode/*First/*PrimeQ];
chars//=Select[ToCharacterCode/*First/*PrimeQ];
Select[dict,StringMatchQ[Repeated[Alternatives@@chars]]]//Column</lang>
Select[dict,StringMatchQ[Repeated[Alternatives@@chars]]]//Column</syntaxhighlight>
{{out}}
{{out}}
<pre>a
<pre>a
Line 759: Line 759:


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import strformat, strutils
<syntaxhighlight lang="nim">import strformat, strutils


func isPrime(n: Natural): bool =
func isPrime(n: Natural): bool =
Line 784: Line 784:
if word.allCharsInSet(PrimeChars):
if word.allCharsInSet(PrimeChars):
inc count
inc count
echo &"{count:2}: {word}"</lang>
echo &"{count:2}: {word}"</syntaxhighlight>


{{out}}
{{out}}
Line 825: Line 825:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict;
use strict;
Line 832: Line 832:
my $pat = join '', grep +(1 x ord) !~ /^(11+)\1+$/, 'a'..'z', 'A'..'Z';
my $pat = join '', grep +(1 x ord) !~ /^(11+)\1+$/, 'a'..'z', 'A'..'Z';
@ARGV = 'unixdict.txt';
@ARGV = 'unixdict.txt';
print join('', grep /^[$pat]+$/, <>) =~ tr/\n/ /r =~ s/.{1,71}\K /\n/gr;</lang>
print join('', grep /^[$pat]+$/, <>) =~ tr/\n/ /r =~ s/.{1,71}\K /\n/gr;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 840: Line 840:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">sap</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">word</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">))==</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">sap</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">word</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">))==</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">words</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">unix_dict</span><span style="color: #0000FF;">(),</span><span style="color: #000000;">sap</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">words</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">unix_dict</span><span style="color: #0000FF;">(),</span><span style="color: #000000;">sap</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d prime words found: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">),</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d prime words found: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">),</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)})</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 852: Line 852:


=={{header|Plain English}}==
=={{header|Plain English}}==
<lang plainenglish>To run:
<syntaxhighlight lang="plainenglish">To run:
Start up.
Start up.
Put "c:\unixdict.txt" into a path.
Put "c:\unixdict.txt" into a path.
Line 877: Line 877:
If the substring's first's target is not prime, say no.
If the substring's first's target is not prime, say no.
Add 1 to the substring's first.
Add 1 to the substring's first.
Repeat.</lang>
Repeat.</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:18em">
<pre style="height:18em">
Line 922: Line 922:


Here is the brute force pre-processor, again this can be done easily manually, but if you are feeling lazy, like me :)
Here is the brute force pre-processor, again this can be done easily manually, but if you are feeling lazy, like me :)
<syntaxhighlight lang="python">
<lang Python>
for i in range(65,123):
for i in range(65,123):
check = 1
check = 1
Line 930: Line 930:
if check==1:
if check==1:
print(chr(i),end='')
print(chr(i),end='')
</syntaxhighlight>
</lang>


This produces the following string :
This produces the following string :
Line 944: Line 944:
Tested on Python 3+, the file download will work only if the link is still active. It is possible that you may be able to fetch the file in your browser but download via code may still fail. Check whether you are connected to a VPN, it works on open networks.
Tested on Python 3+, the file download will work only if the link is still active. It is possible that you may be able to fetch the file in your browser but download via code may still fail. Check whether you are connected to a VPN, it works on open networks.


<syntaxhighlight lang="python">
<lang Python>
#Aamrun, 6th November 2021
#Aamrun, 6th November 2021


Line 961: Line 961:


[print(word) for word in wordList if len(set(word).difference(primeSet))==0]
[print(word) for word in wordList if len(set(word).difference(primeSet))==0]
</syntaxhighlight>
</lang>


And here's the final output :
And here's the final output :
Line 1,011: Line 1,011:
In an effort to anticipate / head-off a rash of tiny variant tasks, a series of one-liners:
In an effort to anticipate / head-off a rash of tiny variant tasks, a series of one-liners:


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


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


say "\nNumber of words that are prime in custom base 26, whose digits are all prime, and whose digital sum is prime: ",
say "\nNumber of words that are prime in custom base 26, whose digits are all prime, and whose digital sum is prime: ",
@words.hyper.grep({ !.contains(/<-alpha>/) && all(.comb».&from-base(26)).is-prime && .&from-base(26).is-prime && .comb».&from-base(26).sum.is-prime }).&{display +$_, $_, ''};</lang>
@words.hyper.grep({ !.contains(/<-alpha>/) && all(.comb».&from-base(26)).is-prime && .&from-base(26).is-prime && .comb».&from-base(26).sum.is-prime }).&{display +$_, $_, ''};</syntaxhighlight>
{{out}}
{{out}}
<pre>Number of words whose ords are all prime: 36;
<pre>Number of words whose ords are all prime: 36;
Line 1,110: Line 1,110:
=={{header|REXX}}==
=={{header|REXX}}==
No attempt was made to exclude any "word" if it contained any non-letter (Latin alphabet) characters.
No attempt was made to exclude any "word" if it contained any non-letter (Latin alphabet) characters.
<lang rexx>/*REXX program finds words whose ASCII code for its letters are prime (in a dictionary).*/
<syntaxhighlight lang="rexx">/*REXX program finds words whose ASCII code for its letters are prime (in a dictionary).*/
parse arg iFID . /*obtain optional arguments from the CL*/
parse arg iFID . /*obtain optional arguments from the CL*/
if iFID=='' | iFID=="," then iFID='unixdict.txt' /*Not specified? Then use the default.*/
if iFID=='' | iFID=="," then iFID='unixdict.txt' /*Not specified? Then use the default.*/
Line 1,136: Line 1,136:
67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 ,
67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 ,
157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251
157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251
@.= 0; do j=1 for words(p); _= word(p, j); @._= 1; end /*j*/; return</lang>
@.= 0; do j=1 for words(p); _= word(p, j); @._= 1; end /*j*/; return</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>
Line 1,184: Line 1,184:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
load "stdlib.ring"
load "stdlib.ring"


Line 1,209: Line 1,209:
see "Prime words are:" + nl
see "Prime words are:" + nl
see Words
see Words
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 1,252: Line 1,252:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>require 'prime'
<syntaxhighlight lang="ruby">require 'prime'


puts File.open("unixdict.txt").select{|line| line.chomp.chars.all?{|c| c.ord.prime?} }
puts File.open("unixdict.txt").select{|line| line.chomp.chars.all?{|c| c.ord.prime?} }
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>a
<pre>a
Line 1,296: Line 1,296:


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>// [dependencies]
<syntaxhighlight lang="rust">// [dependencies]
// primal = "0.3"
// primal = "0.3"


Line 1,324: Line 1,324:
Err(error) => eprintln!("{}", error),
Err(error) => eprintln!("{}", error),
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,340: Line 1,340:


=={{header|Swift}}==
=={{header|Swift}}==
<lang swift>import Foundation
<syntaxhighlight lang="swift">import Foundation


class BitArray {
class BitArray {
Line 1,409: Line 1,409:
} catch {
} catch {
print(error.localizedDescription)
print(error.localizedDescription)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,454: Line 1,454:
{{libheader|Wren-math}}
{{libheader|Wren-math}}
{{libheader|Wren-trait}}
{{libheader|Wren-trait}}
<lang ecmascript>import "io" for File
<syntaxhighlight lang="ecmascript">import "io" for File
import "/math" for Int
import "/math" for Int
import "/trait" for Stepped
import "/trait" for Stepped
Line 1,468: Line 1,468:
for (word in words) {
for (word in words) {
if (word.all { |c| primeChars.contains(c) }) System.print(word)
if (word.all { |c| primeChars.contains(c) }) System.print(word)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,512: Line 1,512:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>func IsPrime(N); \Return 'true' if N is prime
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is prime
int N, I;
int N, I;
[if N <= 2 then return N = 2;
[if N <= 2 then return N = 2;
Line 1,544: Line 1,544:
];
];
until Ch = EOF;
until Ch = EOF;
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}