|
||||||
| Python Discussion forum for Python, a high-level language with simple syntax, but yet powerful. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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
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. |
| Sponsored Links |
|
|
|
|||||
|
@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. |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 |
| 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 |