~pythonregexp2.7/python/issue2636-01+09-01-01

« back to all changes in this revision

Viewing changes to Demo/scripts/fact.py

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-22 00:02:12 UTC
  • mfrom: (39022.1.34 Regexp-2.7)
  • Revision ID: darklord@timehorse.com-20080922000212-7r0q4f4ugiq57jph
Merged in changes from the Atomic Grouping / Possessive Qualifiers branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
import sys
9
9
from math import sqrt
10
10
 
11
 
error = 'fact.error'            # exception
12
 
 
13
11
def fact(n):
14
 
    if n < 1: raise error   # fact() argument should be >= 1
 
12
    if n < 1: raise ValueError # fact() argument should be >= 1
15
13
    if n == 1: return []    # special case
16
14
    res = []
17
15
    # Treat even factors special, so we can use i = i+2 later
18
16
    while n%2 == 0:
19
17
        res.append(2)
20
 
        n = n/2
 
18
        n = n//2
21
19
    # Try odd numbers up to sqrt(n)
22
20
    limit = sqrt(float(n+1))
23
21
    i = 3
24
22
    while i <= limit:
25
23
        if n%i == 0:
26
24
            res.append(i)
27
 
            n = n/i
 
25
            n = n//i
28
26
            limit = sqrt(n+1)
29
27
        else:
30
28
            i = i+2