6
\$\begingroup\$

This question already has an answer here:

We define a prime character as a character that has a prime ASCII value. We define a prime string as a String that only contains prime characters.

Given a String that only contains printable ASCII, determine whether that String is prime.

Input / Output

You can take input either as a String or as a list of individual characters. You have to output two different and consistent values depending on whether the String is prime, following the standard rules for a . You are guaranteed that the input will be non-empty.

Test Cases

Truthy cases (Prime Strings):

"aeg"
"aIS5"
"aaaaaaaaaaaaaa"
"/S/S"
"=;+"

Falsy cases (not Prime Strings):

"maTHS"
"Physics"
"\\\\"
"PPCG"
"Code Golf"
"Prime"

This is , so the shortest code in bytes wins! Standard rules for this tag apply.

\$\endgroup\$

marked as duplicate by Mr. Xcoder, Community Jul 17 '17 at 18:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ It's not very unique, though I +1'd it. \$\endgroup\$ – Magic Octopus Urn Jul 17 '17 at 17:48
  • 2
    \$\begingroup\$ @Mr.Xcoder I downvoted (and I personally would say dupe) of generic prime checking with some boilerplate (.unicodePoints.every(isPrime)) the thing is none of these boilerplate functions would even likely have the capability to be golfed as they are isPrime surrounded by language built ins. I don't want to smash as I would hammer it and would also like to get consensus \$\endgroup\$ – Downgoat Jul 17 '17 at 17:58
  • 1
    \$\begingroup\$ The definition of a prime string as simply not containing any non-prime characters seems less interesting than it could be, and a poor analogy with digits of numbers. For example, in regular decimal numbers, 33 is non-prime even though all its digits are prime. \$\endgroup\$ – Peter Cordes Jul 17 '17 at 18:02

15 Answers 15

5
\$\begingroup\$

05AB1E, 3 bytes

ÇpW

Try it online!


Ç   # To ASCII array.
 p  # Is prime?
  W # Min value from.

ÇpP also works.

\$\endgroup\$
  • \$\begingroup\$ You beat me by a few seconds. I had ÇpP. \$\endgroup\$ – Riley Jul 17 '17 at 15:10
  • \$\begingroup\$ @Riley I'm surprised I was beaten by a Jelly answer, I answered hella quick, all I had to do was grab the ASCII command heh. \$\endgroup\$ – Magic Octopus Urn Jul 17 '17 at 15:11
5
\$\begingroup\$

Jelly, 4 bytes

OÆPẠ

Try it online!

Explanation

Remember that a string is an array of characters, so operations like O will apply to each individual character.

OÆPẠ
O        Ord; cast each character to a number
 ÆP      Is it ÆPrime? gives 1 for truthy and 0 for falsey
   Ạ     Ạll; returns 1 if the array contains no falsey values
\$\endgroup\$
4
\$\begingroup\$

Python 3, 56 bytes

lambda s:all(x%i for x in map(ord,s)for i in range(2,x))

Try it online!

\$\endgroup\$
3
\$\begingroup\$

MATL, 3 bytes

ZpA

Outputs 1 for prime string, 0 otherwise.

Try it online!

Explanation

     % Implicit input
Zp   % Isprime. Applies element-wise to the code point of each entry
A    % All. Gives 1 iff all results are non-zero.
     % Implicit display
\$\endgroup\$
2
\$\begingroup\$

Brachylog, 3 bytes

ạṗᵐ

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Haskell, 32 bytes

all(`elem`"%)+/5;=CGIOSYaegkmq")

Try it online!

\$\endgroup\$
1
\$\begingroup\$

CJam, 8 bytes

q:i:mp:&

Try it online!

Explanation

q         e# Read input
 :i       e# Convert to code points
   :mp    e# Check if each is prime
      :&  e# Reduce by AND
\$\endgroup\$
1
\$\begingroup\$

Perl, 46 bytes

45 bytes code + 1 for -p.

$_=y///c==grep!/^(11+?)\1+$/,map"1"x ord,/./g

Try it online! (-l added to run all tests at once)

\$\endgroup\$
1
\$\begingroup\$

R, 42 bytes

all(pracma::isprime(utf8ToInt(scan(,''))))

Reads from stdin, requires the pracma (practical math) package to be installed. Checks if all the UTF-8 encoding of the characters are prime. Unfortunately, this won't work on TIO...

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Mathematica, 30 bytes

And@@PrimeQ@ToCharacterCode@#&

Quite self-explanatory.

\$\endgroup\$
1
\$\begingroup\$

QBIC, 28 bytes

[_l;||q=q*µasc(_sA,a,1)]?q=0

This prints -1 for non-prime strings, and 0 for prime ones. That is opposite to QBasic's usual approach to true/false, but this is the shortest way t comply with the 'consistent values' criterium. Output would otherwise be 0 or 1/-1.

Explanation

[    |          FOR a = 1 to
 _l |             the length of
   ;              A$ (read from the cmd line)
q=q*            Multiply q (which starts as 1) by
µ                -1 if the following is prime, 0 otherwise:
 asc(       )   take the ASCII value of
     _sA,a,1      a substring of A$, starting at pos a, for 1 char
]               NEXT
?q=0            If we've tested all chas in the string, print -1 if q=0 
                and we've found a non-prime, or a 0 for prime strings
\$\endgroup\$
1
\$\begingroup\$

C, 113 bytes

p(int c){for(int i=1;++i<c;)if(!(c%i))return 1;return 0;}c(char*s){char*a="p";while(*s++)if(p(*s))++a;printf(a);}
\$\endgroup\$
  • \$\begingroup\$ You can do for(int i=1;++i<c;) for your first for-loop to save 1 byte. \$\endgroup\$ – Cows quack Jul 17 '17 at 15:50
  • 1
    \$\begingroup\$ You can always omit int return type and argument type (if there's no other types) in C \$\endgroup\$ – Keyu Gan Jul 17 '17 at 16:48
  • \$\begingroup\$ I reduced the size from 137 to 113 bytes \$\endgroup\$ – xanoetux Jul 18 '17 at 3:25
1
\$\begingroup\$

APL (Dyalog), 21 bytes

⎕CY'dfns'
∧/1pco⎕UCS⎕

Try it online!

Explanation

⎕CY'dfns' This allows us to use pco
⎕UCS⎕     Convert the characters in the input to its respective code points
1pco      pco with 1 as the left argument gives 1 iff the right argument is prime
          This is applied across the array of code points
∧/        Reduce by AND; return 1 if all elements are truthy, otherwise give false
\$\endgroup\$
0
\$\begingroup\$

Mathematica, 36 bytes

FreeQ[PrimeQ@ToCharacterCode@#,1<0]&

Try it online!

-2 bytes from @JungHwanMin

\$\endgroup\$
  • \$\begingroup\$ -2 bytes: FreeQ[PrimeQ@ToCharacterCode@#,1<0]& \$\endgroup\$ – JungHwan Min Jul 17 '17 at 16:36
0
\$\begingroup\$

C# (.NET Core), 60 bytes

s=>s.All(i=>Enumerable.Range(2,(int)i-2).All(j=>((int)i)%j))
\$\endgroup\$
  • \$\begingroup\$ You need to include using System.Linq; into your byte count here. \$\endgroup\$ – TheLethalCoder Jul 19 '17 at 12:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.