Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

## #1 2006-01-29 06:03:50

se7en
Member

Offline

### a program that searches for values of pi

You must surely have heard that 22/7 is approximately equal to pi. Well, I have found a much better value that approximates pi... 355/113. 355/113 gives 3.14159292 and pi = 3.14159265. 22/7 only gives you 3.142857143, which is obviously way off. I found this by writing a program that searches for values that give something close to pi, and this is what I came up with. I'm quite stoked. Here's the Qbasic code for the program:

'program that searches for values of pi

DEFINT A-Z
CLS

FOR x = 1 TO 1000
FOR y = 1 TO 1000
n& = x / y * 100000
IF n& = 314159 THEN
PRINT x; "/"; y; "="; x / y; "   ...press a key"
DO: LOOP WHILE INKEY\$ = ""
END IF
NEXT
NEXT

Here's the same thing but in C++...

//program that searches for values of pi
#include <iostream>
using namespace std;

int main() {
float m;
int n;
for (float x = 1; x <= 1000; x++) {
for (float y = 1; y <= 1000; y++) {
m = x / y * 100000;
n = int(m);
if (n == 314159) {
cout << x << " / " << y << " = " << x / y << "   ...press enter" << endl;
cin.get();
}
}
}
return 0;
}

## #2 2006-01-29 07:11:41

Ricky
Moderator

Offline

### Re: a program that searches for values of pi

You can improve the algorithm quite a bit by only testing x values which around a multiple of 3:

#include <iostream>
#include <math.h>
#include <iomanip>

using namespace std;

int main()
{
cout << setprecision(10);
for (double y = 1; y <= 100000; y++) {
for (double x = y*3; x/y < 22.0/7.0; x++) {
//cout << "HERE" << endl;
if (x / y > 3.1415926 && x / y < 3.1415927) {
cout << x << " / " << y << " = " << x / y << endl;
return 0;
}
}
}
return 0;
}

86953 / 27678 = 3.141592601

"In the real world, this would be a problem.  But in mathematics, we can just define a place where this problem doesn't exist.  So we'll go ahead and do that now..."

## #3 2006-01-29 07:52:21

siva.eas
Full Member

Offline

### Re: a program that searches for values of pi

355/113 has been known to the mathematical community for some time

## #4 2006-01-29 08:55:53

mathsyperson
Moderator

Offline

### Re: a program that searches for values of pi

Yes, I was about to say that I'd mentioned that a few times on the forum. But siva beat me to it. Well done! In fact, there's even a limerick to go with it:

In China, the Great Tsu-Chung Chi,
Was more clever with numbers than we.
"Lo!" he said, "I,
Have a value for pi,
Divide 3 5 5 by 1 1 3"

Having said that, well done for finding it. I'm sure if you continue further, you'll be able to find far better approximations.

Why did the vector cross the road?
It wanted to be normal.

## #5 2006-01-29 09:15:36

MathsIsFun

Offline

### Re: a program that searches for values of pi

Well done se7en! You discovered it on your own ... so one day you may discover something no one else has.

Have you found any more approximations like that one, and how close are they?

"The physicists defer only to mathematicians, and the mathematicians defer only to God ..."  - Leon M. Lederman

## #6 2006-01-29 10:10:32

mikau
Super Member

Offline

### Re: a program that searches for values of pi

In my oppinion it might be more interesting to write an algorthim to use numerical integretion to find pi by finding twice the area under the curve x^2 + y^2 = 1 and above the x axis. (the area of a cirle of radius 1 is pi.)

A logarithm is just a misspelled algorithm.

## #7 2006-01-29 10:29:19

Ricky
Moderator

Offline

### Re: a program that searches for values of pi

Using better values for pi, I got:

103993 / 33102 = 3.141592653

On a computer, you can't get more exact with 64bit float values (double).  However, I have long been thinking of making a float class which can represent n bit values by using arrays, maybe I'll start working on that now.

Last edited by Ricky (2006-01-29 10:30:27)

"In the real world, this would be a problem.  But in mathematics, we can just define a place where this problem doesn't exist.  So we'll go ahead and do that now..."

## #8 2006-01-29 10:57:36

mikau
Super Member

Offline

### Re: a program that searches for values of pi

Well just  wrote this program to use numerical integration of the area of a unit circle in the first quadrant, then multiply that by 4 to find pi. It works but using super large values of x seems to decrease the level of accuracy rather then increase it. Probably 'cause the values aren't being stored to enough decimal places and loosing their accuracy.

#### Code:

```#include <iostream>
#include <math.h>

float f(float x);

int main()
{

float x;

float n = 200000;

for (int i = 0; i < n; i++)
{

x += 1/n * f(i/n);

}

std::cout << "pi = " << 4 * x << "\n";

return 0;

}

float f(float x)
{

if ((1 - x*x) < 0) { std::cout << "error " << x << "\n"; return 0; }

return sqrt(1 - x*x);

}```

A logarithm is just a misspelled algorithm.

## #9 2006-01-29 11:05:27

siva.eas
Full Member

Offline

### Re: a program that searches for values of pi

103993 / 33102 was also known to the mathematical community for some time. There is a series they used to achieve these numbers.

## #10 2006-01-29 11:07:59

mikau
Super Member

Offline

### Re: a program that searches for values of pi

siva.eas, I doubt we're gonna make any never before made discoveries here. We're just having fun.

A logarithm is just a misspelled algorithm.

## #11 2006-02-05 19:42:20

krassi_holmz
Real Member

Offline

### Re: a program that searches for values of pi

very interesting problem. I'm sure there's very fast algoritm.

IPBLE:  Increasing Performance By Lowering Expectations.

## #12 2006-02-05 19:50:44

krassi_holmz
Real Member

Offline

### Re: a program that searches for values of pi

I wrote some topics and now have some code.
Here's it in Mathematica:

#### Code:

```<< NumberTheory`Rationalize`
RAPi[n_] := AffineRationalize[{N[Pi,n]}, n]
(*RAPi function gives the simplest rational approximation of Pi with n correct demical places*)
(*2.2006*)```

Last edited by krassi_holmz (2006-02-05 20:11:08)

IPBLE:  Increasing Performance By Lowering Expectations.

## #13 2006-02-05 20:07:56

krassi_holmz
Real Member

Offline

### Re: a program that searches for values of pi

Here are some results:

#### Code:

`Table[ToString[i] <> "::" <> ToString[Numerator[RAPi[i]]] <> "/" <> ToString[Denominator[RAPi[i]]] , {i, 1, 100}]`

#### Code:

```{1::{3}/{1},
2::{3}/{1},
3::{22}/{7},
4::{22}/{7},
5::{355}/{113}, \
6::{355}/{113},
7::{355}/{113},
8::{355}/{113},
9::{355}/{113}, \
10::{104348}/{33215},
11::{312689}/{99532},
12::{1146408}/{364913}, \
13::{1146408}/{364913},
14::{5419351}/{1725033},
15::{5419351}/{1725033}, \
16::{80143857}/{25510582},
17::{411557987}/{131002976}, \
18::{1068966896}/{340262731},
19::{2549491779}/{811528438}, \
20::{6167950454}/{1963319607}, \
21::{21053343141}/{6701487259},
22::{21053343141}/{6701487259}, \
23::{21053343141}/{6701487259},
24::{21053343141}/{6701487259}, \
25::{3587785776203}/{1142027682075},
26::{8958937768937}/{2851718461558}, \
27::{8958937768937}/{2851718461558},
28::{139755218526789}/{44485467702853}, \
29::{428224593349304}/{136308121570117}, \
30::{428224593349304}/{136308121570117}, \
31::{428224593349304}/{136308121570117}, \
32::{6134899525417045}/{1952799169684491}, \
33::{30246273033735921}/{9627687726852338}, 34::{66627445592888887}/{21208174623389167},
35::{430010946591069243}/{136876735467187340}, \
36::{430010946591069243}/{136876735467187340}, \
37::{2646693125139304345}/{842468587426513207}, \
38::{2646693125139304345}/{842468587426513207}, \
39::{2646693125139304345}/{842468587426513207}, \
40::{2646693125139304345}/{842468587426513207}, \
41::{265099323460521503743}/{84383735478118508040}, \
42::{792651277256425206884}/{252308737846929010913}, \
43::{1850401877973371917511}/{589001211171976529866}, \
44::{11895062545096656711950}/{3786316004878788190109}, \
45::{37535589513263342053361}/{11947949225808341100193}, \
46::{37535589513263342053361}/{11947949225808341100193}, \
47::{436681609736090076010871}/{139000073493649328482341}, \
48::{436681609736090076010871}/{139000073493649328482341}, \
49::{2857198258041217165097342}/{909474452321624805685313}, \
50::{2857198258041217165097342}/{909474452321624805685313}, \
51::{26151465932107044561886949}/{8324270144388272579650158}, \
52::{26151465932107044561886949}/{8324270144388272579650158}, \
53::{232505995130922183891885199}/{74008956847172828411166109}, \
54::{671366519460659507113768648}/{213702600397130212653848169}, \
55::{2246605553512900705233191143}/{715116758038563466372710616}, \
56::{2246605553512900705233191143}/{715116758038563466372710616}, \
57::{18644210947563865148979297792}/{5934636664705637943635533097}, \
58::{126016265525921254632388702258}/{40112223136862338672703310447}, \
59::{126016265525921254632388702258}/{40112223136862338672703310447}, \
60::{126016265525921254632388702258}/{40112223136862338672703310447}, \
61::{5005608804695235355702822685879}/{1593334768903120834487234062302}, \
62::{5005608804695235355702822685879}/{1593334768903120834487234062302}, \
63::{41629395862109680461101929914153}/{13251048258768098728278481645552}, \
64::{41629395862109680461101929914153}/{13251048258768098728278481645552}, \
65::{397982065092389582058319745527393}/{126681625842748498335889192663895}, \
66::{754334734322669483655537561140633}/{240112203426728897943499903682238}, \
67::{2660986268060398033024932428949292}/{847018236122935192166388903710609}, \
68::{2660986268060398033024932428949292}/{847018236122935192166388903710609}, \
69::{38406124552260631528062911311958114}/{12225049134990570086608833748294659}, \
70::{38406124552260631528062911311958114}/{12225049134990570086608833748294659}, \
71::{212564178171463672420858478430244273}/{67661279360509603072431780067475929}, \
72::{1139633139961839625160418214775137593}/{362756495072529155535376567833968963}, 73::{5022067040742546477011452727272997032}/{1598573588146126398372978665219122369},
74::{13926567982265799805873939967043853503}/{4432964269365850039583559427823398144}, 75::{13926567982265799805873939967043853503}/{4432964269365850039583559427823398144},
76::{60728338969805745700507212595448411044}/{19330430665609526556707216376512714945},\
77::{256839923861488782607902790348837497679}/{81754686931803956266412424933874257924}, 78::{256839923861488782607902790348837497679}/{81754686931803956266412424933874257924},\
79::{4427007044615115050034854648525685871587}/{1409160108506276783085718440252375099653},\
80::{4427007044615115050034854648525685871587}/{1409160108506276783085718440252375099653},\
81::{4427007044615115050034854648525685871587}/{1409160108506276783085718440252375099653},\
82::{4427007044615115050034854648525685871587}/{1409160108506276783085718440252375099653},\
83::{4427007044615115050034854648525685871587}/{1409160108506276783085718440252375099653},\
84::{716918301303787149323038550270812273699415}/{228202182891085034903619974895950891885862},\
85::{716918301303787149323038550270812273699415}/{228202182891085034903619974895950891885862},\
86::{716918301303787149323038550270812273699415}/{228202182891085034903619974895950891885862},\
87::{32982668867018823983909808167105890276044677}/{1049870957309841788234960456365399340184935},
88::{32982668867018823983909808167105890276044677}/{10498709573098417882349604563653993401849305},\
89::{32982668867018823983909808167105890276044677}/{10498709573098417882349604563653993401849305},\
90::{790867134507147988464512357460270554351372833}/{251740827571470944141486889552799890752497458},\
91::{2339618734654425141409627264213705772778073822}/{744723773141314414542111064094745678855643069},\
92::{7809723338470423412693394150101387872685594299}/{2485912146995414187767820081837036927319426665},\
93::{44518721296168115334750737636394621463335491972}/{14170749108831170712064809426927475885060916921},\
94::{125746440550033922591558818759082476517320881617}/{40026335179498097948426608198945390727863324098},\
95::{125746440550033922591558818759082476517320881617}/{40026335179498097948426608198945390727863324098},
96::{125746440550033922591558818759082476517320881617}/{40026335179498097948426608198945390727863324098},\
97::{3188179735047016180123721206613456534396357532397}/{1014829128596283619422730014400562244081644019371},\
98::{12626972499638030797903326007694743661068109247971}/{4019290179205636379742493449403303585598712753386},\
99::{12626972499638030797903326007694743661068109247971}/{4019290179205636379742493449403303585598712753386},\
100::{59946682763143137809392908831860261770944188707458}/{19081621767431898279289737232615955683911919747559}}```

Last edited by krassi_holmz (2006-02-05 20:16:05)

IPBLE:  Increasing Performance By Lowering Expectations.

## #14 2006-02-06 04:06:57

John E. Franklin
Star Member

Offline

### Re: a program that searches for values of pi

Wow, that Mathematica is really something!  How long did it take Mathematica to compute those 100 answers?

Imagine for a moment that even an earthworm may possess a love of self and a love of others.

## #15 2006-02-06 04:29:12

krassi_holmz
Real Member

Offline

### Re: a program that searches for values of pi

I'll see.

IPBLE:  Increasing Performance By Lowering Expectations.

## #16 2006-02-06 04:37:13

krassi_holmz
Real Member

Offline

### Re: a program that searches for values of pi

There's a build-in function Timing that returns CPU time for execution.
Here's the code:
in[1]:=

#### Code:

`Timing[Table[ToString[i] <> "::" <> ToString[Numerator[RAPi[i]]] <> "::" <> ToString[ Denominator[RAPi[i]]] , {i, 1, 100}];]`

out[1]:=

#### Code:

`{0.375 Second, Null}`

That's less than a second.
And my computer is not from the fastest.
I have 256MB RAM and 1.5GHz AMD

IPBLE:  Increasing Performance By Lowering Expectations.

## #17 2006-03-26 02:13:52

Skapare
Novice

Offline

### Re: a program that searches for values of pi

The following are programs I wrote a while back to take the actual value of PI (calculated elsewhere) or any other number desired, and find increasingly closer integer fractions/ratios that approximate that value.  The first is written in C and requires LIBGMP (free open source software).  The second is written in PIKE (free open source software).

http://phil.ipal.org/intfrac.c

http://phil.ipal.org/intfrac.pike

Last edited by Skapare (2006-03-26 02:18:41)

## #18 2006-03-26 15:45:38

MathsIsFun

Offline

### Re: a program that searches for values of pi

Looks good Skapare. As a side note, I have a Full Precision Calculator in javascript. It only has basic functionality, but one day I hope to make it do more. (Obviously not as fast as C and not programmable, but still useful)

"The physicists defer only to mathematicians, and the mathematicians defer only to God ..."  - Leon M. Lederman