Discuss Finding perfect numbers in the Python Programming forum on Dev Shed. Finding perfect numbers Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
Receive the tools necessary to be the rock star of your field. Our 12-month program teaches you the evolving world of multi-channel marketing as well as the complex issues and opportunities found in the industry.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Web development can be a daunting task, even for specialists. There is a lot of information to absorb and a lot of technologies to learn in order to manage a superior website. When trying to learn the ropes, developers need a reliable source to introduce new ideas that can be easily implemented. When working on large projects, even web veterans may run into a technology or an aspect of a technology that they are unfamiliar with.
Posts: 30
Time spent in forums: 6 h 40 m 4 sec
Reputation Power: 1
Finding perfect numbers
I am trying to write a program that will find all perfect numbers between 1 & 1,000. I just want to make it list the correct numbers. I know that 6, 28, 496, and 8128 are all that exist in this range. I have researched and understand that I need to use some type of for statement but I do not understand what formula to use or how to make the program know to only choose the perfect numbers in my range.
so far I have...
for number in range (1,1001):
print number
Yea I know this is pretty bad but this is new to me.
Can anyone explain how to make the program choose the perfect numbers from the list?
Posts: 1,075
Time spent in forums: 4 Weeks 1 Day 4 h 41 m 27 sec
Reputation Power: 98
first solve simpler problem
Code:
# first solve simpler problem
def odd(n):
'''
return True if n is odd, otherwise return False
exercise these tests using command
SHELL_PROMPT$ python -m doctest -v this_file_name.py
>>> odd(4)
False
>>> odd(19)
True
'''
# your code fits here!
print('Odd numbers less than 10')
for number in range(10):
if odd(number):
print(number)
Posts: 1,075
Time spent in forums: 4 Weeks 1 Day 4 h 41 m 27 sec
Reputation Power: 98
Then perhaps step up to perfect numbers
Code:
#
# A perfect number equals the sum of its proper factors.
#
def factor(n):
'''
return a list of the proper factors of n
SHELL_PROMPT$ python -m doctest -v thisFile.py
>>> factor(2)
[1]
>>> factor(5)
[1]
>>> factor(12)
[1, 2, 3, 4, 6]
'''
# enter your factorization code here.
# At least write down the steps you would take to factor a number by hand.
def perfect(n):
'''
determine if a number is perfect.
return True if it is perfect, return False if it is not.
'''
factors = factor(n)
return sum(factors) == n
print('perfect numbers') # header
for number in range(1,1001): # try all numbers from 1 to 1000
if perfect(number): # if it is perfect,
print(number) # print it.
Posts: 1,075
Time spent in forums: 4 Weeks 1 Day 4 h 41 m 27 sec
Reputation Power: 98
Note
The perfect number search space is small. The brute force approach works. Feel free to find a better algorithm for perfect numbers, and to find a clever method for factorization.