File: fact.py

package info (click to toggle)
mathomatic 16.0.5-5.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,192 kB
  • sloc: ansic: 22,029; makefile: 340; sh: 319; python: 96; awk: 39
file content (20 lines) | stat: -rw-r--r-- 621 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# This is a general factorial function written in Python.
# A factorial is the product of all positive integers <= a given number.
# Works transparently with integers and floating point;
# that is, it returns the same type as its single argument.
# Gives an error for negative or non-integer input values.

def factorial(x):
	"Return x! (x factorial)."
	if (x < 0 or (x % 1.0) != 0.0):
		raise ValueError("Factorial argument must be a positive integer.")
	if (x == 0):
		return x + 1
	d = x
	while (x > 2):
		x -= 1
		temp = d * x
		if (temp <= d):
			raise ValueError("Factorial result too large.")
		d = temp
	return d