Aaron D. Gifford wrote in post #1034519:
irb(main):001:0> s="2011";s.sum%(48*s.size)
=> 4
And to verify worst-case a string of 1149 nine digits:
irb(main):002:0> s="9"*1149;s.sum%(48*s.size)
=> 10341
For longer strings, pass an argument to String#sum bigger than the
default 16.
Very good, Aaron. Now, is there any way to replace that "s.size" with
something to make it really a one-liner (without the semicolon):
"some number".sum%(48 times something)
?
Regards,
Bill
···
--
Posted via http://www.ruby-forum.com/\.
Admin Tensor wrote in post #1034521:
irb(main):002:0> s="9"*1149;s.sum%(48*s.size)
=> 10341
For longer strings, pass an argument to String#sum bigger than the
default 16.
Very good, Aaron. Now, is there any way to replace that "s.size" with
something to make it really a one-liner (without the semicolon):
"some number".sum%(48 times something)
?
lambda {|x| x.sum%(48*x.size)}["9"*1149]
Or something nasty with "->" if under ruby 1.9. But once you go to that
extreme, you might as well stick with the more obvious and complete:
("9"*1149).chars.inject(0){|a,b|a+b.to_i}
···
--
Posted via http://www.ruby-forum.com/\.