# Thread: A QBasic Program that calculates . . .

1. ## A QBasic Program that calculates . . .

Just for fun, see if you can guess what this program calculates:

Code:
```A# = 1#
B# = .5#
C# = SQR(2#) / 2#
D# = .25#
LblA:
E# = A#
B# = 2# * B#
A# = (A# + C#) / 2#
C# = SQR(E# * C#)
D# = D# - B# * (A# - E#) * (A# - E#)
oldx# = x#
x# = (A# + C#) * (A# + C#) / (4# * D#)
PRINT x#
IF oldx# <> x# THEN GOTO LblA
SYSTEM```  Reply With Quote

2. a range of prime numbers?  Reply With Quote

3. Originally Posted by 37818 Just for fun, see if you can guess what this program calculates:

Code:
```A# = 1#
B# = .5#
C# = SQR(2#) / 2#
D# = .25#
LblA:
E# = A#
B# = 2# * B#
A# = (A# + C#) / 2#
C# = SQR(E# * C#)
D# = D# - B# * (A# - E#) * (A# - E#)
oldx# = x#
x# = (A# + C#) * (A# + C#) / (4# * D#)
PRINT x#
IF oldx# <> x# THEN GOTO LblA
SYSTEM```
Its honestly been a while since I've last looked at QBASIC code. I'm not sure why you're willing to torture yourself with it, but let me get a QBASIC interpreter and see what this code actually does, before trying to guess what it does.  Reply With Quote

4. Ah wait, this is recognisable now. Its the Gauss-Legendre's quadratic algorithm for calculating pi.

You should still initisalise x even though grabbing something random off the memory stack probably won't make oldx match x.

And its prettier in C.

Code:
```int main()
{
double a = 1.0, b = 0.5, c = sqrt(2.0)/2, d = 1.0/4, e, x = 0.0, oldx;

do {
e = a;
b = 2*b;
a = (a+c)/2;
c = sqrt(e*c);
d = d - b * (a - e) * (a - e);
oldx = x;
x = (a + c) * (a + c) / (4 * d);
fprintf(stdout,"%f\n",x);
}
while (x != oldx)

return 0;
}```  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•