Forum: Ruby Help with bigbnum

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Victor R. (Guest)
on 2007-05-04 21:12
(Received via mailing list)
I am trying to use the following computation:  232582656(232582657-1)

n = (2**32582656) * (2**32582657 - 1)

I am getting the msg:

perf.rb:5: warning: in a**b, b may be too big Infinity

Any help will be appreciated.

Thank you

Victor
Jason R. (Guest)
on 2007-05-04 21:37
(Received via mailing list)
You do realize the magnitude of number you're trying to calculate? There
IS
a limit to how big even BigNum can go before deciding to crap out. I'd
like
to see someone try this in Lisp. If that interpreter can't handle it,
then
no language can.

Jason
Brian C. (Guest)
on 2007-05-04 22:45
(Received via mailing list)
On Sat, May 05, 2007 at 02:10:58AM +0900, Victor R. wrote:
> I am trying to use the following computation:  232582656(232582657-1)
>
> n = (2**32582656) * (2**32582657 - 1)

Homework question? Brute force probably isn't the way :-)
Victor R. (Guest)
on 2007-05-04 23:34
(Received via mailing list)
Brian,

Actually, NO. This is not a homework questions. It is, however and
unfortunately, an issue of my deficiency and lack of knowledge of the
language.
For pure and simple intellectual curiosity, I am attempting to write a
ruby
pgm to find perfect numbers.
The largest perfect number found to this date, if I believe website
http://amicable.homepage.dk/perfect.htm  is: (2**32582656) *
(2**32582657 -
1).
Been the neophyte that I am, I thought about picking it up from there
and
play with it, since I have at my disposal a very large IBM
multiprocessor
environment.
I know that Brute force is not the way, but I don't have a nice
algorithm to
use.

Thank you

Victor
Mark Day (Guest)
on 2007-05-04 23:35
(Received via mailing list)
On May 4, 2007, at 10:36 AM, Jason R. wrote:

> You do realize the magnitude of number you're trying to calculate?
> There IS
> a limit to how big even BigNum can go before deciding to crap out.
> I'd like
> to see someone try this in Lisp. If that interpreter can't handle
> it, then
> no language can.

Python 2.5 appears to have handled the expression easily enough (3-4
seconds on my iMac Intel Core Duo).  Converting it to a string for
display seems to be taking quite a long time, which isn't too
surprising since it should have nearly 2 million decimal digits
(according to log10(n)).

-Mark
Brian C. (Guest)
on 2007-05-05 00:10
(Received via mailing list)
On Sat, May 05, 2007 at 04:34:01AM +0900, Victor R. wrote:
> The largest perfect number found to this date, if I believe website
> http://amicable.homepage.dk/perfect.htm  is: (2**32582656) * (2**32582657 -
> 1).

Then that's the number, represented in a compact form.

> I know that Brute force is not the way, but I don't have a nice algorithm to
> use.

But what are you trying to achieve? Simply to display this number in its
direct decimal form?

If my rusty maths is correct, that number is a little under
  2**(32582656 + 32582657)

which is roughly 10 ** (  (32582656 + 32582657) * log(2)/log(10) )
~= 10 ** 19616713

So there are nearly 20 million digits in the answer...
Victor R. (Guest)
on 2007-05-05 00:26
(Received via mailing list)
For the record, I am running ruby 1.86 under AIX 5.3.
If the answer is that it can be done on Ruby, that's an OK answer also.
If
this is the case, I start playing with Python, at least for this
problem.

Thanks

Victor
Todd B. (Guest)
on 2007-05-05 01:03
(Received via mailing list)
--- Victor R. <removed_email_address@domain.invalid> wrote:

> For the record, I am running ruby 1.86 under AIX
> 5.3.
> If the answer is that it can be done on Ruby, that's
> an OK answer also. If
> this is the case, I start playing with Python, at
> least for this problem.
>
> Thanks
>
> Victor

p 2048 ** 31819 - 1024 ** 31819

or

a = 1
32582656.times { a *= 2 }
b = a
32582656.times { b *= 2 }
p b - a
Todd B. (Guest)
on 2007-05-05 01:13
(Received via mailing list)
I said:

> p 2048 ** 31819 - 1024 ** 31819

Oops.  Ignore this one.  I don't know what I was
thinking :)
Victor R. (Guest)
on 2007-05-05 06:13
(Received via mailing list)
Todd, thank you for the example. I truly appreciate it. I'll give it a
try.

Victor
Todd B. (Guest)
on 2007-05-05 18:34
(Received via mailing list)
--- Victor R. <removed_email_address@domain.invalid> wrote:

> Todd, thank you for the example. I truly appreciate
> it. I'll give it a try.

Don't get too excited.  On my system (2.4GHz Intel),
it looks like it would take about 2.5 hours to compute
(I didn't test it to see if it would even work).
Also, it is most likely faster to multiply by larger
numbers than 2.

2**32582656 == 4**16291328 == 16**8145664 ==
256**4072832 == 65536**2036416 and so on

Broken down, it's:

2**32582656 == 2**(2**10 * 3**2 * 3541) ==
( (2**(2**10))) ** (3**2) ) ** 3541

I'm guessing now matter how you cut it up, you will
get the same error, though :(  Maybe ruby chokes at
around a million digits.
Todd B. (Guest)
on 2007-05-05 19:35
(Received via mailing list)
--- Todd B. <removed_email_address@domain.invalid> wrote:

>
> --- Victor R. <removed_email_address@domain.invalid> wrote:
>
> > Todd, thank you for the example. I truly
> appreciate
> > it. I'll give it a try.
>
> Don't get too excited.  On my system (2.4GHz Intel),
> it looks like it would take about 2.5 hours to
> compute

# The next 3 lines compute 2**32582656 in less than a
minute on my system:


a = 2 ** (2**10 * 9)
b = 1
3541.times { b*=a }
# a**3541, which equals 2**32582656, will fail to
# execute in ruby, but the 3541.times works


# this next line, however,

s = b.to_s

# takes a _very_ long time
# similar (but much longer, I think) to Python's
# rendering of b into a string.
This topic is locked and can not be replied to.