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.
1ae92ac1fcb76a88782f461bea73c668?d=identicon&s=25 Victor Reyes (Guest)
on 2007-05-04 19: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
83ca41657a99b65d99889abe712ba5e2?d=identicon&s=25 Jason Roelofs (Guest)
on 2007-05-04 19: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
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (Guest)
on 2007-05-04 20:45
(Received via mailing list)
On Sat, May 05, 2007 at 02:10:58AM +0900, Victor Reyes 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 :-)
1ae92ac1fcb76a88782f461bea73c668?d=identicon&s=25 Victor Reyes (Guest)
on 2007-05-04 21: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
47694fa0220a629040b814342725e1ba?d=identicon&s=25 Mark Day (Guest)
on 2007-05-04 21:35
(Received via mailing list)
On May 4, 2007, at 10:36 AM, Jason Roelofs 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
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (Guest)
on 2007-05-04 22:10
(Received via mailing list)
On Sat, May 05, 2007 at 04:34:01AM +0900, Victor Reyes 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...
1ae92ac1fcb76a88782f461bea73c668?d=identicon&s=25 Victor Reyes (Guest)
on 2007-05-04 22: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
2cef22e5d84ec3cdc107a5f9f97537e5?d=identicon&s=25 Todd Benson (Guest)
on 2007-05-04 23:03
(Received via mailing list)
--- Victor Reyes <victor.reyes@gmail.com> 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
2cef22e5d84ec3cdc107a5f9f97537e5?d=identicon&s=25 Todd Benson (Guest)
on 2007-05-04 23:13
(Received via mailing list)
I said:

> p 2048 ** 31819 - 1024 ** 31819

Oops.  Ignore this one.  I don't know what I was
thinking :)
1ae92ac1fcb76a88782f461bea73c668?d=identicon&s=25 Victor Reyes (Guest)
on 2007-05-05 04:13
(Received via mailing list)
Todd, thank you for the example. I truly appreciate it. I'll give it a
try.

Victor
2cef22e5d84ec3cdc107a5f9f97537e5?d=identicon&s=25 Todd Benson (Guest)
on 2007-05-05 16:34
(Received via mailing list)
--- Victor Reyes <victor.reyes@gmail.com> 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.
2cef22e5d84ec3cdc107a5f9f97537e5?d=identicon&s=25 Todd Benson (Guest)
on 2007-05-05 17:35
(Received via mailing list)
--- Todd Benson <toddkennethbenson@yahoo.com> wrote:

>
> --- Victor Reyes <victor.reyes@gmail.com> 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.