Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming
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.

Learn More!


Download to Enter
| Contest Rules

Tutorials | Forums

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 9th, 2012, 01:30 PM
Jack2311 Jack2311 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 30 Jack2311 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Thanks

Reply With Quote
  #2  
Old February 9th, 2012, 09:17 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Click here for more information.
 
Join Date: Aug 2011
Posts: 1,075 b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
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)

Reply With Quote
  #3  
Old February 9th, 2012, 09:28 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Click here for more information.
 
Join Date: Aug 2011
Posts: 1,075 b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
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.

Reply With Quote
  #4  
Old February 9th, 2012, 10:01 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Click here for more information.
 
Join Date: Aug 2011
Posts: 1,075 b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level)b49P23TIvg User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
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.

Reply With Quote
  #5  
Old February 10th, 2012, 06:51 AM
Jack2311 Jack2311 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 30 Jack2311 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 40 m 4 sec
Reputation Power: 1
Ok, I think I see how to put it together now. I am going to try it again this weekend and see what I can come up with.

Thanks

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Finding perfect numbers


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap