Results 1 to 5 of 5
  1. #1
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Dealing with really big numbers

    I need to find 100!, that is, 100 * 99 * 98 * 97, etc.

    I have set up my code, but at *90 the product is 8704899442529685504. And then at 89 it is: -27200710659158016. I guess this is because it's too big for long? So how can I get my really big number?

    Cheers!

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Dealing with really big numbers


  3. #3
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Dealing with really big numbers

    Yeah, been looking at biginteger, but I can't get it to work. Cannot find a decent example of anyone using it either.

    I want to replace w with a biginteger:
    Java Code:
    public class Problem020 {
    	public static void main (String[] args){
    		long w, x, y, sum;
    		int z;
    		w=100;
    		x=99;
    		do {
    			w*=x;
    			System.out.println("x: " + x + "\tw: " + w);
    			x--;
    		}	
    		while(x>0);
    		String Prod = Long.toString(w);
    		int len=Prod.length();
    		for (z=0;z<len;z++){
    			char char1 = Prod.charAt(z);
    			System.out.print(char1);
    		}
    	}
    }
    But I haven't seen any method for multiplying a big integer by an int, only by another big integer. But bigintegers are immutable so that's pretty awkward. Can anyone explain to me how to get a big integer to do the same thing that w is doing? (Continuously multiplying itself by x)?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Dealing with really big numbers

    Why not just create a loop starting with BigInteger.ONE?

    Java Code:
    BigInteger total = BigInteger.ONE;
    for(BigInteger i = BigInteger.ONE; /* comparison here, check api for method */; i = i.add(BigInteger.ONE)){
        //multiply total by i
    }

  5. #5
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Dealing with really big numbers

    Thanks sunde! My code works now!

    Java Code:
    import java.math.BigInteger;
    
    public class Problem020 {
    	public static void main (String[] args){
    		long x;
    		int z, b = 0;
    		BigInteger bi1 = new BigInteger("100");
    		x=99;
    		
    		do 
    		{
    		BigInteger bi2 = BigInteger.valueOf(x);
    		bi1 = bi1.multiply(bi2);
    		x--;
    		}
    		
    		while(x>0);
    		
    		System.out.println("100! is: " + bi1);
    		String Prod = (bi1).toString();
    		int len=Prod.length();
    		
    		for (z=0;z<len;z++){
    			char char1 = Prod.charAt(z);
    			int number = Character.digit(char1,10);
    			b += number;
    		}
    		System.out.println("\nThe sum of the digits of 100! is: " + b);
    	}
    }

Similar Threads

  1. Dealing with DatagramSocket
    By fabiolanza in forum Advanced Java
    Replies: 1
    Last Post: 04-07-2012, 02:14 AM
  2. Dealing With Videos Java
    By anthropamorphic in forum New To Java
    Replies: 15
    Last Post: 07-05-2011, 06:20 AM
  3. Dealing with IndexOutOfBoundsExceptions
    By MrDave in forum New To Java
    Replies: 4
    Last Post: 06-19-2010, 01:33 AM
  4. dealing with (zip) files
    By aloula in forum Advanced Java
    Replies: 4
    Last Post: 10-06-2009, 07:32 PM
  5. Dealing with iReport 3.0.0
    By HotEvilGirl in forum New To Java
    Replies: 6
    Last Post: 09-11-2009, 11:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •