Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Demo/scripts/fact.py

    r2 r391  
    1010
    1111def fact(n):
    12     if n < 1: raise ValueError # fact() argument should be >= 1
    13     if n == 1: return []    # special case
     12    if n < 1:
     13        raise ValueError('fact() argument should be >= 1')
     14    if n == 1:
     15        return []  # special case
    1416    res = []
    15     # Treat even factors special, so we can use i = i+2 later
    16     while n%2 == 0:
     17    # Treat even factors special, so we can use i += 2 later
     18    while n % 2 == 0:
    1719        res.append(2)
    18         n = n//2
     20        n //= 2
    1921    # Try odd numbers up to sqrt(n)
    20     limit = sqrt(float(n+1))
     22    limit = sqrt(n+1)
    2123    i = 3
    2224    while i <= limit:
    23         if n%i == 0:
     25        if n % i == 0:
    2426            res.append(i)
    25             n = n//i
     27            n //= i
    2628            limit = sqrt(n+1)
    2729        else:
    28             i = i+2
     30            i += 2
    2931    if n != 1:
    3032        res.append(n)
     
    3335def main():
    3436    if len(sys.argv) > 1:
    35         for arg in sys.argv[1:]:
    36             n = eval(arg)
     37        source = sys.argv[1:]
     38    else:
     39        source = iter(raw_input, '')
     40    for arg in source:
     41        try:
     42            n = int(arg)
     43        except ValueError:
     44            print arg, 'is not an integer'
     45        else:
    3746            print n, fact(n)
    38     else:
    39         try:
    40             while 1:
    41                 n = input()
    42                 print n, fact(n)
    43         except EOFError:
    44             pass
    4547
    4648if __name__ == "__main__":
Note: See TracChangeset for help on using the changeset viewer.