27
\$\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
  • 18
    \$\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
    Commented Jan 24, 2017 at 4:52
  • 5
    \$\begingroup\$ Why the restrictions on I/O? \$\endgroup\$ Commented Jan 24, 2017 at 4:59
  • 2
    \$\begingroup\$ What's a "Unibyte value" ?!??? \$\endgroup\$ Commented Jan 24, 2017 at 11:38
  • 7
    \$\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
    Commented Jan 25, 2017 at 20:28
  • 5
    \$\begingroup\$ Test Your code online \$\endgroup\$
    – Titus
    Commented Jan 25, 2017 at 21:54

43 Answers 43

1
2
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\$
1
\$\begingroup\$

Pyt, 2 bytes, byte sum 149

ṗ⁸

Try it online!

Uses Pyt's custom encoding, in which the bytes are as follows:

8d 08

Explanation:

ṗ         checks for primality (returns True/False)
⁸         raises top of stack to the 8th power (coerces True/False to 1/0); implicit print
\$\endgroup\$
1
\$\begingroup\$

Java 8, 114 105 bytes, Prime 9209

interface M{static void main(String[]a){long n=new Long(a[0]),B=n;for(;n%--B>0;);System.out.print(B<2);}}

Assumes \$input\geq2\$ (will give a division-by-zero error for \$input=1\$), since the rule mention "Your prime test can (but doesn't have to) be valid for the number \$1\$.".

Try it online.

Explanation:

interface M{                   // Class
  static void main(String[]a){ //  Mandatory main-method
    long n=new Long(a[0]),     //   The first argument as number
    B=n;for(;n%--B>0;);        //   Check if `n` is a prime
    System.out.print(B<2);}}   //   Print whether `n` was a prime
                               //   (if `B` is 1 after the loop, it means it wasn't
                               //   divisible by any other integer in the range [2,n),
                               //   and therefore is a prime)

I've used B instead of i as variable name to make the unicode sum a prime. Verify the unicode sum here.

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

Japt, 2 bytes, 191

Well, that worked out nicely! :)

Uj

Try it

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

Thunno, 3 bytes (UTF-8), sum = 293

Attempt This Online!

Verify that 293 is prime

Basically, the Thunno interpreter ignores any characters which are not in the printable ASCII set (32-127). As × has the codepoint 215, it is ignored and thus the program becomes N, which is the prime check program.

Thunno, \$ 4 \log_{256}(96) \approx \$ 3.29 bytes, sum = 269

ND0K

Attempt This Online!

Verify that 269 is prime

Explanation:

ND0K  # Implicit input
N     # Prime check (this is all we need, but unfortunately the ordinal of N is 78, which isn't prime)
 D    # Duplicate (completely unnecessary, but needed to make the sum prime)
  0   # Push a 0
   K  # And pop the 0 (yep, that cancels out)
      # Implicit output
\$\endgroup\$
1
\$\begingroup\$

vemf, 9 bytes, sum 911

{α%├2↕α&☻

Prints 1 if \$\alpha\$ is prime and 0 if it is not. Somehow does work on 1, because an empty list is considered truthy.

In the vemf codepage this is represented as

7b e0 25 c3 32 12 e0 26 02

which sums to 0x38f = decimal 911.

Explanation

{α%├2↕α&☻
{         ' define function
 α%    &  ' are all of the remainders nonzero
   ├2↕α   ' between 2 and α-1?
        ☻ ' reprints, just to make the program's sum a prime number
\$\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\$ Commented Apr 2, 2018 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\$ Commented Apr 2, 2018 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\$ Commented Apr 3, 2018 at 0:54
0
\$\begingroup\$

Python, 78 Bytes, 6067

x=lambda a:a and a*x(a-1)or 1
y=lambda b:x(b-1)%b==b-1
print(y(int(input(3))))

The 3 is kinda redundant, just there to make the code sum prime. The first lambda calculates the factorial of a number and the second lambda checks whether a number is prime using Wilson's Theorem.

\$\endgroup\$
7
  • \$\begingroup\$ The True if ... else False is redundant, since it will return a Boolean anyway \$\endgroup\$
    – The Thonnu
    Commented Apr 5, 2023 at 17:28
  • 1
    \$\begingroup\$ You need to delete the if \$\endgroup\$
    – The Thonnu
    Commented Apr 5, 2023 at 18:26
  • \$\begingroup\$ it would be shorter to change the variable names rather than add an extra 7 \$\endgroup\$
    – Jo King
    Commented Apr 6, 2023 at 3:03
  • \$\begingroup\$ @JoKing it just obfuscates the code in my opinion \$\endgroup\$ Commented Apr 6, 2023 at 7:50
  • 1
    \$\begingroup\$ Well, the goal of code-golf is to have the smallest amount of bytes, not the clearest code \$\endgroup\$
    – Jo King
    Commented Apr 6, 2023 at 12:10
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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