Results 1 to 4 of 4

Thread: A QBasic Program that calculates . . .

  1. #1
    tWebber 37818's Avatar
    Join Date
    Jan 2014
    Location
    So. California
    Faith
    Nontraditional Christian
    Gender
    Male
    Posts
    5,185
    Amen (Given)
    815
    Amen (Received)
    443

    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
    . . . the Gospel of Christ, for it is [the] power of God to salvation to every [one] believing, . . . -- Romans 1:16.

    . . . that Christ died for our sins according to the scriptures; And that he was buried, and that he rose again the third day according to the scriptures: . . . -- 1 Corinthians 15:3, 4.

    Whosoever believeth that Jesus is the Christ is born of God: . . . -- 1 John 5:1.

  2. #2
    See, the Thing is... Cow Poke's Avatar
    Join Date
    Jan 2014
    Location
    The Republic of Texas
    Faith
    Christian
    Gender
    Male
    Posts
    48,888
    Amen (Given)
    10483
    Amen (Received)
    23104
    a range of prime numbers?
    --- this space intentionally left blank ---

  3. #3
    tWebber Leonhard's Avatar
    Join Date
    Jan 2014
    Location
    Denmark - Jutland
    Faith
    Catholic
    Gender
    Male
    Posts
    5,088
    Amen (Given)
    916
    Amen (Received)
    2741
    Quote Originally Posted by 37818 View Post
    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.

  4. Amen 37818 amen'd this post.
  5. #4
    tWebber Leonhard's Avatar
    Join Date
    Jan 2014
    Location
    Denmark - Jutland
    Faith
    Catholic
    Gender
    Male
    Posts
    5,088
    Amen (Given)
    916
    Amen (Received)
    2741
    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;
    }

  6. Amen 37818 amen'd this post.

Posting Permissions

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