-4

the number is n = 2747502308387844992 count = 0 Normal ways like using for loop is not working.

for(i=1;i<n;i++)
{
    if(n%i == 0 ){count++;}
}
System.out.println(count%(Math.pow(10,9)+7));

output to be printed is 10240. suggest me the other way which is effiecient. Please try the solution in IDE before putting here.

Ramcharan Reddy
  • 621
  • 1
  • 5
  • 18

2 Answers2

2

You could devide the number into partitions of certain ranges and then use threads to find the divisors of those intervals and add them to an ArrayList.

Let's say you want to find the divisors of 10. You could split it into the intervals of [1,5] and [6,10]. Then use 2 threads to parallel calculate the divisors.

You could also use a thread pool. Create a class which implements the Runnable interface and add a single number to the constructor. Use the run method to calculate the divisor and add it to a shared list.

Bastian
  • 1,553
  • 13
  • 33
  • 1
    Nice thinking @Bastian Schoettle Makes some sence. thanks. – Ramcharan Reddy Jun 23 '16 at 05:26
  • No worries! I'm sure that my first solution will be faster because you could speed up the calculation process by the number of threads. Let's say it would take 2 Minutes to calculate the divisors in a single thread, if you use 4 threads it would only take 30 seconds. This is of course depending on our system. – Bastian Jun 23 '16 at 06:02
0
int end = (int) Math.sqrt(x);
for(i=1;i<end;i++)
{
    if(n%i == 0 )
    {
          count=count+2;
    }

}
if(n%end == 0 && n*n==end){count=count+1;}
if(n%end == 0 && n*n!=end){count=count+2;}
System.out.println(count);
Ramcharan Reddy
  • 621
  • 1
  • 5
  • 18
  • `if(n%end == 0 ){count=count+1;}` This will add one even if the number isn't a perfect square e.g. 10's divisors are 1, 2, 5, 10 but since that's an even number (you always add 2 so `count` will always be even) but there are only 4 divisors. – Arc676 Jun 12 '16 at 12:01
  • have you really tried for my input.... even this method taking lot of time and giving output as 64 which is not correct. – Ramcharan Reddy Jun 12 '16 at 12:28
  • i gave you an idea , try to implement it using BIGDECIMAL – hacene abdessamed Jun 12 '16 at 12:29
  • great idea for small numbers like 10,20 but the question completely about big numbers like the number that i mentions.. whats the point in your answer – Ramcharan Reddy Jun 12 '16 at 12:37
  • you should iterate until square(n) , it is more more rapide than the first methode – hacene abdessamed Jun 12 '16 at 12:39