-1

I'm making a project concerned big numbers without BigInteger, BigDecimal etc. I've managed to do all the basics but now I need to add ability to count factorials. My BigNumber stores data as int[] .

Here's a sample solution with BigInteger but I can't use it without having the actual value of my number.

    BigInteger n = BigInteger.ONE;
    for (int i=1; i<=20; i++) {
        n = n.multiply(BigInteger.valueOf(i));
        System.out.println(i + "! = " + n);
    }

So how to count the value ? Add ints from last to first, multiplying tens by 10, hundreds by 100 and so on and so on and store it as long ?

Source of BigInteger : http://developer.classpath.org/doc/java/math/BigInteger-source.html

Kevin Bourrillion
  • 40,336
  • 12
  • 74
  • 87
owca
  • 1
  • 1
  • 4
    Maybe it's just me, but I don't really understand this question. – Roman Jan 20 '10 at 04:11
  • The question is also not clear to me. It looks like the OP wants to store the integer in an array with each element of the array corresponding to each digit (I might be wrong too) – sateesh Jan 20 '10 at 04:18
  • @sateesh Oh you're right. That's what it seemed like to me too, but I didn't quite understand the role that BigInteger or the code sample played in the question. I just re-read it and it looks like the code is unrelated. – Roman Jan 20 '10 at 04:47
  • @owca You need to clarify your question. If you want to know how BigInteger.valueOf works, just read the source (the link you provided). If you don't understand something in the source, be specific about what you don't understand. If you're asking about how to convert an array of digits to a number, ask that. Your question was vague and all over the place. – Roman Jan 20 '10 at 04:52
  • @owco P.S. If you make changes, edit this question. Don't make a new question - I believe that is frowned upon here. – Roman Jan 20 '10 at 04:58
  • Gotta give the guy credit for posting in English - apparently not his native language. I can't even say "hi" in Polish. So he 'managed to do all the basics', and now wants to 'count' (=compute) factorials. Yet he seems to think the result will be a long integer. Back to the basics! Everything is a 'BigNumber' – gary Jan 20 '10 at 05:04

1 Answers1

0

So how to count the value ? Add ints from last to first, multiplying tens by 10, hundreds by 100 and so on and so on and store it as long ?

I don't think storing it as a long is what you intend. What would happen if the value gets bigger than Long.MAX_VALUE?

If n is a BigInteger, then n.multiply(BigInteger.valueOf(i)); should return a BigInteger object. The multiply method should know how to do multiplication with two BigInteger objects without needing to convert them to long. One way of doing that is to loop through each digit using the multiply-and-carry algorithm that we learned in grade school. This will be pretty slow if your values are astronomical, but it does have the benefit of being easy to understand and implement.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880