Lost Password?


Go Back   CodeCall Programming Forum > Software Development > Python

Python Discussion forum for Python, a high-level language with simple syntax, but yet powerful.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-15-2008, 05:15 PM
makkato makkato is offline
Newbie
 
Join Date: May 2008
Posts: 1
Rep Power: 0
makkato is on a distinguished road
Default Amicable numbers

We have to make a program that finds all the amicable numbers between 1-10000 but I have no idea where to start...I found this pseudo code but it's gibberish to me...any help would be apprieciated. I need to code it in python.

Code:
And the following PseudoCode finds all the Amicable Numbers between two numbers

Procedure Find Amicable Pairs
        Enter Starting Number
        Enter Last Number
        For all the numbers between the Starting Number and Last Number and call this FirstNumber
                Call the Function to add all of the Proper Divisors of the FirstNumber and call this SumOfAllProperDivisorsOfFirstNumber
                Call the Function to add all of the Proper Divisors again this time using SumOfAllProperDivisorsOfFirstNumber and call this SumOfAllProperDivisorsOfSecondNumber
                If SumOfAllProperDivisorsOfFirstNumber is equal to SumOfAllProperDivisorsOfSecondNumber then
                        You found a pair
                End if
        End For Loop
End of Procedure
 
Function Add All Of The Proper Divisors of A Number (call this ANumber)
        Set the initial Running Total to 0
        For all the numbers between 1 and half of ANumber and call this CurrentLoopNumber
                If you divide ANumber with CurrentLoopNumber and the remainder is zero then
                        Add the result to the Running Total
                End If
        End For Loop
        Return the Running Total
End of Function
also I found this....

The following Python language code allows you to check if two numbers are Amicable:
Code:
# Definition of the function
def amicable_numbers(x,y):
    sum_x=0
    sum_y=0
    for i in range(1,x):
        if x%i==0:
            sum_x+=i
 
    for k in range(1,y):
        if y%k==0:
            sum_y+=k
 
    return sum_x==y and sum_y==x
 
# Program body
n_1=int(raw_input('Enter nș 1: '))
n_2=int(raw_input('Enter nș 2: '))
 
if amicable_numbers(n_1,n_2):
    print 'Amicable! :)'
else:
    print 'Not Amicable :('

Last edited by makkato; 05-15-2008 at 05:16 PM. Reason: found something else.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 05-16-2008, 01:10 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,651
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default Re: Amicable numbers

Well, you have a function for checking whether two numbers are amicable or not. Now you only need to test all the numbers between 1 and 10000, which can be done using two simple loops.

I'm a little confused about your question actually, because you say all the amicable numbers between 1 and 10000, but to find amicable numbers you need two numbers.

Well, here you have some code that will find all the amicable numbers where both the first number will go from 1 to 10000, and also the second number.
Code:
def amicable_numbers(x,y):
    sum_x=0
    sum_y=0
    for i in range(1,x):
        if x%i==0:
            sum_x+=i
 
    for k in range(1,y):
        if y%k==0:
            sum_y+=k
 
    return sum_x==y and sum_y==x

for first_number in range(1, 10000+1):
    for second_number in range(1, 10000+1):
        if amicable_numbers(first_number, second_number):
            print "Amicable numbers: %5d and %5d\n" % (first_number, second_number)
You'll have to be patient, though. It will take a long time for it to complete, because it will have to check so many numbers.
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-16-2008, 03:27 PM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: On God's Planet
Posts: 9,852
Last Blog:
Web slideshow in JavaS...
Rep Power: 78
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default Re: Amicable numbers

And not to mention it will freeze up whilst it's doing it...
__________________


Mr. Xav | Website | Forums | Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-17-2008, 01:15 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,651
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default Re: Amicable numbers

Quote:
Originally Posted by Xav
And not to mention it will freeze up whilst it's doing it...
Well, that probably depends on the computer it's running on. I did have it running for some time, and it found numbers, and it didn't freeze at any time.
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-17-2008, 12:04 PM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: On God's Planet
Posts: 9,852
Last Blog:
Web slideshow in JavaS...
Rep Power: 78
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default Re: Amicable numbers

I thought it blocks the calling thread? I was referring to the console.
__________________


Mr. Xav | Website | Forums | Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 05-17-2008, 02:20 PM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,651
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default Re: Amicable numbers

I thought you meant freezing like in not running.
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-17-2008, 02:27 PM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: On God's Planet
Posts: 9,852
Last Blog:
Web slideshow in JavaS...
Rep Power: 78
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default Re: Amicable numbers

No no. But on my computer, anything will freeze.

Once I tried drawing a triangle to the screen, and it ran at about 4fps, as opposed to the usual 200fps.
__________________


Mr. Xav | Website | Forums | Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 05-17-2008, 05:57 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,398
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default Re: Amicable numbers

@makkato: Do you understand what Amicable Numbers are? The variable names in your first pseudocode are a bit lengthy, but the logic looks simple enough.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Random Numbers! PascalPro Visual Basic Programming 4 08-08-2008 05:23 PM
[Request] Numbers Programm! pormadori Visual Basic Programming 4 03-15-2008 02:03 PM
Prime Numbers.. TcM Programming Theory 8 01-15-2008 12:17 PM
complex numbers kenna General Programming 6 11-06-2007 11:32 AM
*!!! HELP: I need a C program that converts words into numbers and vice versa !!!* james24587 C and C++ 2 10-01-2007 12:10 PM


All times are GMT -5. The time now is 04:05 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads