# Digits of Pi (#202)

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

The three rules of Ruby Q.:

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 Q. by submitting ideas and responses
as often as you can!
Visit: http://rubyquiz.strd6.com/suggestions

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem
the original quiz message, if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

## Digits of Pi (#202)

Geia sas Rubyists,

Pi or ð is a mathematical constant whose value is the ratio of any
circle’s circumference to its diameter in Euclidean space. Because ð
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
ð.
[1]: http://en.wikipedia.org/wiki/Pi

Have Fun!

Daniel M. 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.

compute the first 100,000 digits of Ï€.What base do those digits have to be in? This algorithm allows you to calculate the n’th hexadecimal digit directly (without calculating the proceeding hexadecimal digits!):

Algorithm for calculating individual hexadecimal digits of pi
http://rubyurl.com/Zahp

2009/4/24 Joel VanderWerf [email protected]:

Daniel M. 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?

Joel VanderWerf wrote:

Daniel M. 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?

Jeff S. wrote:

valid ruby program, other than running it? If this were my paternus
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.

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

# Is this cheating ?

require ‘bigdecimal’
require ‘bigdecimal/math’
include BigMath

puts PI(100_000)

Harry

Todd B. wrote:

2009/4/24 Joel VanderWerf [email protected]:

Daniel M. 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

2009/4/27 Harry K. [email protected]:

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

# 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

2009/4/25 Joel VanderWerf [email protected]:

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.

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.

2009/4/27 Robert D. [email protected]:

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 D. wrote:

2009/4/25 Joel VanderWerf [email protected]:

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.”

On Mon, Apr 27, 2009 at 9:15 AM, Robert D. [email protected]
wrote:

a TuringMachine.
Been reading Goedel, Escher, Bach again, have we Robert?

Rick DeNatale

On Mon, Apr 27, 2009 at 3:27 PM, Rick DeNatale [email protected]
wrote:

that for any imaginable programming language capable of that task, eg
a TuringMachine.

## Been reading Goedel, Escher, Bach again, have we Robert?

Rick DeNatale

To my defense I can claim: “I did not understand a bit”, but it is
indeed a great read (actually never really finished it )

Si tu veux construire un bateau …
Ne rassemble pas des hommes pour aller chercher du bois, préparer des
outils, répartir les tâches, alléger le travail… mais enseigne aux
gens la nostalgie de l’infini de la mer.

If you want to build a ship, don’t herd people together to collect
wood and don’t assign them tasks and work, but rather teach them to
long for the endless immensity of the sea.

Here’s a solution using the “plus/minus fractiony solution for pi” from
here:
http://tinyurl.com/3ve7wy

Note: this is also referred to as the Leibniz formula for pi

My solution is definitely not a great solution as after about 5 minutes,
I’ve got about 8 digits and it only gets slower. I doubt it will make it
to 100,000 digits. I’m looking forward to seeing other solutions to this
problem.

require “rational”
require ‘enumerator’
require ‘bigdecimal’
require ‘bigdecimal/math’
include BigMath

iterations = 10000000000
current = 1
final = BigDecimal.new(“4”)
other = false

while(current < iterations) do
#puts current
current = current + 2
if(other)
final = final + Rational(4,current)
else
final = final - Rational(4,current)
end

other = !other
print current.to_s + “:”
puts final.to_f
end

On Mon, 2009-04-27 at 15:12 +0900, Todd B. wrote:

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

Todd

I was unaware that one could cheat on a rubyquiz.

Hi

Pi is a variable number which changes with different radii. PI can be
3.12 or 3.45…

Please see the proof of variable PI:

http://www.lecan.net/raremath.html

Good luck

Can Le

— On Mon, 4/27/09, Jeff S. [email protected] wrote:
From: Jeff S. [email protected]
Subject: Re: [QUIZ] Digits of Pi (#202)
To: “ruby-talk ML” [email protected]
Date: Monday, April 27, 2009, 8:20 AM

Robert D. wrote:

2009/4/25 Joel VanderWerf [email protected]:

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.”

I’m sorry, but maybe I’m missing something. Aren’t you just restating
the “many-polygon becomes a circle” problem? Good luck, indeed! I
must have been thinking about something else

That was a bit unfair. I thought you were talking about something else.

Todd

2009/4/27 Harry K. [email protected]:

# Is this cheating ?

I guess the only thing I’d consider cheating would be something like
this:

require ‘rubygems’
require ‘hpricot’
require ‘open-uri’

puts (doc/‘pre’).inner_html

Michael

On Tue, Apr 28, 2009 at 10:32 PM, Can Le [email protected] wrote:

http://www.lecan.net/raremath.html

Good luck

I’m sorry, but maybe I’m missing something. Aren’t you just restating
the “many-polygon becomes a circle” problem? Good luck, indeed! I
must have been thinking about something else

Todd

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.