diff options
Diffstat (limited to 'lib/fact.py')
| -rw-r--r-- | lib/fact.py | 37 |
1 files changed, 37 insertions, 0 deletions
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() |
