#!/usr/bin/python3

# This is a Python program to display large primorials.
# A primorial is the product of all primes up to the given number.
# Uses the programs "matho-primes" and "matho-mult" as follows:
#	primorial 1000
# runs:
#	matho-primes 0 1000 | matho-mult
#
# For fun, try:
#	primorial `matho-primes 2 97`
#
# to display all unique primorials from 2 to 97.

import os
import sys
import subprocess

def usage(ev):
    print(f"""This program calculates large primorials.

Usage: {os.path.basename(sys.argv[0])} integers

A primorial is the product of all primes up to the given number""")
    sys.exit(ev)

def output_primorial(arg):
    p1 = subprocess.Popen(["matho-primes", "0"]+arg.split(), stdout=subprocess.PIPE)
    p2 = subprocess.Popen(["matho-mult"], stdin=p1.stdout, stdout=subprocess.PIPE)
    p1.stdout.close()
    res = p2.communicate()
    print(f"{arg}# = {res[0].decode().strip()}")

args = sys.argv[1:]
if (args == []):
	usage(2)
else:
	for arg in args:
		try:
			if (int(arg) < 1):
				print("Number too small.", file=sys.stderr)
				sys.exit(1)
		except:
			print("Positive integer required.", file=sys.stderr)
			usage(1)
		output_primorial(arg)
