Announcement

Collapse

Computer Lab Guidelines

Here in the computer lab, we talk about cool tech, the newest coolest gadgets, and tackle your toughest tech questions.

If you need to refresh yourself on the decorum, now would be a good time. Forum Rules: here
See more
See less

A QBasic Program that calculates . . .

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    a range of prime numbers?
    "Neighbor, how long has it been since you’ve had a big, thick, steaming bowl of Wolf Brand Chili?”

    Comment


    • #3
      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.

      Comment


      • #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;
        }

        Comment

        Related Threads

        Collapse

        Topics Statistics Last Post
        Started by Ronson, 10-21-2020, 10:34 AM
        10 responses
        70 views
        0 likes
        Last Post rogue06
        by rogue06
         
        Started by Christianbookworm, 07-27-2020, 04:14 PM
        125 responses
        4,580 views
        0 likes
        Last Post mossrose  
        Working...
        X