Coerce to BigInt
user=> (bigint 30) 30 ;; Actually do something BigInteger-ish... (http://download.oracle.com/javase/6/docs/api/) user=> (def x (bigint 97)) #'user/x user=> (.isProbablePrime (.toBigInteger x) 100) true
user> (= (bigint 42) (clojure.lang.BigInt/fromBigInteger (BigInteger. "42"))) true user> (= 42N (bigint 42)) true user> (= 42 (bigint 42)) true user> (= 42 (clojure.lang.BigInt/fromBigInteger (BigInteger. "42"))) true
user> (reduce * (repeat 20 1000)) ArithmeticException integer overflow clojure.lang.Numbers.throwIntOverflow (Numbers.java:1388) user> (reduce * (repeat 20 (bigint 1000))) 1000000000000000000000000000000000000000000000000000000000000N
;; There is a difference between `BigInt` and `BigInteger`. The first is from ;; Clojure and should be better for performace, because less unboxing is ;; necessary. The second is from Java and has more functionality. ;; https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html (type 123N) ;; => clojure.lang.BigInt (type (bigint 123)) ;; => clojure.lang.BigInt (type (biginteger 123)) ;; => java.math.BigInteger (.modInverse (bigint 123) (bigint 4)) ;; IllegalArgumentException No matching method found: modInverse for class ;; clojure.lang.BigInt (.modInverse (biginteger 123) (biginteger 4)) ;; => 3
;; Take care with ratios the decimal part is removed not rounded user=> (bigint 5/4) ; 1.25 ;; 1N user=> (bigint 5/2) ; 2.5 ;; 2N
The last example does not seem to work; there seems to be a missing coercion from Clojure BigInt to Java BigInteger. I get
IllegalArgumentException No matching method found: isProbablePrime for class clojure.lang.BigInt clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:53)