# Check If my String is Prime [duplicate]

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.

• It's not very unique, though I +1'd it. Commented Jul 17, 2017 at 17:48
• @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 Commented Jul 17, 2017 at 17:58
• 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. Commented Jul 17, 2017 at 18:02

# 05AB1E, 3 bytes

ÇpW


Try it online!

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


ÇpP also works.

• You beat me by a few seconds. I had ÇpP. Commented Jul 17, 2017 at 15:10
• @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. Commented Jul 17, 2017 at 15:11

# 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


# Python 3, 56 bytes

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


Try it online!

# 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


# Brachylog, 3 bytes

ạṗᵐ


Try it online!

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


Try it online!

# 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


## 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)

# 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!

# Mathematica, 30 bytes

And@@PrimeQ@ToCharacterCode@#&


Quite self-explanatory.

# 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


# 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);}

• You can do for(int i=1;++i<c;) for your first for-loop to save 1 byte. Commented Jul 17, 2017 at 15:50
• You can always omit int return type and argument type (if there's no other types) in C Commented Jul 17, 2017 at 16:48
• I reduced the size from 137 to 113 bytes Commented Jul 18, 2017 at 3:25

# 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


# Mathematica, 36 bytes

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


Try it online!

-2 bytes from @JungHwanMin

• -2 bytes: FreeQ[PrimeQ@ToCharacterCode@#,1<0]& Commented Jul 17, 2017 at 16:36

# C# (.NET Core), 60 bytes

s=>s.All(i=>Enumerable.Range(2,(int)i-2).All(j=>((int)i)%j))

• You need to include using System.Linq; into your byte count here. Commented Jul 19, 2017 at 12:44