23
2
Write a program that will test the primality of a specified number, and give the output as a Boolean value (True is prime). Your prime test can (but doesn't have to) be valid for the number 1.
Here's the catch: your program itself has to sum to a prime number. Convert every character (including spaces) to its Unicode/ASCII value (table). Then, add all those numbers together to get the sum of your program.
For example, take this not-so-great program I wrote in Python 3.3:
q=None
y=int(input())
for x in range(2,int(y**0.5)+1):
if y%x==0:
q=False
if not q:
q=True
print(q)
If you convert all the characters to their corresponding Unicode/ASCII value, you get:
113 61 78 111 110 101 10 121 61 105 110 116 40 105 110 112 117 116 40 41 41 10 102 111 114 32 120 32 105 110 32 114 97 110 103 101 40 50 44 105 110 116 40 121 42 42 48 46 53 41 43 49 41 58 10 32 32 32 32 105 102 32 121 37 120 61 61 48 58 10 32 32 32 32 32 32 32 32 113 61 70 97 108 115 101 10 105 102 32 110 111 116 32 113 58 10 32 32 32 32 113 61 84 114 117 101 10 112 114 105 110 116 40 113 41
You can then find the sum of those numbers manually or with your own program. This specific program sums to 8293, which is a prime number.
Of course, this is Code Golf, so the smaller you can make your program, the better. As pointed out by other users, this program is not very golfy.
A few rules:
Valid inputs include STDIN and prompts (no functions, it's just a way to add free extra code). Spaces are permitted, but only if they are crucial to the functionality of your program. Output must be an output, not just stored in a variable or returned (use print, STDOUT, etc.)
Flags can be used and should be counted literally, not expanded. Comments are not allowed. As for non-ASCII characters, they should be assigned to the value in their respective encoding.
Make sure to list your program's size and the sum of the program. I will test to make sure programs are valid.
Good luck!
Here is a snippet to count the sum of your program and check if it is prime:
function isPrime(number) { var start = 2; while (start <= Math.sqrt(number)) { if (number % start++ < 1) return false; } return number > 1; } var inp = document.getElementById('number'); var text = document.getElementById('input'); var out = document.getElementById('output'); function onInpChange() { var msg; var val = +inp.value; if (isNaN(val)) { msg = inp.value.toSource().slice(12, -2) + ' is not a valid number.'; } else if (isPrime(val)) { msg = val + ' is a prime number!'; } else { msg = val + ' is not a prime number.'; } out.innerText = msg; } function onTextChange() { var val = text.value; var total = new Array(val.length).fill().map(function(_, i) { return val.charCodeAt(i); }).reduce(function(a, b) { return a + b; }, 0); inp.value = '' + total; onInpChange(); } text.onkeydown = text.onkeyup = onTextChange; inp.onkeydown = inp.onkeyup = onInpChange;
body { background: #fffddb; } textarea, input, div { border: 5px solid white; -webkit-box-shadow: inset 0 0 8px rgba(0,0,0,0.1), 0 0 16px rgba(0,0,0,0.1); -moz-box-shadow: inset 0 0 8px rgba(0,0,0,0.1), 0 0 16px rgba(0,0,0,0.1); box-shadow: inset 0 0 8px rgba(0,0,0,0.1), 0 0 16px rgba(0,0,0,0.1); padding: 15px; background: rgba(255,255,255,0.5); margin: 0 0 10px 0; font-family: 'Roboto Mono', monospace; font-size: 0.9em; width: 75%; }</style><meta charset="utf-8"><style>/**/
<link href="https://fonts.googleapis.com/css?family=Roboto+Mono" rel="stylesheet"><textarea id="input" tabindex="0">Insert your (UTF-8 encoded) code here
Or directly input a number below</textarea><br><input id="number" value="6296" tabindex="1"><br><div id="output">6296 is not a prime number.</div>
12In non-golfing languages, it looks like you could just take the shortest prime-deciding code, and tweak variable names until the sum is prime. – xnor – 2017-01-24T04:52:20.860
5Why the restrictions on I/O? – Jonathan Allan – 2017-01-24T04:59:25.040
@JonathanAllan To prevent excessive and useless code. For example, if function arguments were allowed, you could choose a name for your function that would perfectly match the number you wanted. – Noah L – 2017-01-24T05:01:13.763
2What's a "Unibyte value" ?!??? – aditsu quit because SE is EVIL – 2017-01-24T11:38:06.197
Is our code allowed to exit with an error? – mbomb007 – 2017-01-24T23:03:17.280
1"Flags can be used and should be counted literally, not expanded." What does this mean? If my program runs as
perl -ne 'your code here'
, do I count then
which is the edit distance, or the-n
? – Value Ink – 2017-01-24T23:19:12.787Are built-ins allowed? – mbomb007 – 2017-01-25T01:30:32.243
@mbomb007 Provided the built-ins come with the software by default (no custom-made built ins) – Noah L – 2017-01-25T01:41:56.430
1Do the I/O restrictions still allow full programs that take an argument? – Jonathan Allan – 2017-01-25T04:34:14.160
5You talk about characters and code pages. A Unicode character has always the same code point, no matter which encoding is used to represent it. As for non-ASCII characters, they should be assigned to the value in their respective encoding. makes me think you actually want the sum of the byte values to be prime – Dennis – 2017-01-25T20:28:44.657
3Test Your code online – Titus – 2017-01-25T21:54:55.583
@Titus It looks like your script is thrown off by the sequence
– Laikoni – 2018-04-02T10:27:44.507>0
in the input: Try it online!@Laikoni It´s not
– Titus – 2018-04-02T11:12:57.883>0
, it´s0
. Here is a fixed version: Verify primarity of your code.Is it a lowest score or a shortest code? – l4m2 – 2018-04-02T15:18:00.743
@l4m2 The tag is
code-golf
, so it should be shortest code. (Or equivalently, the score is the byte length.) Challenges where the score is something special are usually tagged ascode-challenge
. – Ørjan Johansen – 2018-04-02T17:58:28.333