# Transposed, primes, oh my!

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.

# Edit:

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

• 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. Oct 9, 2012 at 19:31
• 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.
– vsz
Oct 10, 2012 at 3:09

## 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).

main(m,n){for(scanf("%d",&n);n%++m&&n>1;);n-m||puts("y");}/*
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/

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

abcd/*
\\\\*/

To this:

a\
b\
c\
d\
/*
*/

And both mean simply abcd.

• wow, clever misuse of line concatenation :)
– vsz
Oct 6, 2012 at 15:14
• This is a golf/puzzles site. There is no such thing as misuse. Oct 9, 2012 at 17:23
• @boothby, I guess he means abuse. But I don't argue with compliments. 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.

////m(i;(a=)<
////aans")a;2
//:Di)tc%;;p)
main//bn"o%t4
(a){///f,r-c8
int b///&(-h)
;scanf///bba;
("%d",&///;r}
a);for(b///(
=a;a%--b;//(
);putchar((b
<2)+48);}

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.

////m()s";np
////am{c%n>u
////nnon"+;s
main//rf,+)(
(m,n//((&m;"
){for(//n&ny
scanf(//)&-"
"%d",&n)//m)
;n%++m&&//|;
n>1;);n-m||
puts("y"); }

~.,2>{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. • Wrt "anything fancier would cost a few extra characters" - you can get a GolfScript-style Boolean for only 2. Oct 6, 2012 at 15:22 • @Peter: You mean something like my edit above, only without the !? Or did you have something fancier in mind? Oct 7, 2012 at 14:00 • I was thinking ) before the , so that it always finds a divisor and = at the end. Oct 7, 2012 at 18:39 • : 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 Oct 10, 2012 at 2:08 • @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...). 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;
print!$p+m~ n(.r =$.i
pn$n o%nt p$/!
;_2);p
pf +
|o m
=r ~

# Q

Abused comments for a symmetric, character inefficient solution.

/{/////////////////////////////////
{(~)any 0=mod[x;2+(!)x-2]}"I"$(0:)0 /~ /) /a /n /y / /0 /= /m /o /d /[ /x /; /2 /+ /( /! /) /x /- /2 /] /} /" /I /" /$
/(
/0
/:
/)
/0

Takes input from STDIN, returns a boolean.

• 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. Oct 10, 2012 at 2:11
• Very true, thanks. I haven't had a chance to try and get a better score yet. Oct 10, 2012 at 2:16
• You should include a score in your answer Dec 28, 2017 at 9:32

# Jelly, 2x2 square

PÆ
ÆP

Try it online!

I think I have the transposition part correct, and if so, the transposed version of this is

PÆ
ÆP

Try it online!

(which is the same code)

• This is invalid: "You can use any language, and any library function except if the sole purpose of that function is to find, generate, or detect primes." Sep 2, 2019 at 9:37

# 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).

ÑPQ
ÒgΘ
ÒQP
ÕαΘ
fQO
fs¢
fQ

-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.

Explanation:

Ñ    # 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.

• Ñ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). Sep 2, 2019 at 23:39
• More 3-byters: ÕαΘ, fQO, fs¢, f`Q Sep 2, 2019 at 23:52
• @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. Sep 3, 2019 at 7:23

# Runic Enchantments, 7×1

v̀i'PA@

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.

{t←0,1↓⍳⍵
←⍵∊∘.×⍨t
0∊
,∘
1.
↓×
⍳⍨
⍵t     }

# Python 3, 28 x 28 size

lambda i:i>1 and           \
all(i%j for j in range(2,i))
ml="""                     "
b("                        "
di"                        "
a%"
j
i
:f
io
>r
1
j
a
ni
dn

r
a
n
g
e
(
2
,
i
)
\)"""

Try it online!

# JavaScript (Node.js), 26 25x5

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

Try it online!

Transposed:

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

Try it online!

# APL (Dyalog Unicode), 10x11

{⍵∊∘.×⍨1↓⍳⍵
∊
∘
.
×
⍨
1
↓
⍳
⍵         }

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.

### How

{⍵∊∘.×⍨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.