Prime words

From Rosetta Code
Revision as of 16:31, 4 December 2020 by Thebigh (talk | contribs) (add freebasic)
Prime words is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

A word is a   prime word   if all its individual letters   (expressed as an ASCII decimal code)   are primes.


        A   ASCII decimal code is:   65 
        B   ASCII decimal code is:   66 
        C   ASCII decimal code is:   67 
                 
        X   ASCII decimal code is:   88 
        Y   ASCII decimal code is:   89 
        Z   ASCII decimal code is:   90 
                 
        a   ASCII decimal code is:   97 
        b   ASCII decimal code is:   98 
        c   ASCII decimal code is:   99 
                 
        x   ASCII decimal code is:   120 
        y   ASCII decimal code is:   121 
        z   ASCII decimal code is:   122 
Task

Show here on this page every   prime word   in unixdict.txt.



Factor

Works with: Factor version 0.99 2020-08-14

<lang factor>USING: io.encodings.ascii io.files math.primes prettyprint sequences ;

"unixdict.txt" ascii file-lines [ [ prime? ] all? ] filter .</lang>

Output:
{
    "a"
    "aaa"
    "age"
    "agee"
    "ak"
    "am"
    "ama"
    "e"
    "egg"
    "eke"
    "em"
    "emma"
    "g"
    "ga"
    "gag"
    "gage"
    "gam"
    "game"
    "gamma"
    "ge"
    "gee"
    "gem"
    "gemma"
    "gm"
    "k"
    "keg"
    "m"
    "ma"
    "mae"
    "magma"
    "make"
    "mamma"
    "me"
    "meek"
    "meg"
    "q"
}

FreeBASIC

<lang freebasic> dim shared as boolean prime(0 to 29) =_

   {false, true, false, true, true, false, false, true, false, true, false, false, true, false,_
    false, false, true, false, true, true, false, true, true, false, true, false, false, false, false}
    

function isprimeletter( s as string ) as boolean

   dim as ubyte n = asc(s)
   if n mod 2 = 0 then return false
   return prime( (n-65)/2 )

end function

function isprimeword( s as string ) as boolean

   for i as uinteger = 1 to len(s)
       if not isprimeletter( mid(s,i,1) ) then return false
   next i
   return true

end function

dim as string word

open "unixdict.txt" for input as #1 while true

   line input #1, word
   if word = "" then exit while
   if isprimeword( word ) then print word

wend close #1 end </lang>

Output:
a
aaa
age
agee
ak
am
ama
e
egg
eke
em
emma
g
ga
gag
gage
gam
game
gamma
ge
gee
gem
gemma
gm
k
keg
m
ma
mae
magma
make
mamma
me
meek
meg
q

REXX

No attempt was made to exclude any "word" if it contained any non-letter (Latin alphabet) characters. <lang rexx>/*REXX pg finds words whose ASCII code for its letters (within an identified dictionary)*/ parse arg iFID . /*obtain optional arguments from the CL*/ if iFID== | iFID=="," then iFID='unixdict.txt' /*Not specified? Then use the default.*/ call genPrimes /*generate all primes less than 256. */ say 'reading the dictionary file: ' iFID /*show which dictionary is being read. */ say

  1. = 0 /*count of prime words found (so far).*/
       do recs=0  while lines(iFID)\==0         /*read each word in the file  (word=X).*/
       x= strip( linein( iFID) )                /*pick off a word from the input line. */
                do j=1  for length(x)           /*examine each letter (char) in word.  */
                _= c2d( substr(x, j, 1) )       /*convert each letter to a decimal num.*/
                if \@._  then iterate recs      /*is this ASCII code letter a prime ?  */
                end   /*j*/
       say x                                    /*display a prime word to the terminal.*/
       #= # + 1                                 /*bump the count of  prime words.      */
       end      /*recs*/                        /* [↑]   semaphore name is uppercased. */

say say copies('─', 30) recs "usable words in the dictionary file: " iFID say 'found ' # " prime words in the dictionary." exit 0 /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ genPrimes: p= 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 ,

              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
          @.= 0;    do j=1  for words(p);  _= word(p, j);  @._= 1;  end  /*j*/;    return</lang>
output   when using the default input:
reading the dictionary file:  unixdict.txt

a
aaa
age
agee
ak
am
ama
e
egg
eke
em
emma
g
ga
gag
gage
gam
game
gamma
ge
gee
gem
gemma
gm
k
keg
m
ma
mae
magma
make
mamma
me
meek
meg
q

────────────────────────────── 25104 usable words in the dictionary file:  unixdict.txt
found  36  prime words in the dictionary.

Ring

<lang ring> load "stdlib.ring"

cStr = read("unixdict.txt") wordList = str2list(cStr) Words = []

for n = 1 to len(wordList)

   num = 0 
   len = len(wordList[n])
   for m = 1 to len
       asc = ascii(wordList[n][m])
       if isprime(asc)
          num = num + 1
       else
          exit
       ok
   next
   if num = len
      add(Words,wordList[n])
   ok

next

see "Prime words are:" + nl see Words </lang> Output:

Prime words are:
a
aaa
age
agee
ak
am
ama
e
egg
eke
em
emma
g
ga
gag
gage
gam
game
gamma
ge
gee
gem
gemma
gm
k
keg
m
ma
mae
magma
make
mamma
me
meek
meg
q

Wren

Library: Wren-math
Library: Wren-trait

<lang ecmascript>import "io" for File import "/math" for Int import "/trait" for Stepped

// cache prime characters with codepoints between 33 and 255 say var primeChars = [] for (i in Stepped.new(33..255, 2)) {

   if (Int.isPrime(i)) primeChars.add(String.fromCodePoint(i))

} var wordList = "unixdict.txt" // local copy var words = File.read(wordList).trimEnd().split("\n") System.print("Prime words in %(wordList) are:") for (word in words) {

   if (word.all { |c| primeChars.contains(c) }) System.print(word)

}</lang>

Output:
Prime words in unixdict.txt are:
a
aaa
age
agee
ak
am
ama
e
egg
eke
em
emma
g
ga
gag
gage
gam
game
gamma
ge
gee
gem
gemma
gm
k
keg
m
ma
mae
magma
make
mamma
me
meek
meg
q