The task is simple: your program reads an integer as an input, and prints whether it is a prime or not. You can print "yes/no", "true/false" or anything what unambiguously identifies the result.

The challenge is, the code has to work with its rows and columns transposed.

To exclude the obvious solution (shortest "simple" solution repeated vertically char-by-char with the use of comments), the metric is a little bit different from the usual code-golf:

Because formatting is very important in this challenge, the code size is measured in the area of the smallest rectangle the code fits in. In other words, whitespace does count, and the lines should be filled to be of equal length (you don't have to actually do it when you post the solution, for simplicity's sake). For example

int main()   
    return 0;

would have a size of 4*13 = 52, (and obviously it does not fit either of the two criteria: prime detection and transposable.)

Smallest size wins.

You can use any language, and any library function except if the sole purpose of that function is to find, generate, or detect primes.


While the winner would probably be the Golfscript solution, I'll award a 50 point bounty for the best C or C++ solution!

  • \$\begingroup\$ Your rectangle metric fails to discourage the obvious solution -- better would be to take the longest sidelength. Though, this would reduce the GS answer to having score 4. \$\endgroup\$
    – boothby
    Oct 9, 2012 at 19:31
  • \$\begingroup\$ You are right. The next transposed problem should have a different metric and forbid symmetrical solutions. However I think even then there will be someone who circumvents the rules or at least finds a solution the QA was not expecting when composing the rules. \$\endgroup\$
    – vsz
    Oct 10, 2012 at 3:09

12 Answers 12


C, 2*70 2*60

Prints y for primes, nothing otherwise.
EDIT: Changed code to save 10 chars. Must be run without paramters (so m=1).


The method for handling the transposition is quite generic, and can be applied to any program.
It's based on converting this:


To this:


And both mean simply abcd.

  • \$\begingroup\$ wow, clever misuse of line concatenation :) \$\endgroup\$
    – vsz
    Oct 6, 2012 at 15:14
  • 2
    \$\begingroup\$ This is a golf/puzzles site. There is no such thing as misuse. \$\endgroup\$
    – boothby
    Oct 9, 2012 at 17:23
  • 2
    \$\begingroup\$ @boothby, I guess he means abuse. But I don't argue with compliments. \$\endgroup\$
    – ugoren
    Oct 9, 2012 at 20:04

C - 13x13

Reads the input from stdin and prints a 1 for prime and a 0 for not prime.

//O n{ adfau+
int b///&(-h)

edit: compiles with gcc and clang now, other compilers weren't tested


C, 12x12 chars

A two-dimensional solution, unlike my other answer, based on the same code (and like it, prints y for prime, nothing for composite).
The structure is similar to Quasimodo's answer, but my code is shorter, and I think my usage of comemnts is a bit more efficient, so I can fit 12x12.

puts("y"); }

GolfScript, 13 × 1


GolfScript strikes again!

Repeats the input if it is prime, otherwise prints the input concatenated with its smallest proper divisor. Yes, I know that's stretching the definition of "anything what unambiguously identifies the result", but doing anything fancier would cost a few extra characters. If you want nicer output, appending the three characters ;]! to the code yields 1 for primes and 0 for composite numbers.

The algorithm is really inefficient, just brute force trial division from 2 to n−1. Most GolfScript operators are only single characters, so this code works just as well transposed. Annoyingly, though, the assignment operator : doesn't allow whitespace between itself and its target, so I had to do this entirely without variables.

  • \$\begingroup\$ Wrt "anything fancier would cost a few extra characters" - you can get a GolfScript-style Boolean for only 2. \$\endgroup\$ Oct 6, 2012 at 15:22
  • \$\begingroup\$ @Peter: You mean something like my edit above, only without the !? Or did you have something fancier in mind? \$\endgroup\$ Oct 7, 2012 at 14:00
  • 1
    \$\begingroup\$ I was thinking ) before the , so that it always finds a divisor and = at the end. \$\endgroup\$ Oct 7, 2012 at 18:39
  • \$\begingroup\$ : followed by newline, assigns to the newline character - so it's not that whitespace is not allowed, it's just that the whitespace is what gets assigned to \$\endgroup\$
    – gnibbler
    Oct 10, 2012 at 2:08
  • \$\begingroup\$ @gnibbler: Technically, we're both right. : does not allow whitespace, or anything else, between itself and its target: whatever immediately follows it, whether whitespace or any other token (yes, even numbers, strings or code blocks), is what gets assigned to. However, whitespace is what the official documentation specifically warns about, and for good reason -- since in most other places, adding whitespace between tokens in GolfScript does nothing (normally, unless it's been assigned to...). \$\endgroup\$ Oct 10, 2012 at 12:44

Perl, 14 x 14

I think I'm getting the hang of this. Specify the number as a command line argument, outputs 0 or 1. Probably more room for improvement.

$   n=pop;$p|=
 !  ($n%$_)for
  2 ..$n/2;   
pf +          
|o m          
=r ~          


Abused comments for a symmetric, character inefficient solution.

{(~)any 0=mod[x;2+(!)x-2]}"I"$(0:)0

Takes input from STDIN, returns a boolean.

  • \$\begingroup\$ Noticed the sqrt in there. When looking for primes in code-golf, it's usually advantageous to (wastefully) divide all the way up to n rather than stopping at sqrt n. \$\endgroup\$
    – gnibbler
    Oct 10, 2012 at 2:11
  • \$\begingroup\$ Very true, thanks. I haven't had a chance to try and get a better score yet. \$\endgroup\$
    – skeevey
    Oct 10, 2012 at 2:16
  • \$\begingroup\$ You should include a score in your answer \$\endgroup\$
    – FlipTack
    Dec 28, 2017 at 9:32

05AB1E, 1x5 1x3 (5 3 bytes)

This isn't one big program; each line is a separated alternative program to tackle the prime check (without using the prime builtin).


-2 bytes thanks to Grimy.

Whitespaces in between lines are no-ops in 05AB1E, and since I only use 1-byte commands, this works fine after transposing.

Outputs 1/0 for truthy/falsey respectively.

Try the first one online or verify some more test cases for all of them (with eval builtin .V).
Transposed: Try the first one online.


Ñ    # Get a list of all divisors of the (implicit) input-integer
     # (which would be only 1 and the integer itself for primes)
 P   # Take the product of that list
  Q  # And check if it's equal to the (implicit) input-integer

Ò    # Get a list of all prime factors of the (implicit) input-integer 
 g   # Get the amount of those prime factors by taking the length of the list
  Θ  # Check if that's equal to 1 (so only itself is a prime factor)

Ò    # Get a list of all prime factors of the (implicit) input-integer including duplicates
 Q   # Check for each if it's equal to the (implicit) input-integer
     # (1 if truthy; 0 if falsey)
  P  # Take the product of those checks (resulting in either 1 or 0 as well)

Õ    # Get the Euler totient of the (implicit) input-integer
 α   # Take the absolute difference with the (implicit) input-integer
  Θ  # Check if that's equal to 1

f    # Get a list of all prime factors of the (implicit) input-integer without duplicates
 Q   # Check for each if it's equal to the (implicit) input-integer
  O  # And take the sum of that (resulting in either 1 or 0)

f    # Get a list of all prime factors of the (implicit) input-integer without duplicates
 s   # Swap to get the (implicit) input-integer
  ¢  # And count how many time it occurs in the list

f    # Get a list of all prime factors of the (implicit) input-integer without duplicates
 `   # Dump all the content of this list onto the stack
  Q  # Check if the top two values are equal, or if only a single value is present, it will
     # use the (implicit) input-integer as second value

     # For all these program the same applies at the end:
     # (implicitly output the result with trailing newline)

NOTE: If only a truthy/falsey value is valid, and it doesn't necessary has to be distinct, either Òg or Õα could be used as valid 2-byters, since only 1 is truthy in 05AB1E, and everything else is falsey: Try both of them for some test cases.

If builtins were allowed, a single p would have sufficed: Try it online or verify some more test cases.

  • 1
    \$\begingroup\$ ÑPQ or ÒgΘ or ÒQP for 3 bytes. (Ñ and Ò both have purposes other than “to find, generate, or detect primes”, so they’re not included in the ban, by my reading). \$\endgroup\$
    – Grimmy
    Sep 2, 2019 at 23:39
  • 1
    \$\begingroup\$ More 3-byters: ÕαΘ, fQO, fs¢, f`Q \$\endgroup\$
    – Grimmy
    Sep 2, 2019 at 23:52
  • \$\begingroup\$ @Grimy Ah, can't believe I hadn't thought about the divisors or prime factor builtins.. I answered too quickly I guess. Didn't knew about Õα, though! That's a pretty nice one. \$\endgroup\$ Sep 3, 2019 at 7:23

Runic Enchantments, 7×1


Try it online!

Runic cares not for your feeble attempts at source rearrangement! Conforming to the requirement of still functioning after having the source transposed cost +3 bytes (+2 rectangle width) for the reflection modifier and entry point.

Transposed or transposed, but leaving the combining character attached to its parent.


dzaima/APL, 8×9 = 72

 ⍵t     }

Try the original or transposed!


Python 3, 28 x 28 size

lambda i:i>1 and           \
all(i%j for j in range(2,i))
ml="""                     "
b("                        "
di"                        "

Try it online!


JavaScript (Node.js), 26 25x5

n=>!( P/**//*-
= * P =* /** -         r 
>**/= r=>n%--r?P(r):~-r)(
/*////=  %  *        /*/n
*1 )//>*/   /        *//)

Try it online!


= **1
! //)
/*= *
* > /
* -*/

Try it online!


APL (Dyalog Unicode), 10x11

 ⍵         }

Try it online!

Corrected the function to comply with the specifications. Thanks @Adám for the heads-up.

Returns 0 for truthy, 1 for falsy.


{⍵∊∘.×⍨1↓⍳⍵ ⍝ Dfn. everything below this is a noop up until closing the brace
         ⍳⍵ ⍝ Range [1..arg]
       1↓   ⍝ Dropping the first element (yields [2..arg])
   ∘.×⍨     ⍝ Multiplication table for each element in the vector
 ⍵∊         ⍝ Check if the argument is in the table.

The transposed version is the exact same.


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.