Announcement

Collapse
No announcement yet.

problem 64

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

  • problem 64

    Again a problem from Project Euler.net
    The loop in the main function does not work and I don not know why.
    If you enter single values everything is ok.
    #COMPILE EXE
    #DIM ALL

    FUNCTION PBMAIN () AS LONG
    DIM str AS STRING
    DIM j AS LONG,s AS LONG, s1 AS LONG
    s = 0
    FOR j = 2 TO 10000
    str = sqrtcfrac(j)
    s1 = add(str)
    IF s1 MOD 2 > 0 THEN
    s+=1
    END IF
    NEXT j
    PRINT s


    WAITKEY$

    END FUNCTION
    FUNCTION sqrtcfrac(n AS LONG) AS STRING
    DIM a AS LONG, w AS LONG, w2 AS LONG, q AS LONG, q0 AS LONG, q1 AS LONG, m AS LONG, m1 AS LONG
    DIM st AS STRING
    st = ""
    w = INT(SQR(n))
    q = n - w*w
    IF q = 0 THEN
    sqrtcfrac = STR$(w)+st
    END IF
    w2 = 2*w
    a = w
    m = w
    q0 = 1
    st = STR$(a)+" " + st
    WHILE NOT a = w2
    a = (m+w) \ q
    st = st + STR$(a)+","
    m1 = a*q - m
    q1 = q0 + a*(m - m1)
    m = m1
    q0 = q
    q = q1
    WEND
    sqrtcfrac = st
    END FUNCTION

    FUNCTION add(st AS STRING) AS LONG
    DIM i AS LONG, c AS LONG
    c = 0
    FOR i = 1 TO LEN(st)
    IF MID$(st,i,1) = "," THEN
    c+=1
    END IF
    NEXT i
    add = c
    END FUNCTION

    regardxs
    Volker

  • #2
    Please use code tags when posting code.

    http://www.powerbasic.com/support/pb....php?do=bbcode
    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Volker,
      running your program as posted gives a "divide by zero" error because the value if q becomes zero inside the While/Wend loop so the line a = (m+w) \ q cannot be calculated.

      Paul.

      Comment


      • #4
        Volker,
        Add #DEBUG DISPLAY ON before #COMPILE EXE and the compiler will tell you what kind of error you have
        Old QB45 Programmer

        Comment


        • #5
          re: Problem 64

          Paul,
          thank you for your hint. At first I did not believe it, because I have a clause to handle y = 0. But that is not enough in Powerbasic. Before "end if" you have to insert "exit function". Now everything works well
          Volker

          Comment

          Working...
          X