Question

Write a Python program to print all Perfect numbers between 1 to n. (Use any loop...

  1. Write a Python program to print all Perfect numbers between 1 to n. (Use any loop you want)

Perfect number is a positive integer which is equal to the sum of its proper positive divisors.

Proper divisors of 6 are 1, 2, 3.

Sum of its proper divisors = 1 + 2 + 3 = 6.

Hence 6 is a perfect number.

*** proper divisor means the remainder will be 0 when divided by that number.

Sample Input: n = 1000;

Sample Output: 6, 28, 496

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def divisors(n):
    s = 1
    for x in range(2,n):
        if(n%x == 0):
            s += x
    return s

def isperfect(n):
    if(divisors(n) == n):
        return True
    else:
        return False

#Testing
def main():
    n = int(input("n = "))
    lst = []
    for i in range(2,n+1):
        if(isperfect(i)):
            lst.append(i)
    print(str(lst)[1:-1])

main()

n = 1000
6, 28, 496

Add a comment
Know the answer?
Add Answer to:
Write a Python program to print all Perfect numbers between 1 to n. (Use any loop...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A perfect number is a positive integer that equals the sum of all of its divisors...

    A perfect number is a positive integer that equals the sum of all of its divisors (including the divisor 1 but excluding the number itself). For example 6, 28 and 496 are perfect numbers because 6=1+2+3 28 1 + 2 + 4 + 7 + 14 496 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248 Write a program to read a positive integer value, N, and find the smallest perfect number...

  • A perfect number is a positive integer that is equal to the sum of its (proper)...

    A perfect number is a positive integer that is equal to the sum of its (proper) positive divisors, including 1 but excluding itself. A divisor of a number is one which divides the number evenly (i.e., without a remainder). For example, consider number 6. Its divisors are 1, 2, 3, and 6. Since we do not include number itself, we only have 1, 2, and 3. Because the sum of these divisors of 6 is 6, i.e., 1 + 2...

  • A positive integer is said to be a perfect number if it equals the sum of...

    A positive integer is said to be a perfect number if it equals the sum of its positive divisors (excluding the number itself). As an example, 6 is aperfect number because its divisors, 1, 2, and 3 sum up to 6. The first four perfect numbers are 6, 28, 496, 8128. Write a C program that asks the user to enter a number and checks if the number is perfect. Your program should run interactively until the user quits. Try...

  • Using the following functions, modify the Python program written for the perfect number into TDD Java...

    Using the following functions, modify the Python program written for the perfect number into TDD Java program: 1) divisors (n) - takes a positive integer 'n' and return it's divisors sum as output. 2) isPerfect(n) - takes a positive integer 'n' as input and produces the result either as 'n is perfect' or 'n is not perfect using the function divisors()

  • Need help programing this in C. rinteivsors Print the proper divisors of an integer value The...

    Need help programing this in C. rinteivsors Print the proper divisors of an integer value The program should read a single integer input value, which you can assume will be positive. It should then print a single line of output with all of the proper divisors of the input value in order from least to greatest. A proper divisor d of an integer n is an integer that evenly divides n: i.e., nld is an integer For example, if the...

  • Please Write the task in c++ Task A perfect number is an integer that is equal...

    Please Write the task in c++ Task A perfect number is an integer that is equal to the sum of its divisors (where 1 is considered a divisor). For example, 6 is perfect because its divisors are 1, 2, and 3, and 1 + 2 + 3 is 6. Similarly, 28 is perfect because it equals 1 + 2 + 4 + 7 + 14. A quite good number is an integer whose badness—the size of the difference between the...

  • For C++: A perfect number has these properties 1) positive integer 2) the sum of its...

    For C++: A perfect number has these properties 1) positive integer 2) the sum of its proper divisors is the number itself 6 and 28 are perfect numbers Write two functions that return true if its value parameter is a perfect number and false if its value parameter is not a perfect numbers. The two function look like bool isPerfect(int) and void isPerfect(int, bool&) If you want more perfect number you find them on the Internet.

  • C Programming Write a C program that asks the user for a positive number n (n...

    C Programming Write a C program that asks the user for a positive number n (n ≥ 1) then, the program displays all the perfect numbers up to (including) n. A number n is said to be a perfect number if the sum of all its positive divisors (excluding itself) equals n. For example, the divisors of 6 (excluding 6 itself) are 1,2 and 3, and their sum equals 6, thus, 6 is a perfect number. Display the numbers with...

  • Write a program that asks the user for a lower limit and an upper limit. The...

    Write a program that asks the user for a lower limit and an upper limit. The program finds all pairs of amicable numbers where the first number of the pair is between those limits. The second number of the pair may or may not be between the limits. To do this sensibly, write a function int sumDivisors( int num ); that returns the sum of all the proper divisors of num. The main program looks at each integer N between...

  • Write a python program that uses a while loop to print numbers 100,98,96... all the way...

    Write a python program that uses a while loop to print numbers 100,98,96... all the way to 0. each number should be printed on a seperate line

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT