Dichotomy - pinkman - 05-15-2020 10:18 PM
Just for fun, if you sometimes use dichotomy, a small but useful implementation.
Start the program, enter you start values, then press + or /. Follow the result is on the screen. When you’re done, press Apps, Enter or Esc.
Code:
EXPORT DICHO_UP, DICHO_DOWN;
EXPORT DICHO()
BEGIN
LOCAL BUP, BDWN, K, I, GUESS;
K := 1;
I := INPUT({DICHO_UP, DICHO_DOWN},"Dichotomy - Start values",{"Up value ", "Down value"});
IF I = 0 THEN
RETURN 0;
END;
BUP := DICHO_UP;
BDWN := DICHO_DOWN;
IF BUP < BDWN THEN
BDWN := BDWN - BUP;
BUP := BUP + BDWN;
BDWN := BUP - BDWN;
END;
I := 0;
WHILE K ≠ 30 AND K > 0 AND K ≠ 4 DO // Keys Enter, Apps or Esc
RECT();
I := I+1;
GUESS := (BUP-BDWN)/2 + BDWN;
TEXTOUT_P("Press + or - , Enter when done.", 5, 205, 2, RGB(220,80,40));
TEXTOUT_P("Guess " + STRING(I), 120, 100, 2, RGB(10,200,80));
TEXTOUT_P(STRING(GUESS), 120, 120, 7);
TEXTOUT_P(STRING(BDWN), 5, 225, 1, RGB(80,80,80));
TEXTOUT_P(STRING(BUP), 280, 225, 1, RGB(80,80,80));
K := WAIT();
IF K == 45 THEN // minus
BUP := GUESS;
ELSE
IF K == 50 THEN // plus
BDWN := GUESS;
ELSE
I := I-1;
END;
END;
END;
RETURN GUESS;
END;
|