Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 too big numbers
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cool dude




PostPosted: 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);               
        }
}
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue Jun 13, 2006 9:09 pm   Post subject: (No subject)

BigInteger
cool dude




PostPosted: Tue Jun 13, 2006 10:21 pm   Post subject: (No 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?
codemage




PostPosted: Wed Jun 14, 2006 8:46 am   Post subject: (No 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
[Gandalf]




PostPosted: Wed Jun 14, 2006 6:10 pm   Post subject: (No 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
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: