# 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. Commented May 17, 2013 at 14:31
• As demonstrated below: multiplication is very different from summation, especially for primes. Commented Jul 31, 2023 at 23:40

## 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. Commented May 17, 2013 at 14:46
• Did I just see an instance of telepathy?! Commented May 17, 2013 at 14:49
• @SohamChowdhury, no, you saw an instance of a trivial answer to a trivial question. Commented May 17, 2013 at 15:04
• Boohoo you just trolled me. Commented May 17, 2013 at 15:19
• Does J have a built-in way of calculating primes? Commented Jun 17, 2013 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 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! Commented May 22, 2013 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]. Commented May 22, 2013 at 16:30

# PARI/GP, 22 bytes

prodeuler(x=1,input,x)


## Mathematica, 27 chars

Times@@Prime@Range@PrimePi@


Usage

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

• I counted only 27 chars. Commented May 18, 2013 at 16:30
• @DavidCarraher You're right. Thanks Commented May 18, 2013 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. Commented Oct 2, 2017 at 0:46

### 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
Commented May 20, 2013 at 12:57

## Haskell, 90 chars

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) Commented Jun 18, 2013 at 21:10

## R - 48

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


# MATLAB/Octave, 23 bytes

prod(primes(input('')))


Try it online!

# 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));}


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

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


# Haskell, 76 bytes

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

• Anything after Java SE 7 Update 21 is disallowed for being released after the challenge was created. Make sure what you use is from that update or before. en.wikipedia.org/wiki/Java_version_history#Java_SE_7 Commented Sep 12, 2016 at 16:31

# Jelly, 3 bytes

ÆRP


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!