473,507 Members | 5,060 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can i find the sum of divisors of a given number in a faster way?

6 New Member
I m solving a problem to find the sum of divisors of a given number(the number is a large one),...i hv written the code bt it's exceding the time limit...hw can i reduce the time in such cases...pls help me....i m a beginner 2 programming in c :)
Hre is my code:

#include<stdio.h>
int main()
{
long long int k,n,t,sum=0;
scanf("%lld",&t);
while(t)
{
scanf("%lld",&n);
for(k=1;k<n;k++)
{
if(n%k==0)
{
sum=sum+k;
}
}
printf("%lld\n",sum);
t--;
sum=0;
}
system("pause");
return 0;
}
Mar 19 '10 #1
15 20570
YarrOfDoom
1,247 Recognized Expert Top Contributor
There's a lot of numbers you can easily skip here, you just have to think about the maths behind it.
Think about this for instance: "What's the highest divisor I'm ever going to find for a number (excluding the number itself of course)?"
If you can calculate this number (or an approximation of it) before the loop, you only have to go up to this number, instead of all the way up to the given number.
I'm sure if you look into it a little bit further you can find other ways to speed it up as well.

PS: If you post your code in code-tags (put [code] in front of your code and [/code] behind it), it is much easier to read for everyone. More posting guidelines can be found here.
Mar 19 '10 #2
donbock
2,426 Recognized Expert Top Contributor
Remember that divisors come in pairs. Every time you find a divisor with your loop you can then find its partner. Then your loop only needs to find half the divisors.
Mar 20 '10 #3
shivpratap
6 New Member
@yaarofdoom

Thanks,this was my 1st post i dis forum,ill tk care abt it next time...btw i know i can cutshort this loop if replace n with n/2 in the loop......bt still it's consuming more time.....ny better algorithm i can use???.....pls help

@donbock

Thanks can u suggest a better algorithm for this 1??
Mar 20 '10 #4
shivpratap
6 New Member
@yaarofdoom

Thanks,this was my 1st post i dis forum,ill tk care abt it next time...btw i know i can cutshort this loop if replace n with n/2 in the loop......bt still it's consuming more time.....ny better algorithm i can use???.....pls help

@donbock

Thanks can u suggest a better algorithm for this 1??
Mar 20 '10 #5
newb16
687 Contributor
...i dis forum,ill tk care abt it
There are 3 words I understand (forum, care, it) and 5 that I either don't know or that look misplaced in this context ( 'i' and 'ill'). Maybe it's not that important, though.
I'd find all prime factos of the number in question ( with their respective powers) and then compute all possible divisors from them. ( if n = p_1^k_1 * ... * p_n^k_n , you have (k_1+1)*...*(k_n+1) possible divisors.
Mar 20 '10 #6
shivpratap
6 New Member
@yaarofdoom

Thanks,this was my 1st post i dis forum,ill tk care abt it next time...btw i know i can cutshort this loop if replace n with n/2 in the loop......bt still it's consuming more time.....ny better algorithm i can use???.....pls help

@donbock

Thanks can u suggest a better algorithm for this 1??
Mar 20 '10 #7
shivpratap
6 New Member
@yaarofdoom

Thanks,this was my 1st post i dis forum,ill tk care abt it next time...btw i know i can cutshort this loop if replace n with n/2 in the loop......bt still it's consuming more time.....ny better algorithm i can use???.....pls help

@donbock

Thanks can u suggest a better algorithm for this 1??
Mar 20 '10 #8
YarrOfDoom
1,247 Recognized Expert Top Contributor
Could you please stop copy-pasting the same post over and over again?
How about you show some of the efforts you have done yourself to implement the suggestions we have given you, so we can help you on that?
And putting some more effort into your spelling would be greatly appreciated as well (maybe you should read the posting guidelines again).
Mar 20 '10 #9
donbock
2,426 Recognized Expert Top Contributor
As you said, you could limit your search for divisors to the range from 1 to n/2. However, you can do even better. Consider your divisor search ... you start with 1 and find its partner (n), then find the next divisor (i) and its partner (n/i). Notice that the partners are larger than the initial divisors: n > 1, n/i > i, etc. As your divisors increase the partners decrease. You continue looking for divisors until the partner is less than or equal to the divisor. After that, all you're doing is finding another instance of a pair you already found.

So ... the trick for deciding when to look for divisors is when the partner is less than or equal to the divisor. Think about the arithmetic for awhile -- how can you characterize this transition point?
Mar 21 '10 #10
shivpratap
6 New Member
I have improved my code a lot now but is it possible to reduce its time further without making a major change in this algorithm??....pls suggest...

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include<stdio.h>
  3. #include<math.h>
  4. int main()
  5. {  
  6.     long long int n,t,i,sum=0,k;
  7.     scanf("%lld",&t);
  8.     while(t)
  9.     {
  10.     scanf("%lld",&n);
  11.      k =(sqrt(n));
  12.      if(n>=1 && n<=500000)
  13.      {
  14.     for(i = 2;i<=k; i++)
  15.     {
  16.         if (n % i == 0)
  17.             sum = sum + i +( n / i);
  18.         if(n/i==i)
  19.         {sum=sum-i;
  20.         }    
  21.     }printf("%lld\n",sum+1);
  22. }
  23.  
  24.     sum=0;
  25.     t--;
  26.     }
  27.     system("pause"); 
  28.     return sum;
  29. }
  30.  
  31.  
Mar 21 '10 #11
newb16
687 Contributor
Well, you can move
Expand|Select|Wrap|Line Numbers
  1. if(n/i==i)
  2.      sum=sum-i;
somewhere outside the loop so that it doesn't check it at each iteration.
But without change in algorithm, if given a number that is a product of powers of several small prime numbers, it will check every possible divisor upto sqrt(n).
Mar 21 '10 #12
donbock
2,426 Recognized Expert Top Contributor
I have a couple of questions regarding the requirements ...

I notice that for a given number n, you don't include divisors 1 and n in the sum. Is that what you want?

I also notice that if a divisor appears twice (for example, 64 = 8*8), you only include the divisor in the sum once. Is that what you want?
Mar 22 '10 #13
jkmyoung
2,057 Recognized Expert Top Contributor
My suggestion: Prime factorize

1. Speed.
a. Check far less numbers, as you can divide out the prime factor with each check. You only have to check as far as Min(2nd greatest prime factor, sqrt(greatest prime factor)) which is a huge time reduction.
b. Calculating the sum involves fewer operations; uses multiplication as opposed to addition.
c. Since you divide out the 2's from the beginning, you only have to check every 2nd number after 2.
2. More complicated to understand.

If you prime factorize the number into primes, p1, p2, p3, ....
Where
n = p1^i1 * p2^i2 * p3^i3 ...

All the divisors are in the form
p1^j1 * p2^j2 * p3 ^ j3 ....
where
0 <= j1 <= i1,
0 <= j2 <= j2,
....

To get the sum of the divisors, you can take each prime seperately, giving you:
[p1^(i1+1)-1]/(p1-1) * [p2^(i2+1)-1]/(p2-1) *[p3^(i3+1)-1]/(p3-1) * ....

So when you find a factor, find out how many times it goes into it.
Say the current sum of divisors is 11, and then you find 3 goes into the number. If it goes in 2 times, then change your current sum like so:

11 *= [3^(2+1) - 1]/(3-1)
=> 11 *= 8/2
=> 44

Note ^ stands for exponent in this case, eg Math.pow or whatever you have to use (although it's not necessary if you do it smartly.)
===
In summary, find the prime factors of the number, and divide them out from the number, so you don't have to deal with them anymore. If you use this method, you shouldn't add both the factor and the opposite divisor like you are doing now. Can show a better example if you want, but it will take some room, and it's not worth it if you don't want to use this method.
====
Mar 22 '10 #14
donbock
2,426 Recognized Expert Top Contributor
A brute force technique for finding all the combinations of the prime factors is illustrated by the following example:

Consider the number 24. Its prime factors are 3, 2, 2, and 2. Build a table like this:
Expand|Select|Wrap|Line Numbers
  1. 3 2 2 2 |
  2. --------+----
  3. 0 0 0 0 |  1
  4. 0 0 0 1 |  2
  5. 0 0 1 0 |  2
  6. ...
  7. 0 1 1 1 |  8
  8. 1 0 0 0 |  3
  9. ...
  10. 1 1 1 0 | 12
  11. 1 1 1 1 | 24
where the left N columns (where N is the number of prime factors) count in binary from 0 to 2^N-1; and the rightmost column is computed by starting with "1" and multiplying by each prime factor whose counting bit is 1.

Discard all duplicates and you have a list of all the factors of the starting number.
Mar 22 '10 #15
jkmyoung
2,057 Recognized Expert Top Contributor
Just to show the algorithm works on 24:
Start with 1 as a factor.
n = 24
i = 2! i divides n.
n => 12 (2^1)
n => 6 (2^2)
n => 3 (2^3)

This gives us (1, 2, 4, 8)
Using the formula, we go:
(2^4 -1)/(2-1) = 15/1 = 15
1 * 15 = 15. So the sum so far is 15. = 1 + 2 + 4 + 8. Check!

Next i = 3. 3 divides n.
n=> 1 (3^1)
Ok, so we have factors
(1, 2, 4, 8) * 1, and (1, 2, 4, 8) * 3
(3^2 - 1)/(3-1) = 8 /2 = 4
15 * 4 = 60
The sum of factors of 24 is 60.
Checking, we have factors
1, 2, 4, 8, 3, 6, 12, 24, rearrange to:
1, 2, 3, 4, 6, 8, 12, 24 => 60.

adv/disadv:
+ Don't have to check for duplicates.
- Complexity.
Mar 22 '10 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

5
2124
by: andrea.gavana | last post by:
Hello NG, I was wondering if there is a faster/nicer method (than a for loop) that will allow me to find the elements (AND their indices) in a list that verify a certain condition. For example,...
5
3213
by: Philp Smith | last post by:
Hi Does anyone have suggested code for a compact, efficient, elegant, most of all pythonic routine to produce a list of all the proper divisors of an integer (given a list of prime...
1
459
by: Sriram Krishnan [at] gmx [dot] net | last post by:
Given an arbitrary number series, how do I identify the series and find the Nth number... for e.g 1,3,5,7 is an AP and the next number will be 9 1,8,27,64 is the series of cubes and the next will...
8
74877
by: J. B. Moreno | last post by:
What's the best (i.e. fastest) way to find out if an array contains a given value? Other than looping, the only way I know to do it is to use an associative array/hash instead.... Is there a...
24
2185
by: shafique | last post by:
Hello, Can anybody have idea as how to find it there exist consective one's in a word/dword using bitwise operatiors in C language. Obviously this can be done by shifting and looping one by...
6
8947
by: Jozef Jarosciak | last post by:
Quickest way to find the string in 1 dimensional string array! I have a queue 1 dimensional array of strings called 'queue' and I need a fast way to search it. Once there is match, I don't need...
21
8118
by: Imran | last post by:
I have a vector of integers, such as and I want to find out the number which occurs most frequently.what is the quick method. My array size is huge. what I am doing is 1. find out the...
1
1760
by: Clint Boaz | last post by:
I am just trying to learn C++ and having a problem trying to write a program that finds the sum of the proper divisors of a given integer n and returns whether n is deficient, perfect or abundant.
1
1536
by: \A_Michigan_User\ | last post by:
I call this Javascript function with "GetEleNum(this)" passing it 1 of the elements of a text-box array. Is there a QUICK way to get the "element #"? I'm currently using this SLOW code. (It...
3
2761
by: jimix | last post by:
I have written the program for factors of #'s 1-50 but the factors print out below the number being factored. I can't figure out how to make it all on the same line. i.e. (now) divisors of 25 are...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7110
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5041
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.