Is there an efficient way to find divisors of large numbers using C++?

  • Context: C/C++ 
  • Thread starter Thread starter smslca
  • Start date Start date
  • Tags Tags
    Code
Click For Summary

Discussion Overview

The discussion revolves around finding efficient methods to compute the divisors of large numbers using programming, particularly in C or C++. Participants explore various algorithms, code snippets, and efficiency considerations related to divisor calculation.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant mentions a function from Wolfram and Pari that can list all divisors of a number, expressing a need for a fast algorithm to compute divisors for very large numbers.
  • A code snippet is provided that lists divisors by checking each number up to half of the input number, but it is noted that this method is not optimal.
  • Another participant critiques the initial code, suggesting that the loop should only check up to the square root of the number instead of half, citing efficiency concerns for large numbers.
  • A recursive approach to factorization is proposed, which involves finding the lowest factor greater than one and calling the function recursively with the quotient.
  • One participant expresses a need to store the divisors generated, indicating a focus on both the quantity and the actual values of the divisors.
  • Another participant mentions the advantages of using C++ for this task, particularly with its dynamic array capabilities.

Areas of Agreement / Disagreement

Participants express differing views on the efficiency of various algorithms for finding divisors. There is no consensus on a single optimal method, as multiple approaches are discussed, each with its own merits and drawbacks.

Contextual Notes

Some participants highlight the limitations of the proposed methods, such as inefficiencies in the initial code and the need for better storage solutions for the divisors. The discussion reflects ongoing exploration rather than settled conclusions.

Who May Find This Useful

Readers interested in programming, algorithm optimization, number theory, and those specifically looking for methods to compute divisors in C or C++ may find this discussion beneficial.

smslca
Messages
63
Reaction score
0
As I saw in wolfram website and in pari , if given divisors(x) , it will give all the divisors of x in a row.
ex: divisors (34567) = { 1, 13, 2659, 34567 }
So I we know some x = 2^5 * 5^15 * 21^5 * 119 , Here we even know the prime factors
Then,
Is there any program in c language (or) any other easily understood computer programing language (so that I could convert into a c program using the same logic , since I know only c language programing) , (or) any easy algorithm to form
divisors at very fast speeds (because I require to form divisors for very large numbers) .
If anyone have the code please give me , or any step wise explained algorithm is also sufficient.

Here I can formulate an algorithm by observing the factors of the number (i.e using the arrangement of factors in ascending or descending order), but I think ,this method is too slow to form the divisors for very very large numbers having a lot and lot of divisors in it.
 
Technology news on Phys.org
Heres the code :D

Heres the code, enjoy :D

Code:
#include <stdio.h>
#include <conio.h>

int main()
{
long int n, d=2;
printf("Enter the number:");
scanf("%d", &n); 
printf("\n\n The divisor(s) : { 1 ");
while(d<= n/2)
 {
  if(n%d==0) printf("%d ", d);
  d++;
 }
printf("%d } \n", n);
getch();
return(0);
}
 
smslca said:
As I saw in wolfram website and in pari , if given divisors(x) , it will give all the divisors of x in a row.
ex: divisors (34567) = { 1, 13, 2659, 34567 }
So I we know some x = 2^5 * 5^15 * 21^5 * 119 , Here we even know the prime factors
Then,
Is there any program in c language (or) any other easily understood computer programing language (so that I could convert into a c program using the same logic , since I know only c language programing) , (or) any easy algorithm to form
divisors at very fast speeds (because I require to form divisors for very large numbers) .
If anyone have the code please give me , or any step wise explained algorithm is also sufficient.

Here I can formulate an algorithm by observing the factors of the number (i.e using the arrangement of factors in ascending or descending order), but I think ,this method is too slow to form the divisors for very very large numbers having a lot and lot of divisors in it.

The code posted above should work but it is by no means optimal.

In fact the current thinking in finding prime factors for a large number is that there is no decent algorithm that will factorize large numbers in a decent time.

The RSA algorithm uses this property to enforce the strength of its public key cryptography system.

If you end up finding a fast method that decomposes an arbitrary large number into primes, then you will be famous and probably considered dangerous as well ;)
 
Its very very useful . thanks for giving the code
 
JackCrow's solution, in theory, could fail for numbers < 4, and will be atrociously inefficient for large numbers. It's better to replace "while(d<= n/2)" with "d <= (long int)sqrt((double)d)". Consider the case d=100. It is obvious that the two largest factors for all d is sqrt(d), thus this loop need only execute 10 times, at most. With d/2 as your exit condition, you're checking 11-50, which is completely unnecessary. We can do some math and find just how ineffiecient if is, given a general d. The sqrt d is what is necessary and all more than that in not, so we'll d/2 - sqrt(d). Think of very large numbers, such as 2^16, that would be 2^15 - 2^8 extraneous iterations.

Another way to consider would be recursion. Although sometimes a little more inefficient than iteration, recursion is almost always more fun. A way you could do this with recursion would be to find the lowest factor >1 in the argument, then call the function recursively with the previous argument divided by the factor. Like so:
Code:
#include <stdio.h>
#include <math.h>

void factor(unsigned long long n);

int main(int argc, char **argv)
{
     factor(4896542);
     return 0;
}

void factor(unsigned long long n)
{
     for(unsigned long i = 2;i <= (unsigned int)sqrt((double)n);i++)
          if(n % i == 0)
          {
               printf("%u ", i)
               factor(n / i);
               break;
          }
}
The only problem is that it doesn't print 1, if you want it to, add it in main.
 
TylerH said:
JackCrow's solution, in theory, could fail for numbers < 4, and will be atrociously inefficient for large numbers. It's better to replace "while(d<= n/2)" with "d <= (long int)sqrt((double)d)". Consider the case d=100. It is obvious that the two largest factors for all d is sqrt(d), thus this loop need only execute 10 times, at most. With d/2 as your exit condition, you're checking 11-50, which is completely unnecessary. We can do some math and find just how ineffiecient if is, given a general d. The sqrt d is what is necessary and all more than that in not, so we'll d/2 - sqrt(d). Think of very large numbers, such as 2^16, that would be 2^15 - 2^8 extraneous iterations.


Thanks for modifying the code.
I just wanted to know how to store the divisors.

I am working on factorization of numbers. So I got a problem where I have to form the divisors for an another number (another number = given number - some number), for which I can get the factors easily.

ex: say I got factors of an another number as 2^32 * 3^12* 5^10 * 383 * 1151
Here I knew I will get 33*13*11*2*2 = 11876 divisors

But the problem is , not the number of divisors , it is what are the divisors and how can I store them.

Sincerely , I am not expecting the above given codes. But on watching them I got some Idea about the formation of divisors.
 
This is where using C++ is nice, with it's vectors and such. There is no way easier than using an array, and having to dynamically resize it for each new factor.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
Replies
69
Views
11K
Replies
14
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 30 ·
2
Replies
30
Views
5K
Replies
1
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K