File: fact.py

package info (click to toggle)
saml 970418-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,204 kB
  • ctags: 1,701
  • sloc: ansic: 17,182; sh: 2,583; yacc: 497; perl: 264; makefile: 250; python: 242
file content (32 lines) | stat: -rwxr-xr-x 655 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
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/python
#
# A little demo to test integer arithmetic in SAML; it will compute the
# factorial of argv[1] (or 300 if no argument is given).
#
# You should compare with factorial0() which does the same job but
# with the builtin "long" integers of Python.

from sys import *
from saml1 import *
from string import atoi

def factorial0(n):
  f = 1l
  while n > 0:
    f,n = f*n,n-1
  return f

def factorial(n):
  f = Mathnode(ST_INTEGER, "1")
  while n > 0:
    f,n = f*n,n-1
  return f

def test():
  number = 300
  if len(argv)==2:
    number = atoi(argv[1])
  print '%d! == %s' % (number,factorial(number))

if __name__ == '__main__':
  test()