My long string of numbers has new line characters in it, so I am
filtering out the newline characters before converting it back to a
string. Then I try to use Integer. on it but get the above exception.
Code is as follows:
big-num-str is truncated for space's sake, is actually much much
longer!!
(def big-num-str "37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250
23067588207539346171171980310421047513778063246676
89261670696623633820136378418383684178734361726757
28112879812849979408065481931592621691275889832738
44274228917432520321923589422876796487670272189318
47451445736001306439091167216856844588711603153276
70386486105843025439939619828917593665686757934951....")
(Integer. (apply str (filter #(Character/isDigit %) big-num-str)))
You want (java.math.BigInteger. "2342343..."). java.lang.Integer is
limited to 2^31-1.
- Chas
user> (type (read-string "123"))
java.lang.Integer
user> (type (read-string "123123123123123"))
java.lang.Long
user> (type (read-string "123123123123123123123123123123123123"))
java.math.BigInteger
Of course, it might also pose a bit of a security threat:
user> (read-string "#=(println \"I OWN YOU NOW!\")")
I OWN YOU NOW!
nil
:)
-Per
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
>
> To unsubscribe from this group, send email to
> clojure+unsubscribegooglegroups.com or reply to this email with the words
> "REMOVE ME" as the subject.
>
user=> (binding [*read-eval* false]
(read-string "#=(println \"I OWN YOU NOW!\")"))
java.lang.RuntimeException: java.lang.Exception: EvalReader not
allowed when *read-eval* is false. (NO_SOURCE_FILE:0)
-Per
Clojure doesn't have a user-programmable reader, so much of the
readtable shenanigans we use in CL don't apply. But yes, specific
readers would be neat.
Clojure has a "bigint" function, so (bigint (apply str (re-seq
#"[0-9]+" big-num-str))) will work as well.
--Feka
PS: Watch out though. The aim of the problem is to add up those 50-
digit numbers and not to create one 50000-digit number.
--feka