PYTHON   35

sum py

Guest on 5th August 2022 03:03:01 PM

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. def sum_pow(max, exp):
  5.     """
  6.    Sums i in intervall 1 to max-1 of pow(i,exp).
  7.    
  8.    @type max: integer
  9.    @param max: the maximum intervall limit
  10.    @type exp: number
  11.    @param exp: the exponent value
  12.    @rtype: number
  13.    @return: the computed sum value
  14.    """
  15.     result = 0
  16.     for i in xrange(1, max):
  17.         result += pow(i,exp)
  18.     return result
  19.  
  20. def sum_divide(max, div):
  21.     """
  22.    Sums i in intervall 1 to max-1 of i / div
  23.    
  24.    @type max: integer
  25.    @param max: the maximum intervall limit
  26.    @type div: number
  27.    @param div: the exponent value
  28.    @rtype: number
  29.    @return: the computed sum value
  30.    """
  31.     result = 0
  32.     for i in xrange(1, max):
  33.         result += i / float(div)
  34.     return result
  35.    
  36. # Test code
  37.  
  38. print sum_pow(12, 6)
  39. print sum_divide(12,6)

Raw Paste


Login or Register to edit or fork this paste. It's free.