Re: converting long string to number
Really embarrassing in two ways: - Could not remove my previous post. :-) - I should have tried to create such a big number (at least after reading all the answers here) before posting... It is possible to crate a 5 digit big integer. PS: Watch out though. The aim of the problem is to add up those 50- digit numbers and not to create one 5-digit number. --feka On Mar 26, 12:40 am, Glen Rubin wrote: > I am trying to convert a long string of numbers to a number, but get a > java.lang.numberformatexception > > 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 received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@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+unsubscr...@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.
Re: converting long string to number
Looks like you are doing the 13th Project Euler problem. That means you have exactly one hundred 50 digit numbers. I am new to Clojure but AFAIK you wont be able to handle a 5000 digit number in it. You have to find another way. There is at least one... ;-) --Feka On Mar 25, 11:40 pm, Glen Rubin wrote: > I am trying to convert a long string of numbers to a number, but get a > java.lang.numberformatexception > > 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 received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@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+unsubscr...@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.
Re: converting long string to number
> You want (java.math.BigInteger. "2342343..."). java.lang.Integer is > limited to 2^31-1. Clojure has a "bigint" function, so (bigint (apply str (re-seq #"[0-9]+" big-num-str))) will work as well. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@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+unsubscr...@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.
Re: converting long string to number
In those hills yonder in the lands of Common Lisp, it's usually considered good practice to blast the entire read table save for what you need when you deal with untrusted data. Barring that, a better option might be a more modular reader: read-number, read-symbol, etc. 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. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@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+unsubscr...@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.
Re: converting long string to number
In those hills yonder in the lands of Common Lisp, it's usually considered good practice to blast the entire read table save for what you need when you deal with untrusted data. Barring that, a better option might be a more modular reader: read-number, read-symbol, etc. -Per On Fri, Mar 26, 2010 at 9:01 AM, Richard Newman wrote: >> 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 >> >> :) > > 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) > > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@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+unsubscr...@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. > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@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+unsubscr...@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.
Re: converting long string to number
Another unadvertised function that is useful to be aware of is clojure.lang.Numbers/reduce which will simplify a number to its most simple type. I often find that I want to use some BigInteger function, but then it is important to turn it back into a "typical Clojure number" at the end. For example, (defn next-prime [n] (clojure.lang.Numbers/reduce (.nextProbablePrime (BigInteger. (str n) --Mark -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@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+unsubscr...@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.
Re: converting long string to number
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 :) 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) -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@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+unsubscr...@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.
Re: converting long string to number
Though it might not be the best option here, the Clojure reader is always ready to serve: 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 On Fri, Mar 26, 2010 at 5:47 AM, Chas Emerick wrote: > Glen, > > You want (java.math.BigInteger. "2342343..."). java.lang.Integer is limited > to 2^31-1. > > - Chas > > On Mar 25, 2010, at 6:40 PM, Glen Rubin wrote: > >> I am trying to convert a long string of numbers to a number, but get a >> java.lang.numberformatexception >> >> >> 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. > > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@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+unsubscr...@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. > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@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+unsubscr...@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.
Re: converting long string to number
Glen, You want (java.math.BigInteger. "2342343..."). java.lang.Integer is limited to 2^31-1. - Chas On Mar 25, 2010, at 6:40 PM, Glen Rubin wrote: I am trying to convert a long string of numbers to a number, but get a java.lang.numberformatexception 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. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@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+unsubscr...@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.
converting long string to number
I am trying to convert a long string of numbers to a number, but get a java.lang.numberformatexception 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 received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@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+unsubscr...@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.