' DBLYEAR.BAS
' 27 Jan 1998 by Marc Kummel aka Treebeard.
' Contact mkummel@rain.org, http://www.rain.org/~mkummel/
'
' A quickie to find longest run of years with repeated digits, eg 1998 has
' two "9"s. This was the DMS Newsnote Stumper for 23 Jan 1998.
' This code uses LONG integers with range -2,147,483,648 to 2,147,483,647.
' This is not quite enough to cover all possible 10 digit years, but it's
' enough to find the max. PDS has a Currency data type that would work.
' Of course 11 digit years MUST repeat a digit.
'
' This code is freeware. For more interesting BASIC software with source
' code, check out Treebeard's Basic Vault on the Web at
' http://www.rain.org/~mkummel/basic/
'--------------------------------------------------------------------------
CONST false = 0, true = NOT false
CONST asc0 = 48
esc$ = CHR$(27)
DIM dig%(0 TO 9)
'prompt for max year to test
CLS
INPUT "Max year to test (9999): ", n$
IF LEN(n$) = 0 THEN n$ = "9999"
IF VAL(n$) < 1 THEN PRINT "too small": END
IF VAL(n$) > 2147483647 THEN n$ = "2147483647"
maxdigit& = VAL(n$)
doubledigit& = 0
runflag% = false
longrun& = 0
thisrun& = 0
PRINT
PRINT "Working on years 1.."; LTRIM$(STR$(maxdigit&))
PRINT "Press to abort."
PRINT
'each year to max
FOR n& = 1 TO maxdigit&
ERASE dig%
n$ = LTRIM$(STR$(n&))
'test each digit
FOR i% = 1 TO LEN(n$)
'assume it's NOT a double
flag% = true
digit% = ASC(MID$(n$, i%, 1)) - asc0
'found a repeated digit
IF dig%(digit%) THEN
flag% = false
doubledigit& = doubledigit& + 1
'we're in a run, so bump count
IF runflag% THEN
thisrun& = thisrun& + 1
'it's a new run, so remember the year
ELSE
thisrunstart& = n&
thisrun& = 1
runflag% = true
END IF
'bail
EXIT FOR
'unique so far
ELSE
dig%(digit%) = true
END IF
NEXT i%
'no double digit, but was this end of longest run yet?
IF flag% AND runflag% THEN
IF thisrun& > longrun& THEN
longrun& = thisrun&
longrunstart& = thisrunstart&
PRINT " run of"; longrun&; " double-digit years from"; longrunstart&; "to"; longrunstart& + longrun& - 1
END IF
runflag% = false
END IF
'press to abort
IF INKEY$ = esc$ THEN
maxdigit& = n&
EXIT FOR
END IF
NEXT n&
'show results
PRINT
PRINT "In the years 1.."; LTRIM$(STR$(maxdigit&)); ","
PRINT " There are"; doubledigit&; "years with repeated digits"
PRINT " Double-digit years:"; CINT(100! * doubledigit& / maxdigit&); "%"
PRINT " Unique-digit years:"; CINT(100! * (maxdigit& - doubledigit&) / maxdigit&); "%"
PRINT
END