- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to identify and print all the perfect numbers in some closed interval [ 2, n ] using Python?
A perfect number is a positive integer that is equal to the sum of its proper divisors. The smallest perfect number is 6, which is the sum of 1, 2, and 3.
You can find perfect numbers within a given range by testing each number for the given condition in the given range.
example
def print_perfect_nums(start, end): for i in range(start, end + 1): sum1 = 0 for x in range(1, i): # Check if a divisor, if it is, add to sum if(i % x == 0): sum1 = sum1 + x if (sum1 == i): print(i) print_perfect_nums(1, 300)
Output
This will give the output
6 28
- Related Articles
- How to Print all Prime Numbers in an Interval using Python?
- Python program to print all Prime numbers in an Interval
- Python Program to Print Numbers in an Interval
- Print n numbers such that their sum is a perfect square
- Print all numbers less than N with at-most 2 unique digits in C++
- Print all n-digit strictly increasing numbers in C++
- How to print all the Armstrong Numbers from 1 to 1000 using C#?
- Print all odd numbers and their sum from 1 to n in PL/SQL
- Sum all perfect cube values upto n using JavaScript
- Python program to print all Happy numbers between 1 and 100
- Queries to Print All the Divisors of n using C++
- Python Program to Read a Number n And Print the Series "1+2+…..+n= "
- Python program to print all even numbers in a range
- Python program to print all odd numbers in a range
- Print all prime numbers less than or equal to N in C++

Advertisements