Click here to Skip to main content
15,915,093 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hello every one..
i am trying to make a console programe in c# in which i want to calculate the product of divisors of a number.
for example the number is 12 then its divisors are 2, 3, 4 ,6. then output should be 2*3*4*6=144.
Here is my code..
C#
using System;
using System.Text;
namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.Clear();
      ulong b=0; ulong c = 1;
      b = Convert.ToUInt32(Console.ReadLine());
      for (ulong i = 2; i <=b/2; i++)
      {
        if (b % i == 0)
        {
          c = c * i;
        }
      }
      Console.WriteLine(c);
    }
  }
}

Now my problem is that i want to run this programe for large numeric numbrs like 10000 or 100000 or 2-3 lakhs may be.
but it gives correct output only for small numbers. it does not give correct output for large number like 10000 whose product of divisors will be very large.
i tried to use high data types but its not giving correct output.

what is going wrong...
Please programersss Help me...........!!!
Posted
Updated 22-Jun-14 19:59pm
v2
Comments
Bh@gyesh 23-Jun-14 2:08am    
Hi, Use System.Numerics.BigInteger. Mau be it can solve your problem. :)

Please see this type: http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx[^].

Be careful with this type: it's way too easy to create a number which needs more memory than all available memory in your system. By the way, this is a useful simple exercises in elementary mathematics and programming.

—SA
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 23-Jun-14 2:12am    
Perfect link :-)
The problem is that you start getting to very, very large products, very, very quickly:
Look at the simple and obvious divisors of 512:
256, 128, 64, 32, 16, 8, 4, 2

Multiply them together:
256 * 128 * 64 * 32 * 16 * 8 * 4 * 2 =   68719476736
                                     or 0x1000000000
Which alreadydoesn't fit into a 32 bit integer!
Have a look at the divisors table: http://www.positiveintegers.org/IntegerTables/501-600[^] and you will see that some of these values can have 20 or more divisors!
These numbers get big quickly and just getting to 10,000 or worse 100,000 is going to take considerable time and generate some incredibly large numbers.

Yes, you can do it - but you will need BigInteger rather than ulong and there isn't going to be much you can do with the numbers once you have generated them!

I don't know why you are doing this, but to be honest: I wouldn't bother!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900