auden
auden

Reputation: 1157

Writing a program that finds perfect numbers - error

I'm working on a program that finds perfect numbers (i.e., 6, because its factors, 1, 2, and 3, add up to itself). My code is

k=2
mprim = []
mprimk = []
pnum = []

def isprime(n):
    """Returns True if n is prime."""
    if n == 2:
        return True
    if n == 3:
        return True
    if n % 2 == 0:
        return False
    if n % 3 == 0:
        return False

    i = 5
    w = 2

    while i * i <= n:
        if n % i == 0:
            return False

        i += w
        w = 6 - w

    return True

def mprime(k):
    while k < 50:
        check = (2**k)-1
        if isprime(check) == True:
            mprim.append(check)
            mprimk.append(k)
            print check
            k+=1
        else:
            k+=1

mprime(k)

def isperf(lst):
    for i in lst:
        prm = (((2**i)-1)(2**i))/(2)
        pnum.append(prm)

isperf(mprimk)

print pnum

The first part, that checks if a number is prime, and produces mercenne primes, is working alright. Its the second part I'm having trouble with. I have read that if 2^k - 1 is prime, then ((2^k - 1)(2^k))/2 is a perfect number, so I am using that formula.

The error it gives is

Traceback (most recent call last):
  File "python", line 47, in <module>
  File "python", line 44, in isperf
TypeError: 'int' object is not callable

Line 47 is isperf(mprimk) and line 44 is prm = (((2**i)-1)(2**i))/(2). Any assistance would be appreciated.

Thanks!

Upvotes: 1

Views: 67

Answers (1)

Damaon
Damaon

Reputation: 612

The error clearly states that you are trying to call an int type, which isn't callable.

In practice it means you trying to do something like 123()

And code responsible for it is ((2**i)-1)(2**i) because you forgot * and it should be (((2**i)-1)*(2**i))/(2)

Upvotes: 1

Related Questions