| •1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
# This is a Python program to display large primorials. |
| 4 |
# A primorial is the product of all primes up to the given number. |
| 5 |
# Uses the programs "matho-primes" and "matho-mult" as follows: |
| 6 |
# primorial 1000 |
| 7 |
# runs: |
| 8 |
# matho-primes 0 1000 | matho-mult |
| 9 |
# |
| 10 |
# For fun, try: |
| 11 |
# primorial `matho-primes 2 97` |
| 12 |
# |
| 13 |
# to display all unique primorials from 2 to 97. |
| 14 |
|
| 15 |
import os |
| 16 |
import sys |
| 17 |
|
| 18 |
def usage(ev): |
| •19 |
print("This program calculates large primorials.") |
| 20 |
print() |
| 21 |
print("Usage: %s integers" % os.path.basename(sys.argv[0])) |
| 22 |
print() |
| 23 |
print("A primorial is the product of all primes up to the given number.") |
| 24 |
sys.exit(ev) |
| 25 |
|
| 26 |
def output_primorial(arg): |
| 27 |
sys.stdout.write(arg + "# = ") |
| 28 |
sys.stdout.flush() |
| 29 |
if (os.system("matho-primes 0 " + arg + " | matho-mult") != 0): |
| 30 |
sys.exit(1) |
| 31 |
|
| 32 |
args = sys.argv[1:] |
| 33 |
if (args == []): |
| 34 |
usage(2) |
| 35 |
else: |
| 36 |
for arg in args: |
| 37 |
try: |
| 38 |
if (int(arg) < 1): |
| •39 |
print("Number too small.", file=sys.stderr) |
| 40 |
sys.exit(1) |
| 41 |
except: |
| •42 |
print("Positive integer required.", file=sys.stderr) |
| 43 |
usage(1) |
| 44 |
output_primorial(arg) |