Finding the value of pi=3.141592653589793238...
Author |
Message |
AtoZ
|
Posted: Sat Jan 31, 2009 5:51 pm Post subject: Finding the value of pi=3.141592653589793238... |
|
|
This program find the value of pi to some number of digits using the method to find an accurate value. Unfortunately, household computers are not very powerful and neither is the turing environment, therefore, it only calculates to 6 digits quickly. For my computer, with a speed of around 1.3mil loops/sec, it takes 25 seconds to get 7 digits, but only 3 seconds to get 6 digits.
Also, it calculates pi to 49 digits (which is the max in turing i think), but the correct digits are only limited to maybe 5-7.
If you have time or a very fast and powerful computer, run it for maybe 8-10 digits, and let me know if it works properly.
This program also finds your computer speed and your processor number and operating system.
If you post about the run time of the program, plz also post ur computer speed with it.
Oh btw, i copied the method to find the computer speed from a turing submission by Zren from this link:http://compsci.ca/v3/viewtopic.php?t=19986. So, sorry for copying, but it is not a major part of the program.
Turing: |
% 4(1-1/3+1/5-1/7+...)= pi
/*
Running time in ms Number of digits (after the decimal) of pi you want.
12 0
12 1
12 2
13 3
35 4
265 5
2550 6
25600 7
These times are for a computer with a speed of
around 1.3 million loops/second.
*/
var correctdigits : int := 0
locate (20, 1)
put "~~~ Note: Any value higher than 6 will take very long to process.~~~"
locate (21, 1)
put "For 6 digits, it takes approximately 2500 ms."
put "After which the processing time increases by 10 times."
locate (1, 1)
put "Hello!"
put "To how many correct digits of pi would you like to see: " ..
get correctdigits
const CONST := (10 ** correctdigits )
const digits := 49
var x : array 1 .. CONST of real
var y : array 1 .. CONST of real
var answer1 : real
var answer2 : real
var total : real
var pi : real
for i : 1 .. CONST
x (i ) := 1 / (- 1 * (i * 4 - 1))
end for
for i : 1 .. CONST
y (i ) := 1 / (i * 4 - 3)
end for
answer1 := 0
for i : 1 .. CONST
answer1 := answer1 + x (i )
end for
answer2 := 0
for i : 1 .. CONST
answer2 := answer2 + y (i )
end for
total := answer2 + answer1
pi := 4 * total
put ""
put "The value of pi to the calculated digits: "
put pi : 0 : digits
put ""
put "The value of pi to the correct digits: " ..
put pi : 0 : correctdigits
var speed : int := 0
function compSpeed : int
var tstart : int := Time.Sec
var t, cycle : int := 0
loop
t := Time.Sec
exit when t > tstart + 1
cycle + = 1
end loop
result cycle
end compSpeed
speed := compSpeed
put ""
put ""
put "Your compter speed: " ..
put speed, " (loops/sec)"
put ""
put "Operating System: ", Config.Machine (cmOS )
put "Processor Number: ", Config.Machine (cmProcessor ) |
MOD EDIT: Remember the code tags!
Description: |
The actual program, either download it, or copy and paste it in the turing environment. |
|
Download |
Filename: |
pi2.t |
Filesize: |
1.89 KB |
Downloaded: |
207 Time(s) |
Description: |
This is just a secondary file, which shows the run-time for different amounts of digits. The actual program which is slightly more versatile, is the next attached file, which is the same as the copied and pasted code in the message body. |
|
Download |
Filename: |
pi.t |
Filesize: |
1.86 KB |
Downloaded: |
181 Time(s) |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Ultrahex
|
Posted: Sat Jan 31, 2009 11:08 pm Post subject: Re: Finding the value of pi=3.141592653589793238... |
|
|
Using your code:
code: | 1 digits - 3.1 - Time: 0.019 seconds
2 digits - 3.14 - Time: 0.025 seconds
3 digits - 3.141 - Time: 0.100 seconds
4 digits - 3.1415 - Time: 0.152 seconds
5 digits - 3.14159 - Time: 0.628 seconds
6 digits - 3.141592 - Time: 4.794 seconds
7 digits - 3.1415926 - Time: 44.842 seconds
8 digits - failed (due to limit of dynamic allocation of an array) |
Using my modification of your code (same algorithm):
(slower due to printing of statements, cause of other processes plus allocation of memory)
code: | 1 digits - 3.1 - Time: 0.019 seconds
2 digits - 3.14 - Time: 0.346 seconds
3 digits - 3.141 - Time: 0.357 seconds
4 digits - 3.1415 - Time: 0.431 seconds
5 digits - 3.14159 - Time: 0.879 seconds
6 digits - 3.141592 - Time: 4.622 seconds
7 digits - 3.1415926 - Time: 52.552 seconds
8 digits - 3.14159265 - Time: 430.287 seconds
9 digits - Integer Overflow |
1.4GHZ amd thunderbird
emulated using wine on linux
similar program written in C
code: | 1 digits - 3.1 - Time: 0.000 seconds
2 digits - 3.14 - Time: 0.000 seconds
3 digits - 3.141 - Time: 0.000 seconds
4 digits - 3.1415 - Time: 0.000 seconds
5 digits - 3.14159 Time: 0.000 seconds
6 digits - 3.141592 - Time: 0.020 seconds
7 digits - 3.1415926 - Time: 0.310 seconds
8 digits - 3.14159265 - Time: 3.100 seconds
9 digits - 3.14159265(8?3) - Time: 31.750 seconds - INCORRECT (due to overflow) |
Or if you want even better (14 digits, 0.179 seconds):
Turing: |
var a := 1. 0
var a2:=a
var b:= 1. 0/ sqrt(2. 0)
var t:= 1. 0/ 4
var p:= 1
for i : 1 .. 3
a2 := (a+b )/ 2
b := sqrt(a*b )
t := t-p* (a-a2 )** 2
p := 2*p
a := a2;
end for
var pi := (a+b )** 2/ (4*t )
put pi: 0: 14
put "Time: ", Time.Elapsed () / 1000, " seconds"
|
|
|
|
|
|
|
The_Bean
|
Posted: Sun Feb 01, 2009 9:26 am Post subject: Re: Finding the value of pi=3.141592653589793238... |
|
|
I would have to say memorizing the number of digits you want is a lot faster. Sure it might take 2 months for 155 decimal places, but then it only takes roughly 30 seconds to type it in. You can also use random sections from it for passwords.
Turing: |
View.Set ("graphics:500,500,nobuttonbar,position:center,center")
var pi : string := "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111"
var timeStart : int
var input : char
Text.Colour (42)
Text.ColourBack (7)
cls
put "Enter Pi to 155 decimal places!"
loop
exit when hasch
end loop
timeStart := Time.Elapsed
for i : 1 .. 157
input := getchar
if input = pi (i ) then
put input ..
else
put "E"
put "You messed up at the number: ", i - 2, " decimal place."
exit
end if
end for
put ""
put "That took: ", (Time.Elapsed - timeStart ) / 1000
|
|
|
|
|
|
|
michaelp
|
|
|
|
|
|
|