Thursday, April 12, 2012

How can i extend data type in java

How i can create my own data type in java that could store a 16-byte integer value



the longest data-type in java by size is "long" which is 8-byte and it could store 19-digit integer value but, i want to find the factorial of 25 and the factorial of 25 is 26-digit(15511210043330985984000000). now the problem is i have no such a data-type in java that could store such huge value of 26-digits or more.



if there is any



public long factorial(int number)
{
int i=1;
long factorial=1;

for(i=1;i<=number;i++)
{
factorial = factorial * i;
}
return factorial;
}




2 comments:

  1. Now the problem is i have no such a data-type in java that could store such huge value of 26-digits or more.

    Have you looked at java.math.BigInteger? Note that this is a class (a reference type) rather than a value type, but it's immutable which means you can think of it as being somewhat value-like.

    ReplyDelete
  2. now the problem is i have no such a data-type in java that could store such huge value

    That's not quite true. There is BigInteger.

    See this answer for an implementation of factorial that uses BigInteger.

    ReplyDelete