• #### calculating pi [Re: php and big data and predictive modelling...]

From robamman2019@gmail.com@21:1/5 to Luuk on Sat Dec 28 02:22:08 2019
On Saturday, December 28, 2019 at 10:52:33 AM UTC+2, Luuk wrote:
On 26-12-2019 23:18, Azathoth Hastur wrote:
On Sunday, December 22, 2019 at 5:43:09 AM UTC-5, robamm...@gmail.com wrote:
pühapäev, 29. september 2019 18:41.47 UTC+3 kirjutas azathot...@gmail.com:
Anyone doing php psotgresql for this?

The java framework craziness I see at work must have better alternatives! >>
I have used postgresql and php. And doing some statistical calculations in php. For example I have tried to calculate Pi. I used the so called "bombing"(method with random numbers). But the result came out so that the first 4 numbers of the number
were only correct. 3.141 . I ran the code about 50 times, and there were no differences in the about 3-4 first numbers. Maybe the "bombing" method is not very good for this. I got the idea of it from schools programming lesson. I'm sure other
mathematical methods would be better, but it was intresting for me to program and test it.

Basically I think, if you can program, then there is no difference in which language you program.

With the best wishes,
Kristjan Robam

Why not use the postgresql to calculate and sort, and php to just get in put and output?

Ok, its not postgresql, i tried to do 'calculate pi' in MSSQL 2017:

WITH pi (x, f, a, kwartpi) as
(SELECT 1,1.0,1,
CONVERT(float(53), 0.0)
UNION ALL
SELECT x+2,-f,x,
CONVERT(float(53),kwartpi+(f/x)) from pi where x<20000 and x>=a
)

SELECT top 10
x, 4*kwartpi
FROM pi
order by x desc
OPTION(MAXRECURSION 10000)

Output:
x
----------- ----------------------
20001 3,14149265341197
19999 3,14169266341197
19997 3,14149263340797
19995 3,14169268341997
19993 3,14149261339597
19991 3,14169270343597
19989 3,14149259337597
19987 3,14169272345997
19985 3,14149257334797
19983 3,14169274349197

(10 rows affected)

Completion time: 2019-12-28T09:49:58.2239611+01:00

*Will postgresql be better in doing this job?*

Follow-up set to comp.databases.postgresql
--
Luuk

What is the idea of this ?
I used a circle inside a square and Pi=(Circle area)*4/(Square area)
And by the way the First numbers of pi area 3.14159.

With the best wishes,
Kristjan Robam

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From robamman2019@gmail.com@21:1/5 to Luuk on Sat Dec 28 04:55:02 2019
On Saturday, December 28, 2019 at 1:32:27 PM UTC+2, Luuk wrote:
On 28-12-2019 11:22, robamman2019@gmail.com wrote:
On Saturday, December 28, 2019 at 10:52:33 AM UTC+2, Luuk wrote:
On 26-12-2019 23:18, Azathoth Hastur wrote:
On Sunday, December 22, 2019 at 5:43:09 AM UTC-5, robamm...@gmail.com wrote:
pühapäev, 29. september 2019 18:41.47 UTC+3 kirjutas azathot....@gmail.com:
Anyone doing php psotgresql for this?

The java framework craziness I see at work must have better alternatives!

I have used postgresql and php. And doing some statistical calculations in php. For example I have tried to calculate Pi. I used the so called "bombing"(method with random numbers). But the result came out so that the first 4 numbers of the number
were only correct. 3.141 . I ran the code about 50 times, and there were no differences in the about 3-4 first numbers. Maybe the "bombing" method is not very good for this. I got the idea of it from schools programming lesson. I'm sure other
mathematical methods would be better, but it was intresting for me to program and test it.

Basically I think, if you can program, then there is no difference in which language you program.

With the best wishes,
Kristjan Robam

Why not use the postgresql to calculate and sort, and php to just get in put and output?

Ok, its not postgresql, i tried to do 'calculate pi' in MSSQL 2017:

WITH pi (x, f, a, kwartpi) as
(SELECT 1,1.0,1,
CONVERT(float(53), 0.0)
UNION ALL
SELECT x+2,-f,x,
CONVERT(float(53),kwartpi+(f/x)) from pi where x<20000 and x>=a
)

SELECT top 10
x, 4*kwartpi
FROM pi
order by x desc
OPTION(MAXRECURSION 10000)

Output:
x
----------- ----------------------
20001 3,14149265341197
19999 3,14169266341197
19997 3,14149263340797
19995 3,14169268341997
19993 3,14149261339597
19991 3,14169270343597
19989 3,14149259337597
19987 3,14169272345997
19985 3,14149257334797
19983 3,14169274349197

(10 rows affected)

Completion time: 2019-12-28T09:49:58.2239611+01:00

*Will postgresql be better in doing this job?*

Follow-up set to comp.databases.postgresql
--
Luuk

What is the idea of this ?
I used a circle inside a square and Pi=(Circle area)*4/(Square area)
And by the way the First numbers of pi area 3.14159.

With the best wishes,
Kristjan Robam

Ah, this method
Ok. Proof.
d = 2r
Pi = C / 2r
Here we need a picture. Please look at : https://www.basic-mathematics.com/proof-of-the-area-of-a-circle.html
Area of a circle = (1/2)r*2Pi*r
Area of a Circle=r*r*Pi
Area of a Square=(2r)*(2r)=4r*r

r*r*Pi Points in Circle
------- =~ -----------------
4r*r Points in Square

So Pi =~ 4*(Points in Circle)/(Points in Square)
The bigger the Square, the more precise Pi we have.

it looks better than trying to calculate 4*arctan(1), which can be
written as the sum of 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 ......

But https://en.wikipedia.org/wiki/Squaring_the_circle pages says:
"In 1882, the task was proven to be impossible,"

--
Luuk

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From robamman2019@gmail.com@21:1/5 to Luuk on Sat Dec 28 08:30:19 2019
On Saturday, December 28, 2019 at 6:03:24 PM UTC+2, Luuk wrote:
On 28-12-2019 13:55, robamman2019@gmail.com wrote:
On Saturday, December 28, 2019 at 1:32:27 PM UTC+2, Luuk wrote:
On 28-12-2019 11:22, robamman2019@gmail.com wrote:
On Saturday, December 28, 2019 at 10:52:33 AM UTC+2, Luuk wrote:
On 26-12-2019 23:18, Azathoth Hastur wrote:
On Sunday, December 22, 2019 at 5:43:09 AM UTC-5, robamm...@gmail.com wrote:
pühapäev, 29. september 2019 18:41.47 UTC+3 kirjutas azathot....@gmail.com:
Anyone doing php psotgresql for this?

The java framework craziness I see at work must have better alternatives!

I have used postgresql and php. And doing some statistical calculations in php. For example I have tried to calculate Pi. I used the so called "bombing"(method with random numbers). But the result came out so that the first 4 numbers of the
number were only correct. 3.141 . I ran the code about 50 times, and there were no differences in the about 3-4 first numbers. Maybe the "bombing" method is not very good for this. I got the idea of it from schools programming lesson. I'm sure other
mathematical methods would be better, but it was intresting for me to program and test it.

Basically I think, if you can program, then there is no difference in which language you program.

With the best wishes,
Kristjan Robam

Why not use the postgresql to calculate and sort, and php to just get in put and output?

Ok, its not postgresql, i tried to do 'calculate pi' in MSSQL 2017:

WITH pi (x, f, a, kwartpi) as
(SELECT 1,1.0,1,
CONVERT(float(53), 0.0)
UNION ALL
SELECT x+2,-f,x,
CONVERT(float(53),kwartpi+(f/x)) from pi where x<20000 and x>=a >>>> )

SELECT top 10
x, 4*kwartpi
FROM pi
order by x desc
OPTION(MAXRECURSION 10000)

Output:
x
----------- ----------------------
20001 3,14149265341197
19999 3,14169266341197
19997 3,14149263340797
19995 3,14169268341997
19993 3,14149261339597
19991 3,14169270343597
19989 3,14149259337597
19987 3,14169272345997
19985 3,14149257334797
19983 3,14169274349197

(10 rows affected)

Completion time: 2019-12-28T09:49:58.2239611+01:00

*Will postgresql be better in doing this job?*

Follow-up set to comp.databases.postgresql
--
Luuk

What is the idea of this ?
I used a circle inside a square and Pi=(Circle area)*4/(Square area)
And by the way the First numbers of pi area 3.14159.

With the best wishes,
Kristjan Robam

Ah, this method
Ok. Proof.
d = 2r
Pi = C / 2r
Here we need a picture. Please look at : https://www.basic-mathematics.com/proof-of-the-area-of-a-circle.html
Area of a circle = (1/2)r*2Pi*r
Area of a Circle=r*r*Pi
Area of a Square=(2r)*(2r)=4r*r

r*r*Pi Points in Circle
------- =~ -----------------
4r*r Points in Square

So Pi =~ 4*(Points in Circle)/(Points in Square)
The bigger the Square, the more precise Pi we have.

it looks better than trying to calculate 4*arctan(1), which can be
written as the sum of 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 ......

But https://en.wikipedia.org/wiki/Squaring_the_circle pages says:
"In 1882, the task was proven to be impossible,"

--
Luuk

If i do more steps, my result should get more precise (but it is not
because of precision of used tool )

Doing it your way you have to do a lot of steps to get, close to, a
correct value.

--
Luuk

I think it would take about same time to calculate into the same precision, because the basic idea behind it all should be the Pythagoras theorem a*a+b*b=c*c. There is a Javascript Library BigNumber, with which it all could be resolved.

Here is a code, that uses it. It is only a test to use BigNumber, but it would need only few modifications, for it to calculate Pi in real, if somebody would like to use it.

-----------------------------------

Findingpi.html:

<html>
<meta charset="utf-8">
<title>Calculating pi</title>
<script src='bignumber/bignumber.js'></script>
<style>
#pivalue {
border: 1px solid green;
margin: 5px;
height: 40px;
width: 300px;
}
</style>
<body>

<form onsubmit="findnumber(); return false;">

<p>Pi(The Circle circumference divided with it's diameter)</p>
<p>Pi can be calculated with a circle inside a square. The Pi estimated value is the following: (Circle area)*4/(Square area).

<p>Please enter square width <input style="width: 100px; height: 40px; font-size: 30px;" type="text" id="squarew"></input></p>
<p><button type="submit" style="width: 200px; height: 40px; font-size: 30px;">Calculate pi</button></p>
<p id="pivalue"></p>

</h1>

</form>
<script>
var incircle=0;
var area=0;
}
function findnumber() {
incircle=0;
var squarew=parseInt(document.getElementById("squarew").value);
//document.getElementById("pivalue").innerHTML
area=squarew*squarew;
var xmax=new BigNumber(squarew);
var ymax=new BigNumber(squarew);
var x0=new BigNumber(squarew).dividedBy(new BigNumber(2));
var y0=new BigNumber(squarew).dividedBy(new BigNumber(2));
var x=new BigNumber(0);
var y=new BigNumber(0);
var increment=new BigNumber(1);
var run2=true;
while(true) {
//console.log("x: "+x.valueOf());
if(x.isEqualTo(xmax)) break;
while(run2) {
if(y.isEqualTo(ymax)) run2=false;
if(run2) {
y=y.plus(1);
//console.log(x.valueOf());
//console.log(y.valueOf());

} else {
y=new BigNumber(0);
}
}
run2=true;
x=x.plus(1);
}
var pivalue=incircle*4/area;
//alert("In circle: "+incircle+" Area: "+area+" Pi value: "+pivalue);
document.getElementById("pivalue").innerHTML=pivalue;
}
</script>
</body>
</html>

-----------------------------------
With the best wishes,
Kristjan Robam

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From robamman2019@gmail.com@21:1/5 to All on Sat Dec 28 09:40:37 2019
The BigNumber library is available at : https://github.com/MikeMcl/bignumber.js/ .
I modified this code a little bit.
For example with width 5000 square The Pi came out 3.1415....

findingpi.html:

<html>
<meta charset="utf-8">
<title>Calculating pi</title>
<script src='bignumber/bignumber.js'></script>
<style>
#pivalue {
border: 1px solid green;
margin: 5px;
height: 200px;
width: 500px;
overflow-x: auto;
overflow-wrap: break-word;
}
</style>
<body>

<form onsubmit="findnumber(); return false;">

<p>Pi(The Circle circumference divided with it's diameter)</p>
<p>Pi can be calculated with a circle inside a square. The Pi estimated value is the following: (Circle area)*4/(Square area).

<p>Please enter square width <input style="width: 200px; height: 40px; font-size: 30px;" type="text" id="squarew"></input></p>
<p><button type="submit" style="width: 200px; height: 40px; font-size: 30px;">Calculate pi</button></p>
<p id="pivalue"></p>

</h1>

</form>
<script>
var incircle=new BigNumber(0);
var area=new BigNumber(0);
var aaa1=xx0.minus(xx);
var aaa2=(yy0).minus(yy);
return isin;
}
function findnumber() {
incircle=new BigNumber(0);
area=new BigNumber(0);

var squarew=new BigNumber(document.getElementById("squarew").value);
//document.getElementById("pivalue").innerHTML
area=squarew.multipliedBy(squarew);
var xmax=new BigNumber(squarew);
var ymax=new BigNumber(squarew);
var x0=new BigNumber(squarew).dividedBy(2);
var y0=new BigNumber(squarew).dividedBy(2);
var x=new BigNumber(0);
var y=new BigNumber(0);
var run2=true;
while(true) {
if(x.isEqualTo(xmax)) break;
while(run2) {
if(y.isEqualTo(ymax)) run2=false;
if(run2) {
y=y.plus(new BigNumber(1));

} else {
y=new BigNumber(0);
}
incircle=incircle.plus(1);
}
}
run2=true;
x=x.plus(1);
}
var pivalue=incircle.multipliedBy(4).dividedBy(area);
//alert("In circle: "+incircle+" Area: "+area+" Pi value: "+pivalue);
document.getElementById("pivalue").innerHTML=pivalue;
}
</script>
</body>
</html>

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From robamman2019@gmail.com@21:1/5 to Luuk on Sun Dec 29 04:15:06 2019
On Sunday, December 29, 2019 at 11:15:01 AM UTC+2, Luuk wrote:
On 28-12-2019 18:40, robamman2019@gmail.com wrote:
The BigNumber library is available at : https://github.com/MikeMcl/bignumber.js/ .
I modified this code a little bit.
For example with width 5000 square The Pi came out 3.1415....

Ok, my script loads within 1 second, and does not need an input, and pi
came out with 15 decimals as: 3.141592653589793

<html>
<meta charset="utf-8">
<title>Calculating pi</title>
<script type="text/javascript">
function findnumber() {
var pivalue=Math.atan(1)*4;
document.getElementById("pi").innerHTML=pivalue;
}
</script>
<h1>Pi</h1>
<p id="pi"></p>
</body>
</html>

for now, i will keep it with this simple approach, and YES, i kno that Math.PI also exists, but than what calculation am i doing anywayz?.

--
Luuk

Yes, it's a little bit more difficult. You need super fast computer for it to calculate. But I think somewhere are these kind of computers already available. But we don't have access for them. Or you have to optimize code.
I tested it on 2600px width, it took 48 seconds. So to get precision 16 decimals it will take about a year on my computer.

I fixed a little bit of it's code.
index.html:

<html>
<meta charset="utf-8">
<title>Calculating pi</title>
<script src='bignumber/bignumber.js'></script>
<style>
#pivalue {
border: 1px solid green;
margin: 5px;
height: 200px;
width: 500px;
overflow-x: auto;
overflow-wrap: break-word;
}
</style>
<body>

<form onsubmit="findnumber(); return false;">

<p>Pi(The Circle circumference divided with it's diameter)</p>
<p>Pi can be calculated with a circle inside a square. The Pi estimated value is the following: (Circle area)*4/(Square area).

<p>Please enter square width <input style="width: 200px; height: 40px; font-size: 30px;" type="text" id="squarew"></input></p>
<p><button type="submit" style="width: 200px; height: 40px; font-size: 30px;">Calculate pi</button></p>
<p id="pivalue"></p>

</h1>

</form>
<script>
var incircle=new BigNumber(0);
var area=new BigNumber(0);
var aaa1=xx0.minus(xx);
var aaa2=(yy0).minus(yy);
return isin;
}
function findnumber() {
incircle=new BigNumber(0);
area=new BigNumber(0);

var squarew=new BigNumber(document.getElementById("squarew").value);
//document.getElementById("pivalue").innerHTML
area=squarew.multipliedBy(squarew);
var xmax=new BigNumber(squarew);
var ymax=new BigNumber(squarew);
var x0=new BigNumber(squarew).dividedBy(2);
var y0=new BigNumber(squarew).dividedBy(2);
var x=new BigNumber(0);
var y=new BigNumber(0);
var run2=true;
while(true) {
if(x.isEqualTo(xmax)) break;
while(run2) {
if(y.isEqualTo(ymax)) run2=false;
if(run2) {
y=y.plus(new BigNumber(1));

} else {
y=new BigNumber(0);
}
incircle=incircle.plus(1);
}
}
run2=true;
x=x.plus(1);
}
var pivalue=incircle.multipliedBy(4).dividedBy(area);
//alert("In circle: "+incircle+" Area: "+area+" Pi value: "+pivalue);
document.getElementById("pivalue").innerHTML=pivalue;
}
</script>
</body>
</html>

---------------
Kristjan Robam
E-mail: robamman2019x @ aol.com
(Please remove x, when writing to me)

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)