abundant and deficient number counting

This is the place for queries that don't fit in any of the other categories.

abundant and deficient number counting

Postby john727377 » Wed Feb 05, 2014 3:13 pm

I need some help in counting the number of abundant and deficient numbers below is the code for the perfect number for a limit.I also want to count both abundant and deficient numbers.So could anyone please help in the code.


Code: Select all
limit = int(input("enter upper limit for perfect number search: "))
for n in range(2, limit + 1):
    sum = 0
    abd = 0
    defc = 0
    for divisor in range(1, n):
        if not n % divisor:
            sum += divisor       
    if sum == n:
        print(n, "is a perfect number")
        if sum > n :
            abd+=1
        if sum < n:
            defc+=1
print("the abundant are" ,abd)
print("the deficient are",defc)
john727377
 
Posts: 4
Joined: Tue Feb 04, 2014 3:42 am

Re: abundant and deficient number counting

Postby casevh » Wed Feb 05, 2014 3:43 pm

john727377 wrote:I need some help in counting the number of abundant and deficient numbers below is the code for the perfect number for a limit.I also want to count both abundant and deficient numbers.So could anyone please help in the code.


Code: Select all
limit = int(input("enter upper limit for perfect number search: "))
for n in range(2, limit + 1):
    sum = 0
    abd = 0
    defc = 0
    for divisor in range(1, n):
        if not n % divisor:
            sum += divisor       
    if sum == n:
        print(n, "is a perfect number")
        if sum > n :
            abd+=1
        if sum < n:
            defc+=1
print("the abundant are" ,abd)
print("the deficient are",defc)


Just hints this time...

Look carefully at where abd and defc are defined. How often are they initialized to 0?

Check your indentation. When are abd and defc incremented? Indentation is important.

Use more descriptive variable names.

Use space around operators. adb += 1 is easier to read.

casevh
casevh
 
Posts: 70
Joined: Sat Feb 09, 2013 7:35 am

Re: abundant and deficient number counting

Postby john727377 » Thu Feb 06, 2014 3:37 am

Got the solution here is the code:

Code: Select all
limit=int(input("enter number"))
counter1=0
counter2=0
counter3=0
for n in range(2, limit + 1):
    sum = 0
    for divisor in range(1, n):
        if not n % divisor:
            sum += divisor
    if sum == n:
        print(n, "is a perfect number")
        counter1+=1
    elif(sum>n):
        counter2+=1
    elif(sum<n):
        counter3+=1   
print(counter2,"abundant numbers")
print(counter3,"deficient numbers")
john727377
 
Posts: 4
Joined: Tue Feb 04, 2014 3:42 am


Return to General Coding Help

Who is online

Users browsing this forum: Yoriz and 2 guests