Transposed, primes, oh my!

15

1

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!

vsz

Posted 2012-10-05T21:01:58.843

Reputation: 7 963

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. – boothby – 2012-10-09T19:31:55.880

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 – 2012-10-10T03:09:27.043

Answers

7

GolfScript, 13 × 1

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

Ilmari Karonen

Posted 2012-10-05T21:01:58.843

Reputation: 19 513

Wrt "anything fancier would cost a few extra characters" - you can get a GolfScript-style Boolean for only 2. – Peter Taylor – 2012-10-06T15:22:07.103

@Peter: You mean something like my edit above, only without the !? Or did you have something fancier in mind? – Ilmari Karonen – 2012-10-07T14:00:25.137

1I was thinking ) before the , so that it always finds a divisor and = at the end. – Peter Taylor – 2012-10-07T18:39:08.223

: 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 – gnibbler – 2012-10-10T02:08:28.117

@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...). – Ilmari Karonen – 2012-10-10T12:44:40.700

21

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.

ugoren

Posted 2012-10-05T21:01:58.843

Reputation: 16 527

wow, clever misuse of line concatenation :) – vsz – 2012-10-06T15:14:52.520

2This is a golf/puzzles site. There is no such thing as misuse. – boothby – 2012-10-09T17:23:36.967

2@boothby, I guess he means abuse. But I don't argue with compliments. – ugoren – 2012-10-09T20:04:28.573

14

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)
//O n{ adfau+
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

quasimodo

Posted 2012-10-05T21:01:58.843

Reputation: 985

12

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
////i,fad%1t
////nnon"+;s
main//rf,+)(
(m,n//((&m;"
){for(//n&ny
scanf(//)&-"
"%d",&n)//m)
;n%++m&&//|;
n>1;);n-m|| 
puts("y"); }

ugoren

Posted 2012-10-05T21:01:58.843

Reputation: 16 527

7

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 ~          

mob

Posted 2012-10-05T21:01:58.843

Reputation: 2 506

3

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.

skeevey

Posted 2012-10-05T21:01:58.843

Reputation: 4 139

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. – gnibbler – 2012-10-10T02:11:47.143

Very true, thanks. I haven't had a chance to try and get a better score yet. – skeevey – 2012-10-10T02:16:59.307

You should include a score in your answer – FlipTack – 2017-12-28T09:32:55.180

2

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)

caird coinheringaahing

Posted 2012-10-05T21:01:58.843

Reputation: 13 702

4This 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." – Kevin Cruijssen – 2019-09-02T09:37:06.097

1

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¢
f`Q

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

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.

Kevin Cruijssen

Posted 2012-10-05T21:01:58.843

Reputation: 67 575

1Ñ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). – Grimmy – 2019-09-02T23:39:18.353

1More 3-byters: ÕαΘ, fQO, fs¢, f`Q – Grimmy – 2019-09-02T23:52:51.383

@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. – Kevin Cruijssen – 2019-09-03T07:23:40.897

0

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.

J. Sallé

Posted 2012-10-05T21:01:58.843

Reputation: 3 233

0

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.

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

Draco18s no longer trusts SE

Posted 2012-10-05T21:01:58.843

Reputation: 3 053

0

dzaima/APL, 8×9 = 72

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

Try the original or transposed!

dzaima

Posted 2012-10-05T21:01:58.843

Reputation: 19 048

0

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!

Joel

Posted 2012-10-05T21:01:58.843

Reputation: 1 691

0

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!

Shieru Asakoto

Posted 2012-10-05T21:01:58.843

Reputation: 4 445