Computer Science Canada

too big numbers

Author:  cool dude [ Tue Jun 13, 2006 7:31 pm ]
Post subject:  too big numbers

a question that i'm doing is this:

Quote:

Problem 20

n! means n × (n − 1) × ... × 3 × 2 × 1

Find the sum of the digits in the number 100!



the problem with this is that the sum is too big(unless i did something wrong) thus it keeps displaying 0. is there any way to fix that?

code:

public class Problem20 {
        public static void main (String[] args){
                int n = 100;
                int total = 100;
                int sum = 0;
               
                for (int i = 1; i <= 99; i++){
                        total = total * (n-i);
                        }
                for (int j = 1; j <= total; j++){
                        sum = sum + j;
                }
                System.out.println(sum);               
        }
}

Author:  wtd [ Tue Jun 13, 2006 9:09 pm ]
Post subject: 

BigInteger

Author:  cool dude [ Tue Jun 13, 2006 10:21 pm ]
Post subject: 

i'm running into too many problems with BigInteger because i don't really know how to use it. i tried it and then it said i can't use operators with BigInteger Confused i know i'm solving the question incorrectly because the answer shouldn't be this big but it doesn't really matter i'm now more interested in knowing how to use BigInteger. was there a tutorial done on BigInteger?

Author:  codemage [ Wed Jun 14, 2006 8:46 am ]
Post subject: 

BigInteger is a class, so it doesn't work the same way that integer primitives do.

See the methods here for common operations:

http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigInteger.html

Author:  [Gandalf] [ Wed Jun 14, 2006 6:10 pm ]
Post subject: 

Why link to the Java 1.4.2 documentation? Whenever possible, you should be using the most modern version of documentation/code. Here's the 5.0 documentation for BigInteger:
http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigInteger.html


: