File: harmonic.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 (29 lines) | stat: -rwxr-xr-x 559 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
#!/usr/bin/python
#
# Computes 1 + 1/2 + 1/3 + ... +1/2000
#

from saml1 import *
import string

def test1(loops):
  s = Mathnode(ST_RATIONAL, '0')
  n = s
  for i in xrange(loops):
    n = n + n.one()
    s = s + n.invert()
  print 'H(%d) == %s' % (loops,s)

# Pretty straightworward, isn't it ?
# Now compare with this:

def test2(loops):
  expr = string.join(map(lambda x:'1/%d'%x, range(1,loops+1)),'+')
  s = Mathnode(ST_RATIONAL,'0').parse(expr)
  print 'H(%d) == %s' % (loops,s)

# Main

if __name__ == '__main__':
  test1(2000)
  print MnAllocStats()