Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Friendly Numbers
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
bigdaddyHess




PostPosted: 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
Sponsor
sponsor
Tony




PostPosted: 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)
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Prince




PostPosted: 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




PostPosted: Thu Apr 10, 2003 2:06 pm   Post subject: thnx

thnx tony
your answer is ownage
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: