Thread Rating:

EdCollins
Joined: Oct 21, 2011
• Threads: 18
• Posts: 1292
November 12th, 2011 at 11:25:48 PM permalink
Quote: Wizard

Can 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
Ibeatyouraces
Joined: Jan 12, 2010
• Threads: 68
• Posts: 11933
November 13th, 2011 at 8:26:30 AM permalink
deleted
DUHHIIIIIIIII HEARD THAT!
teliot
Joined: Oct 19, 2009
• Threads: 39
• Posts: 2209
November 13th, 2011 at 8:36:18 AM permalink
I don't know if this has been mentioned elsewhere in this thread, but this discussion falls under the topic of "continued fractions." So very long ago now, I used to teach this material at Ohio University. I designed an entire introductory number theory course around the book "Continued Fractions" by C.D. Olds.

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.
Poetry website: www.totallydisconnected.com
weaselman
Joined: Jul 11, 2010
• Threads: 20
• Posts: 2349
November 13th, 2011 at 9:08:27 AM permalink
Quote: teliot

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.

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.
"When two people always agree one of them is unnecessary"
teliot
Joined: Oct 19, 2009
• Threads: 39
• Posts: 2209
November 13th, 2011 at 9:15:00 AM permalink
Quote: weaselman

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.

`#include <stdio.h>main() {  double pi = 3.14159265358979323;  printf("%17.15f\n%1.15f\n", pi, pi);}==================teliot@JGLLC-UB:~\$ ./foo 3.1415926535897933.141592653589793teliot@JGLLC-UB:~\$ `
Poetry website: www.totallydisconnected.com
EdCollins
Joined: Oct 21, 2011
• Threads: 18
• Posts: 1292
November 13th, 2011 at 9:21:00 AM permalink
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.
weaselman
Joined: Jul 11, 2010
• Threads: 20
• Posts: 2349
November 13th, 2011 at 9:21:29 AM permalink
Well 1.15 happens to appear to work in this case, because the width qualifier (1) is invalid (the output can't be fit into one character), so it gets ignored. You may also want to try ".15f" and "20.15f" to see how it works.
"When two people always agree one of them is unnecessary"
teliot
Joined: Oct 19, 2009
• Threads: 39
• Posts: 2209
November 13th, 2011 at 11:46:25 AM permalink
Quote: EdCollins

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.
Poetry website: www.totallydisconnected.com
teliot
Joined: Oct 19, 2009
• Threads: 39
• Posts: 2209
November 13th, 2011 at 11:57:40 AM permalink
Quote: EdCollins

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.

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.

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
Poetry website: www.totallydisconnected.com
Dween
Joined: Jan 24, 2010
• Threads: 66
• Posts: 339
November 14th, 2011 at 5:38:12 AM permalink
In the book "A Passion for Mathematics", Clifford A. Pickover published a number of formulas and fractions that approximate or calculate Pi.

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+/(2+/(2+/(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
-Dween!

• Jump to: