Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pascal's Triangle and nth roots.
#3
(09-25-2022, 08:28 PM)Jack Wrote: not bad Smile

@Jack

I did find one error. oldx$ needed to be zeroed. Apparently, when a digit in the answer is zero, oldx$ did not get redefined, as the loop exited before that could happen. I zeroed it in the DO:LOOP, and now it works as expected. It took some testing to see that error produced. I noticed it on the cube root of 19.

QUESTION: By any chance do you know in situations where the digits will not terminate before the digit limit is reached, or simply will not terminate like pi, is there anyway the remainder can be made into a fraction, as in regular long division?

Here is a brief example in numeric form with printed inf to the screen. The "Target" is the remainder and "d" is the growing answer per digit. Note: Ignore decimal point. It's buggy. Left over from a square root prog and needs further tweaking.

Code: (Select All)
$CONSOLE:ONLY
DIM AS DOUBLE d, i, j, r, t, x, oldx
LINE INPUT "Whole number: "; n$
LINE INPUT "Root: "; r$
r = VAL(r$)
'''n$ = LTRIM$(STR$(n%))
hh&& = LEN(n$)
h&& = (r - (r - LEN(n$) MOD r)) + 1
t = VAL(MID$(n$, 1, h&& - 1))
limit&& = 8
' Calculate Pascal's Triangle.
REDIM p$(r + 1)
FOR i1&& = 1 TO r + 1
    p&& = 1
    FOR j1&& = 1 TO i1&&
        p$(j1&&) = LTRIM$(STR$(p&&))
        p&& = p&& * (i1&& - j1&&) \ j1&&
    NEXT
NEXT

DO
    oldx = 0
    lcnt&& = lcnt&& + 1
    PRINT "Target ="; t
    FOR j = 1 TO 10
        x = 0
        FOR i = 1 TO r
            p&& = VAL(p$(i))
            IF i = 1 THEN
                PRINT i; p&&; j; d; " (10 ^"; (i - 1); "*"; p&&; "* d ^"; i - 1; " * j ^"; (r + 1 - i); ") + "
                x = x + 10 ^ (i - 1) * p&& * d ^ (i - 1) * j ^ (r + 1 - i)
            ELSE
                PRINT i; p&&; j; d; " (10 ^"; (i - 1); "*"; p&&; "* d ^"; i - 1; " * j ^"; (r + 1 - i); ") + "
                x = x + 10 ^ (i - 1) * p&& * d ^ (i - 1) * j ^ (r + 1 - i)
            END IF
        NEXT
        PRINT "<<<"; x;" Press a key to cont...": SLEEP
        IF x > t THEN EXIT FOR
        oldx = x: COLOR 6: PRINT "oldx ="; oldx, t; oldx >= t: COLOR 7
    NEXT
    d = VAL(LTRIM$(STR$(d)) + LTRIM$(STR$(j - 1)))
    PRINT "t - oldx as: "; t; "-"; oldx; "="; t - oldx,: COLOR 14: PRINT "d ="; d: COLOR 7
    tmp1$ = LTRIM$(STR$(t - oldx))
    tmp2$ = MID$(n$, h&&, r) + STRING$(r - LEN(MID$(n$, h&&, r)), "0")
    t = VAL(tmp1$ + tmp2$) ' This will remove any leading zeros carried down.
    h&& = h&& + r
    IF t = 0 AND h&& >= LEN(n$) OR lcnt&& = limit&& THEN EXIT DO
    t$ = LTRIM$(STR$(t))
    IF dpx&& = 0 THEN ' Decimal point relocator. Limited to && size unless converted to string.
        IF h&& >= hh&& THEN
            dpx&& = INT(hh&& / 2 + .5)
            IF dpx&& = 0 THEN dpx&& = -1 ' Do not set to zero as -1 accomplishes the same thing and prevents ongoing loops here.
        END IF
    END IF
LOOP
d$ = LTRIM$(STR$(d))
IF dpx&& THEN
    sm_rt$ = MID$(d$, 0, dpx&& + 1) + "." + MID$(d$, dpx&& + 1)
ELSE
    sm_rt$ = d$
END IF
PRINT
PRINT "Answer: "; sm_rt$
PRINT
RUN

Thanks for having a look.

Pete
Reply


Messages In This Thread
Pascal's Triangle and nth roots. - by Pete - 09-25-2022, 06:26 PM
RE: Pascal's Triangle and nth roots. - by Jack - 09-25-2022, 08:28 PM
RE: Pascal's Triangle and nth roots. - by Pete - 09-26-2022, 04:15 PM



Users browsing this thread: 3 Guest(s)