473,806 Members | 2,321 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

algorithm for finding Pi in C

42 3835
On May 26, 4:38*am, Richard Heathfield <r...@see.sig.i nvalidwrote:
James Dow Allen said:
On May 24, 5:41 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
spinoza1111 said:
On May 24, 2:44 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
Presumably you want Pi? Easy. It's about 3.
I'm disappointed no one in the thread mentioned the
almost unbelievable expression due to Ramanujan:
\frac{1}{\pi} = \frac{2\sqrt{2} }{9801}
* * *\sum^\infty_{k =0} \frac{(4k)!(110 3+26390k)}{(k!) ^4 396^{4k}}

But someone did mention it. In case you missed the article, the message ID
is <4afeda9c-ee40-43de-a0ac-e61dd8b84...@f2 4g2000prh.googl egroups.com-
check it out!
Admittedly, this formula may have little interest for those
for whom "about 3" is an adequate approximation.

<grinWell, "about 3" *is* an adequate approximation - on occasion. That
doesn't mean it's adequate for *all* uses.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Well I also learned a sentence for PI to 20 something digits, but I
forgot it. It may be wandering somewhere online...
Jun 27 '08 #21
no************* @gmail.com said:
On May 26, 4:38 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
<snip>
><grinWell, "about 3" *is* an adequate approximation - on occasion.
That doesn't mean it's adequate for *all* uses.

Well I also learned a sentence for PI to 20 something digits, but I
forgot it. It may be wandering somewhere online...
Sir, I bear a rhyme excelling
In mystic force and magic spelling;
Celestial sprites elucidate
All my own striving can't relate.

From memory - except for one word which I wasn't sure that I'd remembered
properly, so I just whizzed through the first twenty places of pi in my
head to check that, even if my choice ("striving") was wrong, at least it
had the right number of letters. That's the trouble with mnemonics -
they're so hard to remember.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #22
In article <fe************ *************** *******@8g2000h se.googlegroups .com>,
no************* @gmail.com <no************ *@gmail.comwrot e:
>Well I also learned a sentence for PI to 20 something digits, but I
forgot it. It may be wandering somewhere online...
Now I, even I, would celebrate in rhymes unapt
The great immortal Syracusan rivaled[*] nevermore
Who in his wondrous lore passed on before
Gave men his guidance how to circles mensurate.
[*] Must be American

-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
Jun 27 '08 #23
Richard Heathfield wrote:
no************* @gmail.com said:
>On May 26, 4:38 am, Richard Heathfield <r...@see.sig.i nvalidwrote:

<snip>
>><grinWell, "about 3" *is* an adequate approximation - on occasion.
That doesn't mean it's adequate for *all* uses.

Well I also learned a sentence for PI to 20 something digits, but I
forgot it. It may be wandering somewhere online...

Sir, I bear a rhyme excelling
In mystic force and magic spelling;
Celestial sprites elucidate
All my own striving can't relate.
Or locate they who can cogitate
And so finally terminate. Finis.

Extends Pi to 31 digits...

Bye, Jojo
Jun 27 '08 #24

"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:DZ******** *************** *******@bt.com. ..
no************* @gmail.com said:
>On May 26, 4:38 am, Richard Heathfield <r...@see.sig.i nvalidwrote:

<snip>
>><grinWell, "about 3" *is* an adequate approximation - on occasion.
That doesn't mean it's adequate for *all* uses.

Well I also learned a sentence for PI to 20 something digits, but I
forgot it. It may be wandering somewhere online...

Sir, I bear a rhyme excelling
In mystic force and magic spelling;
Celestial sprites elucidate
All my own striving can't relate.

From memory - except for one word which I wasn't sure that I'd remembered
properly, so I just whizzed through the first twenty places of pi in my
head to check that, even if my choice ("striving") was wrong, at least it
had the right number of letters. That's the trouble with mnemonics -
they're so hard to remember.
Good thing there wasn't a '0' in the first 20 places.

--
bartc
Jun 27 '08 #25
Bartc said:
"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:DZ******** *************** *******@bt.com. ..
<snip>
>>
Sir, I bear a rhyme excelling
In mystic force and magic spelling;
Celestial sprites elucidate
All my own striving can't relate.

From memory - except for one word which I wasn't sure that I'd
remembered properly, so I just whizzed through the first twenty places
of pi in my head to check [...]. That's the trouble
with mnemonics - they're so hard to remember.

Good thing there wasn't a '0' in the first 20 places.
I am given to understand that the canonical technique for '0' is to use a
ten-letter word (e.g. "understand ").

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #26
"pete" <pf*****@mindsp ring.comwrote in message
news:e7******** *************** *******@earthli nk.com...
aa*****@gmail.c om wrote:
>Hi all,

see:- http://mathforum.org/library/drmath/view/54456.html

This works for me:

#include <float.h>
/*
** pi == (atan(1.0 / 3) + atan(1.0 / 2)) * 4
*/
static double fs_pi(void);

static double fs_pi(void)
{
unsigned n;
double a, b;
static double p;
static int initialized;

if (!initialized) {
initialized = 1;
n = 1;
a = 3;
do {
a /= 9;
b = a / n;
n += 2;
a /= 9;
b -= a / n;
n += 2;
p += b;
} while (b DBL_EPSILON / 4);
n = 1;
a = 2;
do {
a /= 4;
b = a / n;
n += 2;
a /= 4;
b -= a / n;
n += 2;
p += b;
} while (b DBL_EPSILON / 2);
p *= 4;
}
return p;
}
It's not really any better, but p can do double duty:

#include <float.h>
/*
** pi == (atan(1.0 / 3) + atan(1.0 / 2)) * 4
*/
static double fs_pi(void);

static double fs_pi(void)
{
unsigned n;
double a,
b;
static double p = 0;

if (!p) {
n = 1;
a = 3;
do {
a /= 9;
b = a / n;
n += 2;
a /= 9;
b -= a / n;
n += 2;
p += b;
} while (b DBL_EPSILON / 4);
n = 1;
a = 2;
do {
a /= 4;
b = a / n;
n += 2;
a /= 4;
b -= a / n;
n += 2;
p += b;
} while (b DBL_EPSILON / 2);
p *= 4;
}
return p;
}

#ifdef UNIT_TEST
#include <stdio.h>
#include <math.h>
int main(void)
{
const double d = fs_pi();
printf("Pi approximation = %30.20g\n", d);
printf("Error is approximately %30.20g\n",
fabs(d - 3.1415926535897 932384626433832 795028841971693 993751));
return 0;
}
#endif
** Posted from http://www.teranews.com **
Jun 27 '08 #27
On May 26, 8:10 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
noagbodjivic... @gmail.com said:
On May 26, 4:38 am, Richard Heathfield <r...@see.sig.i nvalidwrote:

<snip>
<grinWell, "about 3" *is* an adequate approximation - on occasion.
That doesn't mean it's adequate for *all* uses.
Well I also learned a sentence for PI to 20 something digits, but I
forgot it. It may be wandering somewhere online...

Sir, I bear a rhyme excelling
In mystic force and magic spelling;
Celestial sprites elucidate
All my own striving can't relate.

From memory - except for one word which I wasn't sure that I'd remembered
properly, so I just whizzed through the first twenty places of pi in my
head to check that, even if my choice ("striving") was wrong, at least it
had the right number of letters. That's the trouble with mnemonics -
they're so hard to remember.
see:- http://en.wikiquote.org/wiki/English...s_mnemonics#Pi
Jun 27 '08 #28
On May 26, 3:38*pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
James Dow Allen said:
I'm disappointed no one in the thread mentioned the
almost unbelievable expression due to Ramanujan:

But someone did mention it. In case you missed the article,
the message ID
is <4afeda9c-ee40-43de-a0ac-
e61dd8b84...@f2 4g2000prh.googl egroups.com-
check it out!
Color me gullible, but I did search for this message,
which, in Google, seems to need an "Advanced" option.

But I agree: Great article!
>
<grinWell, "about 3" *is* an adequate approximation
- on occasion. That
doesn't mean it's adequate for *all* uses.
I wanted to post a rejoinder based on the famous
Tennessee legislation, but I discover that's an
urban legend. Indiana did, once, *almost* legislate
a rational value for pi, as shown in an
article by Mark Brader, of rec.puzzles fame:
http://www-personal.umich.edu/~jlawler/aux/pi.html

While Googling for pi = 3, I also found that
this approximation is dictated by the Bible
in Chronicles II: 4:2-5.

Anyway, AFAIK, bank software doesn't use trig.
How *did* you divert all those half-farthings to
your account, Richard? :-)

James
Jun 27 '08 #29
James Dow Allen said:
On May 26, 3:38 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
>James Dow Allen said:
I'm disappointed no one in the thread mentioned the
almost unbelievable expression due to Ramanujan:

But someone did mention it. In case you missed the article,
the message ID
is <4afeda9c-ee40-43de-a0ac-
e61dd8b84...@f 24g2000prh.goog legroups.com-
check it out!

Color me gullible,
No such word.
I wanted to post a rejoinder based on the famous
Tennessee legislation, but I discover that's an
urban legend. Indiana did, once, *almost* legislate
a rational value for pi,
Oddly, a rational value of pi is irrational, whereas an irrational value is
rational.
as shown in an
article by Mark Brader, of rec.puzzles fame:
and indeed comp.lang.c fame (erstwhilely).
While Googling for pi = 3, I also found that
this approximation is dictated by the Bible
in Chronicles II: 4:2-5.
Hardly a diktat - merely a reasonable estimate, and not at all bad for 2500
years ago. Even Archimedes (250 or so years later) could only manage to
establish that the true value was somewhere between 223/71 and 7/2.
Anyway, AFAIK, bank software doesn't use trig.
How *did* you divert all those half-farthings to
your account, Richard? :-)
I could tell you, but then I'd have to bankrupt you.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
9938
by: Bernard Fields | last post by:
Greets, all. As the title suggests, I'm trying to make a maze. Specifically, it's a top-down, 2-d maze, preferably randomly generated, though I'm willing to forego that particular aspect as this point. I've done many, many web-searches, but nothing that I've found so far has provided any clues....
3
2759
by: ckroom | last post by:
Anyone knows a good algorithm to get the next prime of a number n? prime = nextprime( n ); It's for some stuff about double rehashing, thanks. -- ckroom http://nazaries.net/~ckroom
4
5638
by: m sergei | last post by:
I am not asking for code but wanted help with understanding the algorithm to permute all characters of a string. say string is "ABCD" I want to know the algorithm for finding all permutations of the given string, without recursion and with recursion.
12
14872
by: Erik the Red | last post by:
In Fundamental Algorithms (The Art of Computer Programming), the first algorithm discussed is Euclid's Algorithm. The only idea I have of writing this in python is that it must involve usage of the modulo % sign. How do I write this in python?
16
6773
by: a | last post by:
We are writing an app that assigns people to teams based on their curent score. Teams are 8 people, there are 2 teams. (i would like it to be flexible, but this is a start). I need an algorithm that creates the 2 teams such that the total score for each team is as close as possible. Any ideas?? Thanks!
2
2150
by: Sherrie Laraurens | last post by:
Hi all, I'm trying to write a generic algorithm routine that will take begin and end iterators of a container, iterate through the range and perform a "calculation" of sorts. The trouble is that the algorithm will behave differently for the two different types. I've considered calling the algorithm foo_A and foo_B, but I don't like that approach because it will blow out in naming complexity down the track.
4
2642
by: sklett | last post by:
I realize this could be a little off-topic, but there are some great minds on this NG and I hope you can let me slide this time ;0) I'm designing our system to manage what products can fit in which cartons and how many per carton. For example: Widget A measures 10W x 10H x 10D Carton A measures 20W x 10H x 10D Carton B measures 50W x 10H x 10D If I'm fulfilling an order for 2 Widget As I can choose Carton A as it will
4
32087
prometheuzz
by: prometheuzz | last post by:
Hello (Java) enthusiasts, In this article I’d like to tell you a little bit about graphs and how you can search a graph using the BFS (breadth first search) algorithm. I’ll address, and hopefully answer, the following questions: • what is a graph? • how can a graph be represented as an ADT? • how can we search/walk through a graph using the BFS algorithm?
2
4664
by: shashankbs | last post by:
Given a topology and a certain node X, find the shortest path tree with X as the root. * Input: a topology file similar to the following (which is a three-node ring) -------------------------------------------- Nodes: (3) // there are three nodes (node 0, 1 and 2) Edges: (3)
1
14215
by: Glenton | last post by:
Hi All Here is a very simple little class for finding a shortest route on a network, following Dijkstra's Algorithm: #!/usr/bin/env python #This is meant to solve a maze with Dijkstra's algorithm from numpy import inf from copy import copy
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10371
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10110
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.