# Evaluate the primorial of a number [duplicate]

The primorial of a number is the product of all the primes until that number, itself included.

Take a number from STDIN as the input and evaluate its primorial, also known as the prime factorial.

Don't use libraries. Shortest code wins.

Test cases:

1 1
2 2
3 6
4 6
5 30
6 30
7 210
8 210
9 210
10 210
11 2310
12 2310
13 30030
14 30030
15 30030
16 30030
17 510510
18 510510
19 9699690

• Essentially the same task as Sum of primes between given range, just with multiplication instead of addition and the starting value of the range is fixed. – manatwork May 17 '13 at 14:31

## J, 13 characters

*/p:i._1 p:1+


Examples:

   */p:i._1 p:1+7
210

*/p:i._1 p:1+12
2310

• Damn. Just beat me. – Gareth May 17 '13 at 14:46
• Did I just see an instance of telepathy?! – Soham Chowdhury May 17 '13 at 14:49
• @SohamChowdhury, no, you saw an instance of a trivial answer to a trivial question. – Peter Taylor May 17 '13 at 15:04
• Boohoo you just trolled me. – Soham Chowdhury May 17 '13 at 15:19
• Does J have a built-in way of calculating primes? – arshajii Jun 17 '13 at 13:36

## Python 2, 49 bytes

P=k=1
exec"P*=P**k%k<1or k;k+=1;"*input()
print P


Counts up k from 1 to the input, updating the primorial P by multiplying P by k whenever it's prime. Since P contains all lower prime factors, a sufficiently high power of P will contain k as a factor if and only if k is non-prime. The power P**k suffices, and we check whether P**k%k<1, multipling P by 1 if so (doing nothing) and by k otherwise.

## Mathematica, 27 chars

Times@@Prime@Range@PrimePi@


Usage

Times@@Prime@Range@PrimePi@100
(*
2305567963945518424753102147331756070
*)

• I counted only 27 chars. – DavidC May 18 '13 at 16:30
• @DavidCarraher You're right. Thanks – Dr. belisarius May 18 '13 at 17:26
• Is that @ symbol at the end meant to be there? On it's own that'd be invalid syntax. You could probably just omit it. – numbermaniac Oct 2 '17 at 0:46

# Mathematica 21

The large Pi represents the product function.

PrimePi@n returns the number of primes up to and including n.

Prime@i returns the ith prime.

• C'mon... the product function is one char?! That's an ingenious workaround! – Soham Chowdhury May 22 '13 at 16:13
• Thanks. It's somewhat like the case of "a+b", which is shorthand for Plus[a,b] or "a.b.c", which stands for the dot product, Dot[a,b,c]. – DavidC May 22 '13 at 16:30

### Python, 72 characters

Quickly golfed, I imagine there are better python solutions.

n=t=1;exec"t*=(1,n)[all(n%i for i in range(2,n))];n+=1;"*input();print t


I used wikipedia to verify up to 71

Usage:

$python primorial.py 100 2305567963945518424753102147331756070$ python primorial.py
101
232862364358497360900063316880507363070

• Whoops, just updated it so it actually works! – ejrb May 20 '13 at 12:57

# PARI/GP, 22 bytes

prodeuler(x=1,input,x)


main=getLine>>= \v->print.product.takeWhile(<=read v)$[n|n<-[2..],all((>0).rem n)[2..n-1]]  or (also 90 chars): main=getLine>>=print.product.flip takeWhile [n|n<-[2..],all((>0).rem n)[2..n-1]].(>=).read  ### Compilation ghc primorial.hs  ### Usage $ echo "12" | ./primorial
2310


or

$./primorial 12<ENTER> 2310  • Some room for improvement here. takeWhile is unnecessary, we can just add an upper limit for the [2..] instead. Instead of getLine + read, we can use readLn. Also, do-notation is slightly shorter than using >>=. Result: main=do v<-readLn;print$product[n|n<-[2..v],all((>0).rem n)[2..n-1]] (68 characters) – hammar Jun 18 '13 at 21:10

## R - 48

x=1:scan();prod(x[rowSums(outer(x,x,%%)<1)<3])


# C, 125 bytes

p(n,x){return x==1?n:n%x?p(n,x-1):1;}
f(n){return n==1?1:p(n,n-1)*f(n-1);}
main(){int n;scanf("%d",&n);printf("%d\n",f(n));}


Smalltalk, Squeak 4.x flavour

Let's say we read n from stdin in 38 chars (this kind of requirement is just killing!)

n:=FileStream stdin nextLine asNumber.


If only product were defined in Collection in base image, this could be as simple as

^(0class primesUpTo:n+1)product


or:

^((2to:n)select:#isPrime)product


Unfortunately, we have to code #product by ourself in 48 chars (86 total).

(0class primesUpTo:n+1)inject:1into:[:p :i|p*i]


Or if primesUpTo: is considered as a library, we can use isPrime in 62 chars (100 total)

^(1to:n)inject:1into:[:p :i|i isPrime ifTrue:[p*i]ifFalse:[p]]


If even isPrime is forbidden we can find by ourselves in 67 chars (105 total)

p:=1. 2to:n do:[:i|((2to:i-1)anySatisfy:[:d|i\\d=0])or:[p:=p*i]].^p


Or for the fun, we can use a sieve in 108 chars (146 total)

s:=(p:=y:=1)<<n-2. 2to:n do:[:x|y:=(z:=y)*2+1.(z+1bitAnd:s)=0or:[p:=p*x.s:=z<<(n//x*x+x)//y+z+1bitAnd:s]].^p


Explanation of the sieve at http://smallissimo.blogspot.fr/2011/07/revisiting-sieve-of-erathostenes.html

Nota: bitAnd: has a short-cut operator & in Pharo 2.0, which would reduce Sieve cost to 94 chars (132 total)

s:=(p:=y:=1)<<n-2. 2to:n do:[:x|y:=(z:=y)*2+1.z+1&s=0or:[p:=p*x.s:=z<<(n//x*x+x)//y+z+1&s]].^p


main=interact$\n->show$product[x|x<-[2..read n],and[xmody>0|y<-[2..x-1]]]


Example:

\$ echo 12 | ./primorial
2310


# Java 7, 213 bytes

class M{public static void main(String[]a){c(new Long(a[0]));}static void c(long n){long r=1,i=0;for(;++i<=n;r*=p(i)?i:1);System.out.print(r);}static boolean p(long n){int i=2;while(i<n)n=n%i++<1?0:n;return n>1;}}


Ungolfed:

class M{
static void c(long n){
long r = 1,
i = 0;
for (; ++i <= n; r *= p(i) ? i : 1);
System.out.print(r);
}

public static void main(String[] a){
c(new Long(a[0]));
}

static boolean p(long n){
int i = 2;
while(i < n){
n = n % i++ < 1 ? 0 : n;
}
return n > 1;
}
}


Usage: java -jar M.jar 17
Output: 510510

# Jelly, 3 bytes

ÆRP


Try it online!

# MATLAB/Octave, 23 bytes

prod(primes(input('')))


Try it online!

# RProgN 2, 6 bytes

S²P¬-*


## Explained

S²P¬-*
S       # Produce as stack from 1 to the input.
²  -   # Filter it by the function provided
P¬    # Is it not a prime?
*  # Get the product of the now list of primes.


Try it online!

# Japt, 6 bytes

õ fj ×


Try it here

# 05AB1E, 3 bytes

ÅPP


Try it online!

# Casio-Basic, 41 bytes

prod(seq(piecewise(isPrime(x),x,1),x,1,n


Since the calculator doesn't have a "select" function of any kind, a piecewise/hybrid function is used to return the number if it's prime, otherwise return 1. seq does this over a range of integers from 1 to n, and prod multiplies the whole list together.

40 bytes for the function, +1 byte to enter n in the parameters box.