aboutsummaryrefslogtreecommitdiff
path: root/lib/fact.py
blob: 2beb0cbbf99ac7672b146fc8156a5ce76bdb5997 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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()