Thursday, 1 August 2013

Python with big number

Python is a good programming language for processing big number. We don't need to care about type of value or add any library.


Example:


result = 1
for i in range (1, 140):
    result *= i
print result


And this is result: 96157231969410890041971956135297253988256079629956908500367081914696145479218112119985149618783441690577636407442564169301886059792163365100096999581219760002673769583579570682891939608962934200435638009856000000000000000000000000000000000


With java, many friend of me don't know type of value larger than long. However, java support BigInteger and Bigdecimal.

    public class JavaApplication1 {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            question2();
        }
     
        public static void question2() {

            BigInteger fact = BigInteger.valueOf(1);
            for (int i = 1; i <= 139; i++) {
                //fact = fact.multiply(BigInteger.valueOf(i));
                fact.multiply(BigInteger.valueOf(i));
            }
            System.out.println(fact);
        }
    }

And with javascript, a language with dynamic type like python


var result =1;
for(var i=1;i<140;i++){
    result =result*i;
}
document.write(result);


Result: 9.615723196941086e+238
We must find and add libraries  in google to add.

So python is very comfortable in calculator with big number.

No comments:

Post a Comment