0

I have 3 functions working together, but I only have abundant returned every single time, I don't know what is wrong

def sumDivisors(n):
    total = 0
    for counter in range(1, n):
        if (isDivisor(n, counter)):
            total += counter
        return total


def checkForPerfect(n):
    total = sumDivisors
    if (total == n):
        return 'Perfect'
    elif (total < n):
        return 'Deficient'
    else:
        return 'Abundant'
    

    
        
        
def isDivisor(a, b):
    if (a%b) == 0:
        return True
    return False
2
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by persianprez
Featured Replies
  • line 10 should be [icode]total = sumDivisors(n)[/icode]. Also you can speed this up by adding [icode]total += counter + n/counter[/icode] when [icode]counter < n/counter[/icode] and [icode]total += counter[/icode] when [icode]counter == n/counter[/icode]. Read More

1

line 10 should be total = sumDivisors(n) . Also you can speed this up by adding total += counter + n/counter when counter < n/counter and total += counter when counter == n/counter .

0

LOL, it's always the simplest answers, I can't believe I didnt see that -.- THANKS

Edited by persianprez: n/a

0

One more question, I'm also getting wrong output from the following, trying to find prime or composite numbers:

def isPrime(n):
    if (n==2):
        return True
    for counter in range(3,n):
        if n % counter == 0:
            return False
        return True

for the output:

for n in range (start,end+1):
        prime = isPrime(n)
if (n==1):
        print "%-11s" %'Neither',
        
    elif prime:
        print "%-11s" %'Prime',
    else:
        print "%-11s" %'Composite',
0

Your function isPrime should read

def isPrime(n):
    if (n==2):
        return True
    for counter in range(2,n):
        if n % counter == 0:
            return False
    return True

Also it's very inefficient.

0

yeah I know its inefficient, but thats the way the prof. want's it :icon_confused:
Thanks for the correction, I keep overseeing mundane details

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.