JAVA中BigInteger的使用

题目:

使用BigInteger实现计算表达式(1+2)x(1+ 22 )x…x(1+ 2100 )的结果

代码:



import java.math.BigInteger;

/**
 * 使用BigInteger实现计算表达式(1+2)*(1+2^2)*...*(1+2^100)的结果
 * @author ZHR
 */
public class SumBiginteger {
    public static void main(String[] args) {
        // 
        BigInteger sum=new BigInteger("3");
        BigInteger var=new BigInteger("2");
        BigInteger  m=new BigInteger("2");
        BigInteger constent=new BigInteger("1");
        //System.out.println(sum);
        for(int i=2;i<=100;i++) {
            m=m.multiply(var); 
            sum=sum.multiply((constent.add(m)));
        }
        System.out.println(sum);
    }
}

输出:

379163426043123945780935917075871996841723618733680038168730113018994644399212274293112865106126357458323999757406122107777563556935698663289088318700318487717479709229206935783469553588349033173519149848206960793050037479200897321657016795991146619179719490455816126119875944083124912939820578277282329122031684316716875640910704028172892242074263551327709056594124563933541356534334458278404425484210044208540770814760681679484718172329331523479509318845041674903527681356275587969519571654047214930988835373158304690731103201841905374949890898118747365799382866309863291140898579657246311278986107420389194493369391729561038201078747205243527979258920889041290651050912321405934653479918252981724751725794923329880264126476562861517626332800192588826636911123414032539751182181562824995720452701924173695387534322875403109079689562436259067209924950898092677707637951741911020169087949624914043054744321775013829643332777096652535829314341197551677839329224807792310173711780975570418042724594877116050281000701064794190820133092687762752840436128486057193157671696920559928805575965939324778526964020610227439827770680637188646483790212393636547310640119928790833175848154100589840366930207078234048583350562696225129282320229251205081977119864946035780520727705752828904438059575201715336222432208855718300897608638656036641287303859581903468735100159680901432463574250113356916509045244125675248875711720252033196917816410417094755662159691158759372875461338369791519261120577425029931367268501780927181243896484375

猜你喜欢

转载自blog.csdn.net/qq_37999723/article/details/78705240