a. How many 4 digit numbers are there such that at least one digit is a sum of 2 or 3 of the others?
b. How many 5 digit numbers are there such that at least one digit is a sum of 2,3 or 4 of the others?
The following conditions apply both for a and for b:
*No leading zeros.
*The numbers consist of distinct digits.
DEFDBL A-Z
FOR n = 1000 TO 9999
ns$ = LTRIM$(STR$(n))
good = 1
FOR i = 1 TO LEN(ns$) - 1
IF INSTR(i + 1, ns$, MID$(ns$, i, 1)) THEN good = 0: EXIT FOR
NEXT
IF good THEN
good = 0
FOR one = 0 TO 1
t1 = one * VAL(MID$(ns$, 1, 1))
FOR two = 0 TO 1
t2 = t1 + two * VAL(MID$(ns$, 2, 1))
FOR three = 0 TO 1
t3 = t2 + three * VAL(MID$(ns$, 3, 1))
FOR four = 0 TO 1
t4 = t3 + four * VAL(MID$(ns$, 4, 1))
t = one + two + three + four
IF t > 1 THEN
IF VAL(MID$(ns$, 1, 1)) = t4 AND one = 0 THEN good = 1
IF VAL(MID$(ns$, 2, 1)) = t4 AND two = 0 THEN good = 1
IF VAL(MID$(ns$, 3, 1)) = t4 AND three = 0 THEN good = 1
IF VAL(MID$(ns$, 4, 1)) = t4 AND four = 0 THEN good = 1
END IF
NEXT
NEXT
NEXT
NEXT
IF good THEN
ct = ct + 1
END IF
END IF
NEXT
PRINT : PRINT ct
ct = 0
FOR n = 10000 TO 99999
ns$ = LTRIM$(STR$(n))
good = 1
FOR i = 1 TO LEN(ns$) - 1
IF INSTR(i + 1, ns$, MID$(ns$, i, 1)) THEN good = 0: EXIT FOR
NEXT
IF good THEN
good = 0
FOR one = 0 TO 1
t1 = one * VAL(MID$(ns$, 1, 1))
FOR two = 0 TO 1
t2 = t1 + two * VAL(MID$(ns$, 2, 1))
FOR three = 0 TO 1
t3 = t2 + three * VAL(MID$(ns$, 3, 1))
FOR four = 0 TO 1
t4 = t3 + four * VAL(MID$(ns$, 4, 1))
FOR five = 0 TO 1
t5 = t4 + five * VAL(MID$(ns$, 5, 1))
t = one + two + three + four + five
IF t > 1 THEN
IF VAL(MID$(ns$, 1, 1)) = t4 AND one = 0 THEN good = 1
IF VAL(MID$(ns$, 2, 1)) = t4 AND two = 0 THEN good = 1
IF VAL(MID$(ns$, 3, 1)) = t4 AND three = 0 THEN good = 1
IF VAL(MID$(ns$, 4, 1)) = t4 AND four = 0 THEN good = 1
IF VAL(MID$(ns$, 5, 1)) = t5 AND five = 0 THEN good = 1
END IF
NEXT
NEXT
NEXT
NEXT
NEXT
IF good THEN
ct = ct + 1
END IF
END IF
NEXT n
PRINT : PRINT ct
finds the respective answers as:
2400
and
17496
|
Posted by Charlie
on 2013-07-07 12:25:57 |