#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL a_digits(100) AS LONG
    LOCAL b_digits(100) AS LONG
    LOCAL r_digits(110) AS LONG
    LOCAL a_len AS LONG
    LOCAL b_len AS LONG
    LOCAL r_len AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL carry AS LONG
    LOCAL digit AS LONG
    LOCAL temp_str AS STRING

    ' Big integer addition and subtraction with digit arrays
    ' Each digit stores 0-9, least significant at index 0

    ' Reset arrays
    i = 0
    WHILE i < 100
        a_digits(i) = 0
        b_digits(i) = 0
        i = i + 1
    WEND
    i = 0
    WHILE i < 110
        r_digits(i) = 0
        i = i + 1
    WEND

    ' Number: 12345
    a_len = 5
    a_digits(0) = 5
    a_digits(1) = 4
    a_digits(2) = 3
    a_digits(3) = 2
    a_digits(4) = 1

    ' Number: 6789
    b_len = 4
    b_digits(0) = 9
    b_digits(1) = 8
    b_digits(2) = 7
    b_digits(3) = 6

    ' Addition
    carry = 0
    i = 0
    WHILE i < a_len OR i < b_len
        digit = carry
        IF i < a_len THEN digit = digit + a_digits(i)
        IF i < b_len THEN digit = digit + b_digits(i)
        IF digit >= 10 THEN
            carry = 1
            digit = digit - 10
        ELSE
            carry = 0
        END IF
        r_digits(i) = digit
        i = i + 1
    WEND
    IF carry = 1 THEN
        r_digits(i) = 1
        r_len = i + 1
    ELSE
        r_len = i
    END IF

    temp_str = ""
    i = r_len - 1
    WHILE i >= 0
        temp_str = temp_str + CHR$(48 + r_digits(i))
        i = i - 1
    WEND
    PRINT "12345 + 6789 = " + temp_str

    ' Subtraction
    i = 0
    WHILE i < 110
        r_digits(i) = 0
        i = i + 1
    WEND

    ' 12345 - 6789
    carry = 0
    i = 0
    WHILE i < a_len
        digit = a_digits(i) - carry
        IF i < b_len THEN digit = digit - b_digits(i)
        IF digit < 0 THEN
            carry = 1
            digit = digit + 10
        ELSE
            carry = 0
        END IF
        r_digits(i) = digit
        i = i + 1
    WEND
    r_len = a_len
    WHILE r_len > 1 AND r_digits(r_len - 1) = 0
        r_len = r_len - 1
    WEND

    temp_str = ""
    i = r_len - 1
    WHILE i >= 0
        temp_str = temp_str + CHR$(48 + r_digits(i))
        i = i - 1
    WEND
    PRINT "12345 - 6789 = " + temp_str

    ' 999 + 1 = 1000 (carry test)
    i = 0
    WHILE i < 100
        a_digits(i) = 0
        b_digits(i) = 0
        i = i + 1
    WEND
    a_len = 3
    a_digits(0) = 9
    a_digits(1) = 9
    a_digits(2) = 9
    b_len = 1
    b_digits(0) = 1

    i = 0
    WHILE i < 110
        r_digits(i) = 0
        i = i + 1
    WEND

    carry = 0
    i = 0
    WHILE i < a_len OR i < b_len
        digit = carry
        IF i < a_len THEN digit = digit + a_digits(i)
        IF i < b_len THEN digit = digit + b_digits(i)
        IF digit >= 10 THEN
            carry = 1
            digit = digit - 10
        ELSE
            carry = 0
        END IF
        r_digits(i) = digit
        i = i + 1
    WEND
    IF carry = 1 THEN
        r_digits(i) = 1
        r_len = i + 1
    ELSE
        r_len = i
    END IF

    temp_str = ""
    i = r_len - 1
    WHILE i >= 0
        temp_str = temp_str + CHR$(48 + r_digits(i))
        i = i - 1
    WEND
    PRINT "999 + 1 = " + temp_str

    ' 1000 - 1 = 999 (borrow test)
    i = 0
    WHILE i < 100
        a_digits(i) = 0
        b_digits(i) = 0
        i = i + 1
    WEND
    a_len = 4
    a_digits(0) = 0
    a_digits(1) = 0
    a_digits(2) = 0
    a_digits(3) = 1
    b_len = 1
    b_digits(0) = 1

    i = 0
    WHILE i < 110
        r_digits(i) = 0
        i = i + 1
    WEND

    carry = 0
    i = 0
    WHILE i < a_len
        digit = a_digits(i) - carry
        IF i < b_len THEN digit = digit - b_digits(i)
        IF digit < 0 THEN
            carry = 1
            digit = digit + 10
        ELSE
            carry = 0
        END IF
        r_digits(i) = digit
        i = i + 1
    WEND
    r_len = a_len
    WHILE r_len > 1 AND r_digits(r_len - 1) = 0
        r_len = r_len - 1
    WEND

    temp_str = ""
    i = r_len - 1
    WHILE i >= 0
        temp_str = temp_str + CHR$(48 + r_digits(i))
        i = i - 1
    WEND
    PRINT "1000 - 1 = " + temp_str

    ' 999999 + 1 = 1000000 (multi-digit carry cascade)
    i = 0
    WHILE i < 100
        a_digits(i) = 0
        b_digits(i) = 0
        i = i + 1
    WEND
    a_len = 6
    a_digits(0) = 9
    a_digits(1) = 9
    a_digits(2) = 9
    a_digits(3) = 9
    a_digits(4) = 9
    a_digits(5) = 9
    b_len = 1
    b_digits(0) = 1

    i = 0
    WHILE i < 110
        r_digits(i) = 0
        i = i + 1
    WEND

    carry = 0
    i = 0
    WHILE i < a_len OR i < b_len
        digit = carry
        IF i < a_len THEN digit = digit + a_digits(i)
        IF i < b_len THEN digit = digit + b_digits(i)
        IF digit >= 10 THEN
            carry = 1
            digit = digit - 10
        ELSE
            carry = 0
        END IF
        r_digits(i) = digit
        i = i + 1
    WEND
    IF carry = 1 THEN
        r_digits(i) = 1
        r_len = i + 1
    ELSE
        r_len = i
    END IF

    temp_str = ""
    i = r_len - 1
    WHILE i >= 0
        temp_str = temp_str + CHR$(48 + r_digits(i))
        i = i - 1
    WEND
    PRINT "999999 + 1 = " + temp_str

    ' 50-digit number addition
    i = 0
    WHILE i < 100
        a_digits(i) = 0
        b_digits(i) = 0
        i = i + 1
    WEND
    a_len = 50
    i = 0
    WHILE i < 50
        a_digits(i) = 1
        i = i + 1
    WEND
    b_len = 50
    i = 0
    WHILE i < 50
        b_digits(i) = 2
        i = i + 1
    WEND

    i = 0
    WHILE i < 110
        r_digits(i) = 0
        i = i + 1
    WEND

    carry = 0
    i = 0
    WHILE i < a_len OR i < b_len
        digit = carry
        IF i < a_len THEN digit = digit + a_digits(i)
        IF i < b_len THEN digit = digit + b_digits(i)
        IF digit >= 10 THEN
            carry = 1
            digit = digit - 10
        ELSE
            carry = 0
        END IF
        r_digits(i) = digit
        i = i + 1
    WEND
    r_len = i

    temp_str = ""
    i = r_len - 1
    WHILE i >= 0
        temp_str = temp_str + CHR$(48 + r_digits(i))
        i = i - 1
    WEND
    PRINT "50-digit number + 50-digit number:"
    PRINT "Result length: " + STR$(r_len) + " digits"

    PRINT ""
    PRINT "Big integer add/sub demo complete."

    FUNCTION = 0
END FUNCTION
