27
1
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.
Can I ask how you discovered that
husband
was valid? Or any of them? Great problem, though! – Gabriel Benamy – 2016-11-06T21:55:38.6903
@GabrielBenamy I'm glad someone asked! I looped through an online dictionary file, trying a few random capitalizations of each letter, sometimes switching letters for numbers, etc. Then I had a look through the outputted list and picked a couple of test cases I liked
– FlipTack – 2016-11-06T22:00:10.920Every 1-2 character input is guaranteed to return
False
, correct? – mbomb007 – 2016-11-08T21:21:05.143... because
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. – mbomb007 – 2016-11-08T21:35:06.857@mbomb007 1 char inputs cannot have prime numbers columnwise, so they will return
false
. 2 char inputs could, but not in the ASCII range we're using, so for this scenario you're correct. – FlipTack – 2016-11-08T21:36:46.357All of the test cases only have prime characters, i.e. rows in the bitmatrix, so they don't cover the case where only columns are checked for primality. Add
beezz
to fix this. There are two non-prime rows, but only prime columns inbeezz
. – movatica – 2019-06-26T19:31:06.640What is the intended output for
7/8
? – Nitrodon – 2019-06-26T20:07:54.210Of the answers I was able to test, six accept
7/8
and six reject7/8
. – Nitrodon – 2019-06-26T20:22:36.403The actual problem with
7/8
is leading zeroes. Should leading columns be ignored, if they are completely zeroed? Or should we always check 7 bits for each character? What about zero columns in the middle? – movatica – 2019-06-26T21:35:06.970