[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[QUIZ] Digits of Pi (#202

Daniel Moore

4/24/2009 9:05:00 PM

-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=
=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-

The three rules of Ruby Quiz:

1. Please do not post any solutions or spoiler discussion for this
quiz until 48 hours have elapsed from the time this message was
sent.

2. Support Ruby Quiz by submitting ideas and responses
as often as you can!
Visit: <http://rubyquiz.../sugge...

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem
helps everyone on Ruby Talk follow the discussion. Please reply to
the original quiz message, if you can.

RSS Feed: http://rubyquiz.../q...

-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=
=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-

## Digits of Pi (#202)

Geia sas Rubyists,

Pi or =F0 is a mathematical constant whose value is the ratio of any
circle's circumference to its diameter in Euclidean space. Because =F0
is an irrational number, its decimal expansion never ends and does not
repeat. This infinite sequence of digits has fascinated mathematicians
and laymen alike, and much effort over the last few centuries has been
put into computing more digits and investigating the number's
properties.[1]

This week's quiz is to write a Ruby program that can compute the first
100,000 digits of =F0.

[1]: http://en.wikipedia.o...

Have Fun!
--=20
-Daniel
http://rubyquiz...

22 Answers

Joel VanderWerf

4/24/2009 9:37:00 PM

0

Daniel Moore wrote:
> This week's quiz is to write a Ruby program that can compute the first
> 100,000 digits of ?.

Bonus points for determining the first non-trivial ruby program encoded
in that sequence of digits.

(Represent a program source string as a sequence of octal triplets that
is, or is not, a subsequence of the octal representation of ?.)

Non-trivial means not "", not just a literal, etc. Be imaginative.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Jeff Schwab

4/25/2009 1:25:00 AM

0

Joel VanderWerf wrote:
> Daniel Moore wrote:
>> This week's quiz is to write a Ruby program that can compute the first
>> 100,000 digits of ?.
>
> Bonus points for determining the first non-trivial ruby program encoded
> in that sequence of digits.

Interesting idea!

Is there any way of determining whether a given sequence of bytes is a
valid ruby program, other than running it? If this were my paternus
lingua, C++, I would start by chucking each candidate sequence at a
compiler.

> (Represent a program source string as a sequence of octal triplets
> that is, or is not, a subsequence of the octal representation of ?.)

What is an "octal triplet?" Do you mean that the integer value of each
byte in the source code should be represented by a sequence of three
octal digits (e.g. 077 for ?? and 141 for ?a)?

I'm curious: Why octal? It seems an odd choice, given that an "octal
triplet" corresponds to 9 bits, rather than the 8 in a standard byte.
Why not "hex pairs?"

Btw, isn't every finite sequence of digits a subsequence of Pi's
representation in that base? Or is that unknowable?

Todd Benson

4/25/2009 2:54:00 AM

0

2009/4/24 Joel VanderWerf <vjoel@path.berkeley.edu>:
> Daniel Moore wrote:
>>
>> This week's quiz is to write a Ruby program that can compute the first
>> 100,000 digits of =F0.
>
> Bonus points for determining the first non-trivial ruby program encoded i=
n
> that sequence of digits.

Do you mean non-trivial in that it will never fail with the correct
initial conditions? Or simply that it's syntactically correct?

Joel VanderWerf

4/25/2009 2:57:00 AM

0

Todd Benson wrote:
> 2009/4/24 Joel VanderWerf <vjoel@path.berkeley.edu>:
>> Daniel Moore wrote:
>>> This week's quiz is to write a Ruby program that can compute the first
>>> 100,000 digits of ?.
>> Bonus points for determining the first non-trivial ruby program encoded in
>> that sequence of digits.
>
> Do you mean non-trivial in that it will never fail with the correct
> initial conditions? Or simply that it's syntactically correct?

I dunno. That's why I said be imaginative :)

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Joel VanderWerf

4/25/2009 3:13:00 AM

0

Jeff Schwab wrote:
> Joel VanderWerf wrote:
>> Daniel Moore wrote:
>>> This week's quiz is to write a Ruby program that can compute the first
>>> 100,000 digits of ?.
>>
>> Bonus points for determining the first non-trivial ruby program
>> encoded in that sequence of digits.
>
> Interesting idea!
>
> Is there any way of determining whether a given sequence of bytes is a
> valid ruby program, other than running it? If this were my paternus
> lingua, C++, I would start by chucking each candidate sequence at a
> compiler.
>
>> (Represent a program source string as a sequence of octal triplets
>> that is, or is not, a subsequence of the octal representation of ?.)
>
> What is an "octal triplet?" Do you mean that the integer value of each
> byte in the source code should be represented by a sequence of three
> octal digits (e.g. 077 for ?? and 141 for ?a)?
>
> I'm curious: Why octal? It seems an odd choice, given that an "octal
> triplet" corresponds to 9 bits, rather than the 8 in a standard byte.
> Why not "hex pairs?"

You're right, that didn't make much sense. I was just thinking of "\nnn"
and counting to three, but of course that's 9 bits. What I was trying
to do was slice off just enough bits to make a printable char with high
probability. Otherwise, the 128..255 will keep breaking up otherwise
legal program strings.

Base 128 (7 bits) would be better, but there are still some non-printing
chars. Even better would be to skip ascii and use something else, but I
didn't want to get too far into fantasy land...

> Btw, isn't every finite sequence of digits a subsequence of Pi's
> representation in that base? Or is that unknowable?

IIRC that's true. But the _first_ ruby program... gee, that's got to
mean something. :)

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Harry Kakueki

4/27/2009 1:09:00 AM

0

>
> This week's quiz is to write a Ruby program that can compute the first
> 100,000 digits of =F0.
>
>

# Is this cheating ? :)

require 'bigdecimal'
require 'bigdecimal/math'
include BigMath

puts PI(100_000)



Harry


--=20
A Look into Japanese Ruby List in English
http://www.kakueki.com/ruby...

Robert Dober

4/27/2009 5:50:00 AM

0

2009/4/27 Harry Kakueki <list.push@gmail.com>:
>>
>> This week's quiz is to write a Ruby program that can compute the first
>> 100,000 digits of =F0.
>>
>>
>
> # Is this cheating ? =A0:)
I would say no, but...
I honestly could not come up with a solution, always getting confused
how many significant digits I need in the involved constants. Thus I
am really looking forward to the solutions and the summary. (Distant
memories tell me I should do some range arithmetics, we will see)
Sorry to say Harry, but your solution has not enlightened me on the topic ;=
).
Cheers
Robert

Todd Benson

4/27/2009 6:13:00 AM

0

2009/4/27 Robert Dober <robert.dober@gmail.com>:
> 2009/4/27 Harry Kakueki <list.push@gmail.com>:
>>>
>>> This week's quiz is to write a Ruby program that can compute the first
>>> 100,000 digits of =F0.
>>>
>>>
>>
>> # Is this cheating ? :)
> I would say no, but...
> I honestly could not come up with a solution, always getting confused
> how many significant digits I need in the involved constants. Thus I
> am really looking forward to the solutions and the summary. (Distant
> memories tell me I should do some range arithmetics, we will see)
> Sorry to say Harry, but your solution has not enlightened me on the topic=
;).
> Cheers
> Robert

For what it's worth, I tried a Gauss-Legendre and it halted my PC at a
delta of 1e-10 and smaller (only 10 good digits), and then started to
diverge pretty rapidly, so much so that the program would halt (yes, I
simply sat there and hit the gets over and over). I then tried
Srinavasa's method, and it converged so quickly to 10 places, and
never gave up after that, but it took about 3 hours to run k up to
1024 (without my intervention), which amounts to around 12_000 correct
places.

There's Daniel Shanks, which I might try my hand at eventually, but
the winner for this programming language might end up being
brute-force by-digit deterministic approach mentioned by one of the
first posters.

I think, Robert, cheating would be writing a program that grabs the
digits from the website :)

Todd

Robert Dober

4/27/2009 1:16:00 PM

0

2009/4/25 Joel VanderWerf <vjoel@path.berkeley.edu>:
>> Btw, isn't every finite sequence of digits a subsequence of Pi's
>> representation in that base? =A0Or is that unknowable?
>
> IIRC that's true. But the _first_ ruby program... gee, that's got to mean
> something. :)

But that means that the representation of pi contains a ruby program
that computes pi itself (although of course it never finishes), and
that for any imaginable programming language capable of that task, eg
a TuringMachine.

I am confused now, but I bet the Ruby program starts exactly at the
42**42 hexdigit - for a well chosen base of course ;)
R.

Jeff Schwab

4/27/2009 1:19:00 PM

0

Robert Dober wrote:
> 2009/4/25 Joel VanderWerf <vjoel@path.berkeley.edu>:
>>> Btw, isn't every finite sequence of digits a subsequence of Pi's
>>> representation in that base? Or is that unknowable?
>> IIRC that's true. But the _first_ ruby program... gee, that's got to mean
>> something. :)
>
> But that means that the representation of pi contains a ruby program
> that computes pi itself (although of course it never finishes), and
> that for any imaginable programming language capable of that task, eg
> a TuringMachine.

Infinite is biiiiiiiig...

> I am confused now, but I bet the Ruby program starts exactly at the
> 42**42 hexdigit - for a well chosen base of course ;)

"You may think it's a long walk to the chemist, but that's just peanuts
to space."