Results 1 to 10 of 10

Thread: BigInteger

  1. #1
    7cardcha is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default BigInteger

    I have some code to add *very* large numbers.

    Java Code:
    import java.math.*;
    
    public class charles {
    	public static void main(String[] args) {
    		BigInteger a1 = new BigInteger("46376937677490009712648124896970078050417018260538");
    		BigInteger a2 = new BigInteger("74324986199524741059474233309513058123726617309629");
    		BigInteger a3 = new BigInteger("91942213363574161572522430563301811072406154908250");
    		BigInteger a4 = new BigInteger("23067588207539346171171980310421047513778063246676");
    		BigInteger a5 = new BigInteger("89261670696623633820136378418383684178734361726757");
    		BigInteger a6 = new BigInteger("28112879812849979408065481931592621691275889832738");
    		BigInteger a7 = new BigInteger("44274228917432520321923589422876796487670272189318");
    		BigInteger a8 = new BigInteger("47451445736001306439091167216856844588711603153276");
    		BigInteger a9 = new BigInteger("70386486105843025439939619828917593665686757934951");
    		BigInteger a10 = new BigInteger("62176457141856560629502157223196586755079324193331");
    		BigInteger a11 = new BigInteger("64906352462741904929101432445813822663347944758178");
    		BigInteger a12 = new BigInteger("92575867718337217661963751590579239728245598838407");
    		BigInteger a13 = new BigInteger("58203565325359399008402633568948830189458628227828");
    		BigInteger a14 = new BigInteger("80181199384826282014278194139940567587151170094390");
    		BigInteger a15 = new BigInteger("35398664372827112653829987240784473053190104293586");
    		BigInteger a16 = new BigInteger("86515506006295864861532075273371959191420517255829");
    		BigInteger a17 = new BigInteger("71693888707715466499115593487603532921714970056938");
    		BigInteger a18 = new BigInteger("54370070576826684624621495650076471787294438377604");
    		BigInteger a19 = new BigInteger("53282654108756828443191190634694037855217779295145");
    		BigInteger a20 = new BigInteger("36123272525000296071075082563815656710885258350721");
    		BigInteger a21 = new BigInteger("45876576172410976447339110607218265236877223636045");
    		BigInteger a22 = new BigInteger("17423706905851860660448207621209813287860733969412");
    		BigInteger a23 = new BigInteger("81142660418086830619328460811191061556940512689692");
    		BigInteger a24 = new BigInteger("51934325451728388641918047049293215058642563049483");
    		BigInteger a25 = new BigInteger("62467221648435076201727918039944693004732956340691");
    		BigInteger a26 = new BigInteger("15732444386908125794514089057706229429197107928209");
    		BigInteger a27 = new BigInteger("55037687525678773091862540744969844508330393682126");
    		BigInteger a28 = new BigInteger("18336384825330154686196124348767681297534375946515");
    		BigInteger a29 = new BigInteger("80386287592878490201521685554828717201219257766954");
    		BigInteger a30 = new BigInteger("78182833757993103614740356856449095527097864797581");
    		BigInteger a31 = new BigInteger("16726320100436897842553539920931837441497806860984");
    		BigInteger a32 = new BigInteger("48403098129077791799088218795327364475675590848030");
    		BigInteger a33 = new BigInteger("87086987551392711854517078544161852424320693150332");
    		BigInteger a34 = new BigInteger("59959406895756536782107074926966537676326235447210");
    		BigInteger a35 = new BigInteger("69793950679652694742597709739166693763042633987085");
    		BigInteger a36 = new BigInteger("41052684708299085211399427365734116182760315001271");
    		BigInteger a37 = new BigInteger("65378607361501080857009149939512557028198746004375");
    		BigInteger a38 = new BigInteger("35829035317434717326932123578154982629742552737307");
    		BigInteger a39 = new BigInteger("94953759765105305946966067683156574377167401875275");
    		BigInteger a40 = new BigInteger("88902802571733229619176668713819931811048770190271");
    		BigInteger a41 = new BigInteger("25267680276078003013678680992525463401061632866526");
    		BigInteger a42 = new BigInteger("36270218540497705585629946580636237993140746255962");
    		BigInteger a43 = new BigInteger("24074486908231174977792365466257246923322810917141");
    		BigInteger a44 = new BigInteger("91430288197103288597806669760892938638285025333403");
    		BigInteger a45 = new BigInteger("34413065578016127815921815005561868836468420090470");
    		BigInteger a46 = new BigInteger("23053081172816430487623791969842487255036638784583");
    		BigInteger a47 = new BigInteger("11487696932154902810424020138335124462181441773470");
    		BigInteger a48 = new BigInteger("63783299490636259666498587618221225225512486764533");
    		BigInteger a49 = new BigInteger("67720186971698544312419572409913959008952310058822");
    		BigInteger a50 = new BigInteger("95548255300263520781532296796249481641953868218774");
    		BigInteger a51 = new BigInteger("76085327132285723110424803456124867697064507995236");
    		BigInteger a52 = new BigInteger("37774242535411291684276865538926205024910326572967");
    		BigInteger a53 = new BigInteger("23701913275725675285653248258265463092207058596522");
    		BigInteger a54 = new BigInteger("29798860272258331913126375147341994889534765745501");
    		BigInteger a55 = new BigInteger("18495701454879288984856827726077713721403798879715");
    		BigInteger a56 = new BigInteger("38298203783031473527721580348144513491373226651381");
    		BigInteger a57 = new BigInteger("34829543829199918180278916522431027392251122869539");
    		BigInteger a58 = new BigInteger("40957953066405232632538044100059654939159879593635");
    		BigInteger a59 = new BigInteger("29746152185502371307642255121183693803580388584903");
    		BigInteger a60 = new BigInteger("41698116222072977186158236678424689157993532961922");
    		BigInteger a61 = new BigInteger("62467957194401269043877107275048102390895523597457");
    		BigInteger a62 = new BigInteger("23189706772547915061505504953922979530901129967519");
    		BigInteger a63 = new BigInteger("86188088225875314529584099251203829009407770775672");
    		BigInteger a64 = new BigInteger("11306739708304724483816533873502340845647058077308");
    		BigInteger a65 = new BigInteger("82959174767140363198008187129011875491310547126581");
    		BigInteger a66 = new BigInteger("97623331044818386269515456334926366572897563400500");
    		BigInteger a67 = new BigInteger("42846280183517070527831839425882145521227251250327");
    		BigInteger a68 = new BigInteger("55121603546981200581762165212827652751691296897789");
    		BigInteger a69 = new BigInteger("32238195734329339946437501907836945765883352399886");
    		BigInteger a70 = new BigInteger("75506164965184775180738168837861091527357929701337");
    		BigInteger a71 = new BigInteger("62177842752192623401942399639168044983993173312731");
    		BigInteger a72 = new BigInteger("32924185707147349566916674687634660915035914677504");
    		BigInteger a73 = new BigInteger("99518671430235219628894890102423325116913619626622");
    		BigInteger a74 = new BigInteger("73267460800591547471830798392868535206946944540724");
    		BigInteger a75 = new BigInteger("76841822524674417161514036427982273348055556214818");
    		BigInteger a76 = new BigInteger("97142617910342598647204516893989422179826088076852");
    		BigInteger a77 = new BigInteger("87783646182799346313767754307809363333018982642090");
    		BigInteger a78 = new BigInteger("10848802521674670883215120185883543223812876952786");
    		BigInteger a79 = new BigInteger("71329612474782464538636993009049310363619763878039");
    		BigInteger a80 = new BigInteger("62184073572399794223406235393808339651327408011116");
    		BigInteger a81 = new BigInteger("66627891981488087797941876876144230030984490851411");
    		BigInteger a82 = new BigInteger("60661826293682836764744779239180335110989069790714");
    		BigInteger a83 = new BigInteger("85786944089552990653640447425576083659976645795096");
    		BigInteger a84 = new BigInteger("66024396409905389607120198219976047599490197230297");
    		BigInteger a85 = new BigInteger("64913982680032973156037120041377903785566085089252");
    		BigInteger a86 = new BigInteger("16730939319872750275468906903707539413042652315011");
    		BigInteger a87 = new BigInteger("94809377245048795150954100921645863754710598436791");
    		BigInteger a88 = new BigInteger("78639167021187492431995700641917969777599028300699");
    		BigInteger a89 = new BigInteger("15368713711936614952811305876380278410754449733078");
    		BigInteger a90 = new BigInteger("40789923115535562561142322423255033685442488917353");
    		BigInteger a91 = new BigInteger("44889911501440648020369068063960672322193204149535");
    		BigInteger a92 = new BigInteger("41503128880339536053299340368006977710650566631954");
    		BigInteger a93 = new BigInteger("81234880673210146739058568557934581403627822703280");
    		BigInteger a94 = new BigInteger("82616570773948327592232845941706525094512325230608");
    		BigInteger a95 = new BigInteger("22918802058777319719839450180888072429661980811197");
    		BigInteger a96 = new BigInteger("77158542502016545090413245809786882778948721859617");
    		BigInteger a97 = new BigInteger("72107838435069186155435662884062257473692284509516");
    		BigInteger a98 = new BigInteger("20849603980134001723930671666823555245252804609722");
    		BigInteger a99 = new BigInteger("53503534226472524250874054075591789781264330331690");
    		BigInteger sum = new BigInteger("0");
    		sum.add(a1);
    		sum.add(a2);
    		sum.add(a3);
    		sum.add(a4);
    		sum.add(a5);
    		sum.add(a6);
    		sum.add(a7);
    		sum.add(a8);
    		sum.add(a9);
    		sum.add(a10);
    		sum.add(a11);
    		sum.add(a12);
    		sum.add(a13);
    		sum.add(a14);
    		sum.add(a15);
    		sum.add(a16);
    		sum.add(a17);
    		sum.add(a18);
    		sum.add(a19);
    		sum.add(a20);
    		sum.add(a21);
    		sum.add(a22);
    		sum.add(a23);
    		sum.add(a24);
    		sum.add(a25);
    		sum.add(a26);
    		sum.add(a27);
    		sum.add(a28);
    		sum.add(a29);
    		sum.add(a30);
    		sum.add(a31);
    		sum.add(a32);
    		sum.add(a33);
    		sum.add(a34);
    		sum.add(a35);
    		sum.add(a36);
    		sum.add(a37);
    		sum.add(a38);
    		sum.add(a39);
    		sum.add(a40);
    		sum.add(a41);
    		sum.add(a42);
    		sum.add(a43);
    		sum.add(a44);
    		sum.add(a45);
    		sum.add(a46);
    		sum.add(a47);
    		sum.add(a48);
    		sum.add(a49);
    		sum.add(a50);
    		sum.add(a51);
    		sum.add(a52);
    		sum.add(a53);
    		sum.add(a54);
    		sum.add(a55);
    		sum.add(a56);
    		sum.add(a57);
    		sum.add(a58);
    		sum.add(a59);
    		sum.add(a60);
    		sum.add(a61);
    		sum.add(a62);
    		sum.add(a63);
    		sum.add(a64);
    		sum.add(a65);
    		sum.add(a66);
    		sum.add(a67);
    		sum.add(a68);
    		sum.add(a69);
    		sum.add(a70);
    		sum.add(a71);
    		sum.add(a72);
    		sum.add(a73);
    		sum.add(a74);
    		sum.add(a75);
    		sum.add(a76);
    		sum.add(a77);
    		sum.add(a78);
    		sum.add(a79);
    		sum.add(a80);
    		sum.add(a81);
    		sum.add(a82);
    		sum.add(a83);
    		sum.add(a84);
    		sum.add(a85);
    		sum.add(a86);
    		sum.add(a87);
    		sum.add(a88);
    		sum.add(a89);
    		sum.add(a90);
    		sum.add(a91);
    		sum.add(a92);
    		sum.add(a93);
    		sum.add(a94);
    		sum.add(a95);
    		sum.add(a96);
    		sum.add(a97);
    		sum.add(a98);
    		sum.add(a99);
    		System.out.println(sum);
    	}
    
    }

    Problem is sum is always = to 0. Are my numbers to big or am I doing something else wrong? Please help.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: BigInteger

    Did you read the API to see what the add method does?

  3. #3
    7cardcha is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: BigInteger

    Nope I just jumped into it. Let me check that out.
    EDIT
    Since a mod needs to approve my next post Ill just edit this one. I figured out the problem. It needs to be sum = sum.add(a99)
    I fixed it. However the answer it is giving seems to low(Or just plain wrong) I am trying to do http://projecteuler.net/problem=13 And the answer I give it is not right.
    Last edited by 7cardcha; 10-25-2011 at 05:32 AM.

  4. #4
    7cardcha is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: BigInteger

    Ok I fixed the problem it should be sum = sum.add(a99)
    But the answer it is returning seems much to low(5500268942856974534503250748612148381527149696 152422)
    Are the numbers to big?
    This is what I am trying to do Problem 13 - Project Euler The answer I give it is not right. What is wrong?

  5. #5
    7cardcha is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: BigInteger

    WHY ARE MY POSTS NOT APPEARING. I fixed the problem but the number it gets seems to low. Is there some kind of limit?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: BigInteger

    You are a new poster, and the software requires that your posts be approved by a moderator to be visible. I've just done this. Sorry for the inconvenience, but it does help to keep out spam.

  7. #7
    7cardcha is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: BigInteger

    Ok thanks:D It appears my posts are spam because I posted like 3 times because I thought for some reason I was not posting right 0.o
    Last edited by 7cardcha; 10-25-2011 at 05:49 AM.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: BigInteger

    1. The problem states to sum 100 numbers. Your code only has 99 numbers.
    2. Disply first 10 digits of result. Your code displays the full result.

  9. #9
    7cardcha is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: BigInteger

    Thanks it worked. I am such a retard.(PS I didn't have problem 2 I only copy/pasted the first 10 digits)

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default Re: BigInteger

    Quote Originally Posted by 7cardcha View Post
    But the answer it is returning seems much to low(5500268942856974534503250748612148381527149696 152422)
    Are the numbers to big?
    The third digit from the left in your answer is incorrect. I just checked it (my answer seems to be correct) but you must have a typo somewhere in that big ugly list.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. BigInteger nth root
    By adamkoe in forum New To Java
    Replies: 1
    Last Post: 08-29-2011, 09:47 AM
  2. BigDecimal and BigInteger
    By jon80 in forum New To Java
    Replies: 8
    Last Post: 11-28-2010, 11:15 AM
  3. BigInteger value of
    By ccie007 in forum New To Java
    Replies: 2
    Last Post: 09-26-2010, 03:46 PM
  4. BigInteger
    By windows.login in forum New To Java
    Replies: 8
    Last Post: 07-13-2010, 01:10 PM
  5. how to convert from BigInteger to Hex
    By nanaji in forum Advanced Java
    Replies: 10
    Last Post: 05-22-2008, 12:44 PM

Posting Permissions

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