A Primenary (binary-prime) string is one which, when written as a binary grid, every row and column has a prime total.
That's quite a vague explanation, so let's break it down with a worked example...
For this example we'll use the string bunny
:
First, find the ASCII code point of each character and its binary representation:
Char | ASCII | Binary
b 98 1100010
u 117 1110101
n 110 1101110
n 110 1101110
y 121 1111001
Take these binary values, from top to bottom, and arrange them into grid (adding leading zeros if necessary):
1 1 0 0 0 1 0
1 1 1 0 1 0 1
1 1 0 1 1 1 0
1 1 0 1 1 1 0
1 1 1 1 0 0 1
Then, count the number of 1
s in each row and column:
1 1 0 0 0 1 0 > 3
1 1 1 0 1 0 1 > 5
1 1 0 1 1 1 0 > 5
1 1 0 1 1 1 0 > 5
1 1 1 1 0 0 1 > 5
v v v v v v v
5 5 2 3 3 3 2
If, and only if, every single total is prime (such as here) then the string is a valid binary-prime.
The Challenge
Your task is to create a function or program which, when given a string, returns/outputs truthy
if the string is primenary, and falsy
otherwise.
Rules/Details
- You may assume that the string's characters will always be in the ASCII range
33-126
(inclusive). - The string will not be empty.
- A primenary string does not have to have a prime length - for example,
W1n*
is valid, despite having 4 characters. - This is code-golf, so the shortest answer (in bytes) wins - but all submissions are welcome.
- Standard loopholes are banned.
Test Cases
'husband' -> True
'HOTJava' -> True
'COmPaTIBILE' -> True
'AuT0HACk' -> True
'PPCW' -> False
'code-golf' -> False
'C++' -> False
'/kD' -> False
'HI' -> False
'A' -> False
There is also a working, but incredibly verbose Python example on repl.it that you can test your solution against.
husband
was valid? Or any of them? Great problem, though! \$\endgroup\$False
, correct? \$\endgroup\$0
and1
are not prime, and every 1-2 char input string containing only chars in the given range is guaranteed to contain at least one0
or1
as a vertical sum. You should add some 1 and 2 character strings as test cases. \$\endgroup\$false
. 2 char inputs could, but not in the ASCII range we're using, so for this scenario you're correct. \$\endgroup\$