0

I have a 309 digit integer, I want to iterate through its characters.

Currently I am using:

require 'openssl'
e = 116505013962726356794269846667188147473899121100449069443844506823885859211073843523906823741034558875724969276233769835502344452366515593952571468651971447660633083078837371793388842846199643249996094940742465135064478448126948741186882484457847959126808512823416166517945252986434515406363102297514031583117

and I have:

e.times do |i|
   ...
end

Which, understandably, yields an error:

undefined method `times' for #<OpenSSL::BN:0x007fec05002140>

I attempted to convert the bignum to an integer:

 e.to_i.times do |i|
   ...
 end

Which returned:

bignum too big to convert into `long'

I understand why I am receiving these errors, but I am asking how do I iterate through each character of such a large number?

Wayne Conrad
  • 103,207
  • 26
  • 155
  • 191
Derptacos
  • 179
  • 10
  • 2
    Iterating `e` times will take you to the end of times. – dee-see Nov 28 '13 at 17:46
  • 1
    Do you mean "iterate through the digits (as characters) in a base 10 representation" (which the current answer addresses), or something else? – Peter Alfvin Nov 28 '13 at 17:50
  • I don't think it is practical. Even if there were a way to do it, I don't think you would still be alive by the time the iteration finishes. – sawa Nov 28 '13 at 17:54
  • There is a bounds, dealing with some crypto things at the moment so I guess iterations this large are common. – Derptacos Nov 28 '13 at 17:56

1 Answers1

3

How is this ?

e = 116505013962726356794269846667188147473899121100449069443844506823885859211073843523906823741034558875724969276233769835502344452366515593952571468651971447660633083078837371793388842846199643249996094940742465135064478448126948741186882484457847959126808512823416166517945252986434515406363102297514031583117
e.to_s.each_char do |c|
    # code
end
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317