From a19a216bc60160c162e616145ef091dd18ce4e61 Mon Sep 17 00:00:00 2001 From: Skip Montanaro Date: Tue, 16 Feb 2021 14:40:46 -0600 Subject: Python 0.9.1 as posted in alt.sources --- lib/fact.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 lib/fact.py (limited to 'lib/fact.py') diff --git a/lib/fact.py b/lib/fact.py new file mode 100644 index 0000000..ba961c4 --- /dev/null +++ b/lib/fact.py @@ -0,0 +1,37 @@ +# Factorize numbers -- slow, could use a table of all primes <= 2*16 + +import sys +import math + +error = 'fact.error' # exception + +def fact(n): + if n < 1: raise error # fact() argument should be >= 1 + if n = 1: return [] # special case + res = [] + _fact(n, 2, res) + return res + +def _fact(n, lowest, res): + highest = int(math.sqrt(float(n+1))) + for i in range(lowest, highest+1): + if n%i = 0: + res.append(i) + _fact(n/i, i, res) + break + else: + res.append(n) + +def main(): + if len(sys.argv) > 1: + for arg in sys.argv[1:]: + n = eval(arg) + print n, fact(n) + else: + try: + while 1: + print fact(input()) + except EOFError: + pass + +main() -- cgit v1.2.3