Friendly Numbers
Author |
Message |
bigdaddyHess
|
Posted: Wed Apr 09, 2003 2:45 pm Post subject: Friendly Numbers |
|
|
how can i print a list of friendly numbers (up to 20000 btw)?
two numbers are friendly is the sum of factors of one is equal to the other number
for eg. 220 & 284
sum of factors of 220 is equal to 284
and sum of factors of 284 is equal to 220 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Tony
|
Posted: Wed Apr 09, 2003 2:51 pm Post subject: (No subject) |
|
|
the simpliest way I can think of is start finding sums of factors of the first number. Then take the sum, add up its factors and see if it equals to original number or not.
code: |
for i:1..20000
sum:=sumoffactors(i) %sumoffactors is an outside function
sum2:=sumoffactiors(sum)
if sum2 = i then
put i, " and ", sum, " are friendly numbers"
end if
end for
|
all thats left is writing a function to addup the factors. I actually think someone already posted it somewhere on this board. (try using search feature) |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Prince
|
Posted: Wed Apr 09, 2003 5:58 pm Post subject: (No subject) |
|
|
code: |
function properFactors (n : int) : int
var x, y, temp : int := 0
for decreasing i : n .. 1 by 1
x := n mod i
if x = 0 then
y := n div i
if y not= n then
temp := temp + y
end if
end if
end for
result temp
end properFactors
|
no searching involved |
|
|
|
|
|
bigdaddyHess
|
Posted: Thu Apr 10, 2003 2:06 pm Post subject: thnx |
|
|
thnx tony
your answer is ownage |
|
|
|
|
|
|
|