Quote:WizardCan anyone get improve upon 355/113, where the degree of success is the number of total digits saved, in this case 1 (using 6 to get pi right to 7 digits).

Well, I can't. I tried.

A few close calls that match the degree of success of 1:

21 digits in fraction, 22 accuracy

21053343141 / 6701487259 = 3.1415926535897932384623

37 digits in fraction, 38 accuracy:

2646693125139304345 / 842468587426513207 = 3.14159265358979323846264338327950288418

At any rate, the key theorem that guarantees and endless supply of these interesting fractions is Hurwitz's Theorem.

Very roughly speaking, for any irrational number, there are infinitely many fractions that give accuracy of approximation that is double the number of digits (or better) in the denominator of the fraction. So 355/113 gives 6 places (113 has 3 decimals). The fraction given by EdCollins above has 18 digits in the denominator and 38 digits in accuracy.

When I taught intro to programming at UCSB, this was one of my standard first projects:

=========================

Programming Assignment 3 (25 points)

Engr 3, Fall 2000

Due: Wednesday, October 25, 2000, 11:59 PM.

Instructions: Complete the following parts.

Part A (10 points) The number 153 is very interesting. In fact, 153 = 1*1*1 + 5*5*5 + 3*3*3, that is, 153 is the sum of the cube of its digits: 153 = 1^3 + 5^3 + 3^3. Your task in this programming project is to write a program that determines all integers between 100 and 999 that are the sum of the cubes of their digits. That is, find all numbers xyz such that xyz = x^3 + y^3 + z^3. The name of this program must be “Gath.c” . (Question: why Gath?) The grader will run your program to verify that your program outputs the correct numbers in a reasonably brief period of time.

Part B (15 points) The value of pi to 15 decimal places is PI = 3.141592653589793 . . . Your job is to write a program that can be used to find decent approximations to PI. More specifically, your program must find integers N and D such that the fraction N/D is the best possible approximation to PI and such that D < 1,000,000.

For example, 3/1 = 3.0 is fairly lame, 22/7 = 3.1428 is pretty good, but 355/113 = 3.14159292 is even better. A really bad try is 3141592/1000000 = 3.141592, which isn’t even as good as 355/113. So, what’s the best you can do? Remember, you can use any denominator at all, but it must be less than 1,000,000.

You will need to use the absolute value function fabs and include math.h (see page 767). Also, to get your program to print out a double to 15 decimal places you will need to use the qualifier %1.15f instead of just %f in the printf statement where you print out your results.

The name of this program must be “findPi.c” A working sample program is available in ~teliot/ENG3/pa3. However, this program only solves the problem for a maximum denominator of 100,000. Note that the program prints increasingly accurate approximations. You must consider questions of efficiency for this program, a program that does not quickly print out the correct answer will not receive full credit.

Quote:teliotAlso, to get your program to print out a double to 15 decimal places you will need to use the qualifier %1.15f instead of just %f in the printf statement where you print out your results.

Not that it matters much, but this is wrong. The qualifier should be %17.15f.

The number before radix is the total width of the output.

I'm not sure what I don't know here, not that it matters.Quote:weaselmanNot that it matters much, but this is wrong. The qualifier should be %17.15f.

The number before radix is the total width of the output.

#include <stdio.h>

main() {

double pi = 3.14159265358979323;

printf("%17.15f\n%1.15f\n", pi, pi);

}

==================

teliot@JGLLC-UB:~$ ./foo

3.141592653589793

3.141592653589793

teliot@JGLLC-UB:~$

Quote:teliot

Your task in this programming project is to write a program that determines all integers between 100 and 999 that are the sum of the cubes of their digits. That is, find all numbers xyz such that xyz = x^3 + y^3 + z^3.

I expected more than this, but I come up with just four. 153, 370, 371, and 407.

Your class sounds like it was a lot of fun.

See this for more information about the number 153. It's really quite an amazing number. If you look it up on Google, you will find a lot more, especially its use and possible meaning in the Bible.Quote:EdCollinsI expected more than this, but I come up with just four. 153, 370, 371, and 407.

Your class sounds like it was a lot of fun.

Here is a problem that I really enjoyed giving my first quarter Freshmen. Try writing this and having your program print out the fractions with MAX = 1000.Quote:EdCollinsI expected more than this, but I come up with just four. 153, 370, 371, and 407.

Your class sounds like it was a lot of fun.

Part D (10 points) The name of this program is “fractions.c”. In it you will ask the user for a single integer, called MAX. Your program will proceed to print out all the fractions between 0 and 1, in lowest terms, with a denominator between 1 and MAX (inclusive), in increasing order. For example, if you input a value of

MAX = 5, then your program will print out:

0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1

Quite a few of the approximations and formulas come from mathematician Srinivasa Ramanujan.

This one converges so slowly, it takes 300 terms before it is accurate to 2 decimal places:

Pi = 4(1-

^{1}/

_{3}+

^{1}/

_{5}-

^{1}/

_{7}+

^{1}/

_{9}...)

This one only uses the number 2 to calculate Pi:

Pi = 2 x (

^{2}/

_{√2}) x (

^{2}/

_{√(2+√2)}) x (

^{2}/

_{√(2+√(2+√2))})...

A formula for Pi that has Pi in it:

Pi = (1+

^{1}/

_{Pi})

^{1+Pi}

With continued fractions:

^{4}/

_{Pi}= 1+

^{1}/

_{(3+4/(5+9/(7+16/(9+...}

And more:

^{4}/

_{Pi}=

^{1}/

_{(1+1²/(2+3²/(2+5²/(2+7²...}

Pi = √(6 x (1+

^{1}/

_{4}+

^{1}/

_{9}+

^{1}/

_{16}+

^{1}/

_{25}...))

^{Pi}/

_{2}= 1 +

^{1}/

_{3}(1+

^{2}/

_{5}(1+

^{3}/

_{7}(1+

^{4}/

_{9}(1+...))))

An approximation:

Pi ≈ (

^{2143}/

_{22})

^{1/4}

Correct to 8 places:

Pi ≈

^{99²}/

_{2206√2}