26
\$\begingroup\$

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>

\$\endgroup\$
  • 14
    \$\begingroup\$ In non-golfing languages, it looks like you could just take the shortest prime-deciding code, and tweak variable names until the sum is prime. \$\endgroup\$ – xnor Jan 24 '17 at 4:52
  • 5
    \$\begingroup\$ Why the restrictions on I/O? \$\endgroup\$ – Jonathan Allan Jan 24 '17 at 4:59
  • 2
    \$\begingroup\$ What's a "Unibyte value" ?!??? \$\endgroup\$ – aditsu quit because SE is EVIL Jan 24 '17 at 11:38
  • 6
    \$\begingroup\$ You 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 \$\endgroup\$ – Dennis Jan 25 '17 at 20:28
  • 4
    \$\begingroup\$ Test Your code online \$\endgroup\$ – Titus Jan 25 '17 at 21:54

35 Answers 35

1
2
0
\$\begingroup\$

Whispers v2, 33 bytes

>>> ⊤ℙ∘ℕ
> Input
>> 1∘2

Try it online!

  1. Score: 44381
  2. Only 6 bytes/2 characters added to make it valid!
  3. 1 is not prime

How it works

This is shown in the order it is executed in:

		; Line 3:
>>  ∘		; Compose...
   1            ; Line 1 with
     2          ; The result of line 2

		; Line 2:
> Input		; Retrieve a line of input

		; Line 1:
>>> ⊤		; The input is...
     ℙ		; Prime
      ∘		; And it is...
       ℕ	; Natural
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

JavaScript (Node.js), 33 bytes (sum 2237)

p=(n,i=1)=>++i*i>n||n%i&&p(n,i,1)

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ This challenge does not allow function solutions. \$\endgroup\$ – Ørjan Johansen Apr 3 '18 at 0:54
0
\$\begingroup\$

TI-Basic, 27 bytes, byte-sum 2843

Prompt F
3
For(A,2,sqrt(F
If not(remainder(F,A
0
End
Disp Ans

Prints 3 for prime and 0 for composite. Does not work for 1 (prints 3).

TI-Basic is a tokenized lanugage. remainder( is a two-byte token; all others are one-byte tokens.

Explanation:

Prompt F             # 0xdd, 0x46, 0x3f # Store input in F
3                    # 0x33, 0x3f       # Store 3 in Ans
For(A,2,sqrt(F       # 0xd3, 0x41, 0x2b, 0x32, 0x2b, 0xbc, 0x46, 0x3f
                        # For each integer A from 2 to sqrt(F)
If not(remainder(F,A # 0xce, 0xb8, (0xef, 0x32), 0x46, 0x2b, 0x41, 0x3f
0                    # 0x30, 0x3f
                        # If F is divisible by A, store 0 in Ans
End                  # 0xd4, 0x3f # end For loop
Disp Ans             # 0xde, 0x72 (no newline) # Print Ans

Sum testing with Python:

>>> sum(eval(i) for i in ['0xdd', '0x46', '0x3f', '0x33', '0x3f', '0xd3', '0x41', '0x2b', '0x32', '0x2b', '0xbc', '0x46', '0x3f', '0xce', '0xb8', '0xef', '0x32', '0x46', '0x2b', '0x41', '0x3f', '0x30', '0x3f', '0xd4', '0x3f', '0xde', '0x72'])
2843
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

GolfScript, 14 bytes

~.,{*.!+}*.*\%

Try it online!

Same solution for the question "Is this number a prime?", the program sums to 887.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

APL (Dyalog Unicode), 7 bytes

Sum: 569

1 pco⌈⎕

Try it online! or Verify

The program uses the ceiling function() since it's a no-op on integers, and pads the codepoint sum to a prime number, according to APL's code page.

pco is a function from the dfns library which checks for primality.

| improve this answer | |
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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