28
\$\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\$
15
  • 15
    \$\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\$ Jan 24 '17 at 4:59
  • 2
    \$\begingroup\$ What's a "Unibyte value" ?!??? \$\endgroup\$ 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

38 Answers 38

1
2
1
\$\begingroup\$

Jelly, 4 bytes, Σ = 239

ÆPa1

Try it online!

Proof: Try it online! (take the 1-based indices of the program's chars of Jelly's code page, decrement to make them 0-based, sum and then check if the result is prime).

\$\endgroup\$
1
\$\begingroup\$

Help, WarDoq!, 2 bytes, ∑ 127

P/

Outputs 1 if it is a prime (positive only) and 0 if it is not a prime.

Try it online! Note: Due to technical limitations, this interpreter takes the first line as the code and the second line as STDIN.

1 is not prime in this.

The code's codepoints are 80 47, which sums to 127.

\$\endgroup\$
1
\$\begingroup\$

GolfScript, 14 bytes

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

Try it online!

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

\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 42 bytes, 2687

INPUT N:FOR D=2TO N/2P=P+!(N MOD D)NEXT?!P

Outputs 1 (true) if the number is prime, otherwise 0 (false).

The variable names were not just chosen to make the program prime. N is the number to test, D is the divisor, and P keeps track of whether N is prime.

\$\endgroup\$
0
\$\begingroup\$

Wonder, 7 bytes, 537

pr rl e

There probably is a better way...

\$\endgroup\$
0
\$\begingroup\$

Rust, 190 bytes, 15013 score

fn main(){let A=&mut"".into();std::io::stdin().read_line(A);let(B,mut C)=(A.trim().parse::<u64>().unwrap(),true);for H in 2..((B as f64).sqrt()+1.0) as u64{if B%H==0{C=false}}print!("{}",C)}

Ungolfed

fn main() {
    let input = &mut "".into();
    std::io::stdin().read_line(input);
    let (number, mut prime) = (input.trim().parse::<u64>().unwrap(), true);

    for x in 2..((number as f64).sqrt() + 1.0) as u64 {
        if number % x == 0 {
            prime = false;
        }
    }

    print!("{}", prime);
}

Does not work for 1

\$\endgroup\$
0
\$\begingroup\$

Stax, 349

|pq

Run and debug it

\$\endgroup\$
2
  • \$\begingroup\$ |pQ works for a score of 317. If swapping truthy/falsy is allowed, |p! works as well for 269. \$\endgroup\$ Apr 2 '18 at 19:02
  • \$\begingroup\$ Thank you. I don't think the challenge require that the score be minimized so I didn't tweak that part. \$\endgroup\$ Apr 2 '18 at 19:12
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!

\$\endgroup\$
1
  • \$\begingroup\$ This challenge does not allow function solutions. \$\endgroup\$ Apr 3 '18 at 0:54
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.