08-18-2022, 01:34 AM
@Jack
And here we go... floating points now supported...
Also goof-proofed negs.
Pete
And here we go... floating points now supported...
Code: (Select All)
WIDTH 160, 42
_SCREENMOVE 0, 0
DO
betatest% = -1
loops# = 29
INPUT "Number: "; x#
IF x# = 0 THEN SYSTEM
IF x# < 0 THEN PRINT "Negatives not allowed. Redo..": _DELAY 3: RUN
oldy$ = ""
x$ = LTRIM$(STR$(x#))
IF INSTR(x$, ".") THEN
decx$ = MID$(x$, 1, INSTR(x$, ".") - 1)
x$ = MID$(x$, 1, INSTR(x$, ".") - 1) + MID$(x$, INSTR(x$, ".") + 1)
ELSE
decx$ = x$
END IF
j# = LEN(decx$)
a$ = RIGHT$(LTRIM$(STR$(j#)), 1)
IF VAL(a$) / 2 = VAL(a$) \ 2 THEN
i# = 1 ' Even number length.
ELSE
i# = 0 ' Odd number length.
END IF
DO
a$ = (MID$(x$, i#, 2))
z$ = LTRIM$(STR$(VAL(z$) - k#)) + a$
oldy$ = ""
FOR j# = 1 TO 10
IF i# > 1 THEN y$ = LTRIM$(STR$(VAL(sqrt$) * 2)) + LTRIM$(STR$(j#)) ELSE y$ = LTRIM$(STR$(j#))
REM COLOR 8, 0: PRINT "y$ = "; y$, VAL(y$) * j#: COLOR 7, 0
IF VAL(y$) * j# >= VAL(z$) THEN
IF VAL(y$) * j# = VAL(z$) THEN
h% = 0: oldy$ = y$ ' Perfect square division.
ELSE
h% = 1
END IF
IF VAL(z$) = 0 AND VAL(oldy$) * (j# - h%) = 0 AND i# >= LEN(decx$) THEN EXIT DO
IF dp# = 0 THEN IF i# >= LEN(decx$) THEN dp# = INT(LEN(decx$) / 2 + .5)
IF betatest% THEN PRINT "Sqrt "; sqrt$; " * 2 = ";: COLOR 2, 0: PRINT LTRIM$(STR$(VAL(sqrt$) * 2));: COLOR 7, 0: PRINT LTRIM$(STR$(j# - h%)); " * "; LTRIM$(STR$(j# - h%)); " ="; VAL(oldy$) * (j# - h%)
sqrt$ = sqrt$ + LTRIM$(STR$(j# - h%))
k# = VAL(oldy$) * (j# - h%)
REM PRINT "oldy$ = "; oldy$; " h% ="; h%; "j#-h% ="; j# - h%, "k# ="; k#, "sqrt = "; sqrt$
IF betatest% THEN PRINT "Remainder "; z$; " minus"; k#; "= ";
EXIT FOR
END IF
oldy$ = y$
NEXT
IF betatest% THEN PRINT LTRIM$(STR$(VAL(z$) - k#)); " sqrt = "; sqrt$
i# = i# + 2
IF i# > loops# THEN EXIT DO
x$ = x$ + "00"
LOOP
PRINT
IF dp# THEN
sqrt$ = MID$(sqrt$, 0, dp# + 1) + "." + MID$(sqrt$, dp# + 1)
END IF
PRINT "QB64 SQR:"; SQR(x#)
PRINT "Pete SQR: "; sqrt$: _DELAY 1: CLEAR
PRINT
LOOP
Also goof-proofed negs.
Pete