Title: Find patterns within Pi in C#
March 14 is known as Pi Day because its numeric representation 3/14 includes the first three digits of Pi: 3.14. (At least in the United States. In Europe where they usually write dates month first, Pi Day is July 22 because 22/7 is also a good approximation of Pi.)
Because you can find 3/14 in the digits of Pi, you might wonder whether you can find other patterns of digits in Pi. This program searches the first 100 thousand digits of Pi to find dates or other patterns that you enter.
The program includes the file Pi.txt. (I got this file from the web site Digits of Pi, where you can download 10, 50, 1 hundred, 1 thousand, 10 thousand, 100 thousand, or 1 million digits of Pi.)
At design time I opened Solution Explorer, right-clicked the project, opened the Add menu item, selected Existing Item, and added the file to the project. I then set the file's "Copy to Output Directory" property to "Copy if newer." Now when the program runs, the file is copied into the executable directory if it isn't already there.
When the program starts, it uses the following code to load the file.
// The digits of pi.
private string Pi;
// Load the digits of pi.
private void Form1_Load(object sender, EventArgs e)
{
Pi = File.ReadAllText("Pi.txt");
rchPi.Text = Pi;
rchPi.Select(0, 0);
lblPosition.Text = "";
}
This code defines variable Pi at the class level. The form's Load event handler uses System.IO.File.ReadAllText to read the file into the string Pi.
The code then displays the string Pi in the RichTextBox named rchPi.
When you enter a pattern in the text box and click Search, the program executes the following code.
// Search for a pattern.
private void btnSearch_Click(object sender, EventArgs e)
{
// Get the pattern.
string pattern = txtPattern.Text;
// Remove all non-digits.
Regex reg_exp = new Regex("[^0-9]");
pattern = reg_exp.Replace(pattern, "");
// Search for the pattern.
int position = Pi.IndexOf(pattern);
// Display the result.
rchPi.Text = Pi;
if (position < 0)
lblPosition.Text = pattern + " was not found";
else
{
lblPosition.Text = pattern +
" found at digit " + position.ToString();
rchPi.Select(position, pattern.Length);
rchPi.SelectionBackColor = Color.Yellow;
rchPi.SelectionColor = Color.Red;
rchPi.ScrollToCaret();
}
}
This code gets the pattern that you entered. It then creates a Regex object that searches for non-digits and uses that pattern to replace non-digits with empty strings, thus removing the non-digits from the pattern.
Next the code searches Pi for the pattern. If the pattern does not appear, the code says so. If the pattern is contained in the string Pi, the program highlights it in the RichTextBox.
In fact, it is not known whether Pi contains every finite pattern of digits although most mathematicians believe it does. There are certainly some interesting patterns in there. For example, use this program to search for the pattern 999999 or 12345. This program only searches the first 100 thousand digits of Pi, however, so there are lots of patterns that it cannot find.
Some may be tempted to think there's some sort of mystical significance to the fact that Pi may contain every possible pattern. For example, if Pi really does contain all possible finite sequences of digits, then somewhere in there is the ASCII encoding of all of Shakespeare's works including all of his lost works. In that case, you could imagine that some divine being has hidden a message for you in Pi.
Of course the digits of Pi would also include ASCII encodings of every other conceivable piece of gibberish. They would include, "Go forth and bring peace unto the world," but they would also include, "Go forth and s#*9n GR&HH @F5& s-x r!KWY."
So use this program to look for your birthday, but don't take it all too seriously.
Download the example to experiment with it and to see additional details.
|