By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,715 Members | 748 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,715 IT Pros & Developers. It's quick & easy.

to calculate the universal distribution of continued fraction expansion

P: 2
Hi all,

I'm studying python program in uni.
my lecture asks us to calculate the universal distribution of continued fraction expansion

But I always have an error says: TypeError: 'builtin_function_or_method' object is not subscriptable.
here's my algorithm:

Expand|Select|Wrap|Line Numbers
  1. def universal_distro(N=10000):
  2.         import random
  3.     x = random.random()
  4.     g = list()
  5.     m = dict()
  6.     o = dict()
  7.     N = len(o)
  8.     while N < 10000:
  9.         for x in range[0,1]:
  10.             w = number2cfe(x,N=15)
  11.             e = w[n]
  12.             if n < 15:
  13.                 count = 0
  14.                 if w[n] not in g:
  15.                     for e in w:
  16.                         if e == w[n]:
  17.                             count += 1
  18.                     g.append(w[n])
  19.                 n += 1
  20.                 m[w[n]] = count
  21.         for k in m.keys():
  22.             if o.has_key(k):
  23.                 o[k] += m[k]
  24.             else:
  25.                 o[k] = m[k]
  26.         N += 1
  27.     return o
  28.  
  29. >>> universal_distro(10000)
  30. File "<pyshell#120>", line 9, in universal_distro
  31.     for x in range[0,1]:
  32. TypeError: 'builtin_function_or_method' object is not subscriptable
  33.  
Can someone help me out?
thanks
Sep 3 '11 #1
Share this Question
Share on Google+
3 Replies


P: 1
ohh I think your syntax is incorrect for the Range()

try using ( instead of [
Sep 3 '11 #2

P: 2
Hi I want to calculate the distribution of continued fraction expansion (of N up to 10000). I think I need to choose a random variable x and calculate its cfe for n = 15, then store the counts in a dictionary. Then, choose another random variable x and continue the calculation until the size of the dictionary reaches 10000.

But I just can't let my program understand this idea.
Sep 4 '11 #3

Expert 100+
P: 621
You don't use the random numbers anywhere? Explain in more detail what you want to do (the single letter variable names give me no clue). Also, you now have "x" defined twice
Expand|Select|Wrap|Line Numbers
  1.     x = random.random()
  2. #
  3. # and --> note that this is a bogus statement as it will only execute once
  4.         for x in range[0,1]:  # should be range(0, 1)
  5. #
  6. # perhaps you want something more like
  7. import random
  8. for x in xrange(0, 10): ## but you would use while len(a_dict) < 10000:
  9.     print random.random() 
Sep 4 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.