' MOD Operator Patterns — even/odd, wrap-around, circular buffer
#COMPILE EXE
#DIM ALL

SUB EvenOdd()
    LOCAL i AS LONG
    PRINT "=== EVEN/ODD ==="
    FOR i = 1 TO 20
        IF i MOD 2 = 0 THEN
            PRINT STR$(i) & " is even"
        ELSE
            PRINT STR$(i) & " is odd"
        END IF
    NEXT i
    PRINT
END SUB

SUB Divisibility()
    LOCAL i AS LONG
    PRINT "=== DIVISIBILITY ==="
    PRINT "Numbers divisible by 3 or 5 from 1 to 30:"
    FOR i = 1 TO 30
        IF i MOD 3 = 0 AND i MOD 5 = 0 THEN
            PRINT STR$(i) & " (by 3 AND 5)"
        ELSEIF i MOD 3 = 0 THEN
            PRINT STR$(i) & " (by 3)"
        ELSEIF i MOD 5 = 0 THEN
            PRINT STR$(i) & " (by 5)"
        END IF
    NEXT i
    PRINT
END SUB

SUB CircularBuffer()
    LOCAL bufferSize AS LONG
    LOCAL i AS LONG
    LOCAL writePos AS LONG
    LOCAL readPos AS LONG
    LOCAL value AS LONG
    bufferSize = 8
    writePos = 0
    readPos = 0
    PRINT "=== CIRCULAR BUFFER (size=" & STR$(bufferSize) & ") ==="
    PRINT "Writing values 10-25 to buffer..."
    FOR i = 10 TO 25
        value = i
        writePos = i MOD bufferSize
        PRINT "Write pos " & STR$(writePos) & " = " & STR$(value)
    NEXT i
    PRINT
    PRINT "Reading back from buffer:"
    FOR i = 10 TO 17
        readPos = i MOD bufferSize
        PRINT "Read pos " & STR$(readPos)
    NEXT i
    PRINT
END SUB

SUB WrapAround()
    LOCAL i AS LONG
    LOCAL n AS LONG
    PRINT "=== WRAP-AROUND INDEXING ==="
    PRINT "7-segment display cycling (0-6):"
    FOR i = 0 TO 20
        n = i MOD 7
        PRINT "Step " & STR$(i) & " -> segment " & STR$(n)
    NEXT i
    PRINT
END SUB

SUB ModClock()
    LOCAL i AS LONG
    LOCAL hour AS LONG
    PRINT "=== MOD CLOCK (12-hour) ==="
    FOR i = 0 TO 23
        hour = i MOD 12
        IF hour = 0 THEN hour = 12
        PRINT FORMAT$(i, "##") & ":00 = " & STR$(hour) & ":00 (period " & STR$(INT(i / 12) + 1) & ")"
    NEXT i
    PRINT
END SUB

SUB LeapYearCheck()
    LOCAL year AS LONG
    PRINT "=== LEAP YEAR CHECK ==="
    FOR year = 2000 TO 2028
        IF (year MOD 400 = 0) OR (year MOD 4 = 0 AND year MOD 100 <> 0) THEN
            PRINT STR$(year) & " is a leap year"
        ELSE
            IF year MOD 4 = 0 THEN PRINT STR$(year) & " is NOT a leap year (century rule)"
        END IF
    NEXT year
    PRINT
END SUB

SUB ModDistribution()
    LOCAL i AS LONG
    LOCAL counts(0 TO 9) AS LONG
    PRINT "=== MOD DISTRIBUTION ==="
    PRINT "Distribution of i^2 MOD 10 for i = 1 to 100:"
    FOR i = 1 TO 100
        counts((i * i) MOD 10) = counts((i * i) MOD 10) + 1
    NEXT i
    FOR i = 0 TO 9
        PRINT "  " & STR$(i) & ": " & STR$(counts(i)) & " occurrences"
    NEXT i
    PRINT
END SUB

FUNCTION PBMAIN() AS LONG
    PRINT "============================"
    PRINT " MOD Operator & Remainder"
    PRINT "============================"
    PRINT
    CALL EvenOdd()
    CALL Divisibility()
    CALL CircularBuffer()
    CALL WrapAround()
    CALL ModClock()
    CALL LeapYearCheck()
    CALL ModDistribution()
    PRINT "MOD demos complete."
    FUNCTION = 0
END FUNCTION
