Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Delphi/Python

Delphi/Python Forum for discussing Borland Delphi and Python coding techniques, tips and tricks. Ask your python questions here.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-15-2008, 04: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 04: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, 12:10 AM
v0id's Avatar   
v0id v0id is offline
Super Mod
 
Join Date: Apr 2007
Location: Denmark
Posts: 1,876
Last Blog:
CherryPy(thon)
Rep Power: 23
v0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to all
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-16-2008, 02:27 PM
Xav's Avatar   
Xav Xav is offline
Guru
 
Join Date: Mar 2008
Location: London, England
Posts: 2,969
Last Blog:
Piano Exam
Rep Power: 25
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Default Re: Amicable numbers

And not to mention it will freeze up whilst it's doing it...
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-17-2008, 12:15 AM
v0id's Avatar   
v0id v0id is offline
Super Mod
 
Join Date: Apr 2007
Location: Denmark
Posts: 1,876
Last Blog:
CherryPy(thon)
Rep Power: 23
v0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to all
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-17-2008, 11:04 AM
Xav's Avatar   
Xav Xav is offline
Guru
 
Join Date: Mar 2008
Location: London, England
Posts: 2,969
Last Blog:
Piano Exam
Rep Power: 25
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Default Re: Amicable numbers

I thought it blocks the calling thread? I was referring to the console.
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
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, 01:20 PM
v0id's Avatar   
v0id v0id is offline
Super Mod
 
Join Date: Apr 2007
Location: Denmark
Posts: 1,876
Last Blog:
CherryPy(thon)
Rep Power: 23
v0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to all
Send a message via MSN to v0id
Default Re: Amicable numbers

I thought you meant freezing like in not running.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-17-2008, 01:27 PM
Xav's Avatar   
Xav Xav is offline
Guru
 
Join Date: Mar 2008
Location: London, England
Posts: 2,969
Last Blog:
Piano Exam
Rep Power: 25
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
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.
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 05-17-2008, 04:57 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Mod
 
Join Date: Jul 2006
Age: 35
Posts: 1,751
Last Blog:
Game software (GURPS)
Rep Power: 24
WingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to all
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
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
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

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


All times are GMT -5. The time now is 10:51 PM.

Contest Stats

John ........ 87.50000
dargueta ........ 75.00000
Xav ........ 50.00000
MeTh0Dz ........ 20.00000
gaylo565 ........ 18.00000
Johnnyboy ........ 3.00000

Contest Rules

Ads