![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
Sum of Divisors
I'm looking for a way to write an efficient method that will return the sum of all the divisors of a number. For example, sigma(12) = 1 + 2 + 3 + 4 + 6 = 16.
So far all I can come up with is testing whether the number is divisble by each number from 2 to N/2. However, takes way too long on extremely large numbers. Can anyone help me out with a more efficient method? Thanks. |
|
|
|
|
|
#2 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
well, up to N/2 would be a little extreme, since 2 would yield whether that is a divisor. You'd only have to to up to sqrt(N), as anything less would get you the pair of divisors.
|
|
|
|
|
|
#3 | |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#4 |
|
Expert Programmer
|
That seems too complicated. Isn't there an easier (and faster) way?
|
|
|
|
|
|
#5 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
What I meant was, if you divide by 2 and it is a divisor, then N/2 is also one. so there's no reason to go up past sqrt(N), as any divisor > sqrt(N) will have a corresponding divisor < sqrt(N)
:banana: |
|
|
|
|
|
#6 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
but he wants all the devisors...
I don't think there is any easier way. I don't recall any theore for divisors calculation (except for the ones mentionned here already). |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I don't think Jimbo is suggesting that sqrt (N) is the answer; I think he's suggesting that it is a reasonable limit....
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
well every number is divisble by 1 and itself...then you may have unique divisors like 2, 3, 5, and seven. at some point you're going to have to set a limit for the numbers. 1000000 is divisible by 2 and 4 and others...and ten times that is divisible by those and others plus all of those plus ten times that and on and on...maybe you want to just develop an algorithm for finding primes up to that number and multiples of those up to that number and trying those on a case-by-case basis. i'm drunk right now, but this still seems weird...
stack overflow ring a bell?
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#9 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
The guys are right. I wrote a couple of little Python scripts to find out exactly how much faster just counting up to the square root was, and it's rediculous. When finding the factors of, say, one billion (1,000,000,000), the faster script took half a second; the slower is still going, and I started it two minutes ago.
|
|
|
|
|
|
#10 | |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|