Place one of the letters C, L, X, V or I into each of the 25 cells of a 5x5 grid so that each row and each column forms a 5-letter roman number under 300 (using the modern standard subtractive notation in which IV = 4, IX = 9, XL = 40, XLIX = 49, XC = 90, etc.). No roman number is used more than once, so there are ten different roman numbers. The rows are in descending order of value top-to-bottom and the columns are also in descending order left-to-right.
What is the sum of the values of the ten roman numbers?
I used the following code to test all possible 5x5 squares and found 4 solutions.
DECLARE SUB RomanNumerals (num$)
DECLARE SUB RomanReplace (num$, Frm$, r$)
DECLARE FUNCTION Romans$ (n%)
CLS 0
OPEN ".\rmnsqrs.txt" FOR OUTPUT AS #1
DIM rmn5(1 TO 66) AS STRING
DIM rmn5v%(1 TO 66)
cnt% = 0
FOR i% = 1 TO 300
rmn$ = Romans$(i%)
IF LEN(rmn$) = 5 THEN
cnt% = cnt% + 1
rmn5(cnt%) = rmn$
rmn5v%(cnt%) = i%
END IF
NEXT i%
DIM grid(1 TO 5) AS STRING
DIM cols%(1 TO 5)
cprct = 0
mx = 8936928
found = 0
cnt = 0
FOR i5% = 1 TO 62
FOR i4% = i5% + 1 TO 63
FOR i3% = i4% + 1 TO 64
FOR i2% = i3% + 1 TO 65
FOR i1% = i2% + 1 TO 66
grid(1) = rmn5(i1%)
grid(2) = rmn5(i2%)
grid(3) = rmn5(i3%)
grid(4) = rmn5(i4%)
grid(5) = rmn5(i5%)
r1% = rmn5v%(i1%)
r2% = rmn5v%(i2%)
r3% = rmn5v%(i3%)
r4% = rmn5v%(i4%)
r5% = rmn5v%(i5%)
fail% = 0
FOR i% = 1 TO 5
cl$ = ""
FOR j% = 1 TO 5
cl$ = cl$ + MID$(grid(j%), i%, 1)
NEXT j%
idx% = 1
fd% = 0
WHILE idx% <= 66 AND fd% = 0
IF cl$ = rmn5(idx%) THEN
fd% = 1
cols%(i%) = rmn5v%(idx%)
END IF
idx% = idx% + 1
WEND
IF fd% = 0 THEN fail% = 1
NEXT i%
IF fail% = 0 THEN
FOR i% = 1 TO 4
c% = cols%(i%)
IF c% <= cols%(i% + 1) OR c% = r1% OR c% = r2% OR c% = r3% OR c% = r4% OR c% = r5% THEN fail% = 1
NEXT i%
IF cols%(5) = r1% OR cols%(5) = r2% OR cols%(5) = r3% OR cols%(5) = r4% OR cols%(5) = r5% THEN fail% = 1
IF fail% = 0 THEN
found = found + 1
PRINT #1, "Solution #" + STR$(found) + ":"
FOR i% = 1 TO 5
PRINT #1, grid(i%)
NEXT i%
PRINT #1, "Row Values top to bottom: "; r1%; r2%; r3%; r4%; r5%
PRINT #1, "Column Values left to right: ";
tot% = r1% + r2% + r3% + r4% + r5%
FOR i% = 1 TO 5
PRINT #1, cols%(i%);
tot% = tot% + cols%(i%)
NEXT i%
PRINT #1, ""
PRINT #1, "Total: "; tot%
END IF
END IF
cnt = cnt + 1
prct = INT(100 * cnt / mx)
IF prct > cprct THEN
cprct = prct
PRINT "Search is " + STR$(cprct) + "% complete. " + STR$(found) + " solutions found"
END IF
NEXT i1%
NEXT i2%
NEXT i3%
NEXT i4%
NEXT i5%
CLOSE #1
SUB RomanNumerals (num$)
x% = VAL(num$): num$ = "": IF x% < 1 THEN EXIT SUB
DIM Ones(10) AS STRING
Ones(0) = "": Ones(1) = "I": Ones(2) = "II"
Ones(3) = "III": Ones(4) = "IV": Ones(5) = "V"
Ones(6) = "VI": Ones(7) = "VII": Ones(8) = "VIII": Ones(9) = "IX"
Tens$ = "XLC": Hund$ = "CDM"
num$ = Ones(x% MOD 10)
n$ = Ones((x% \ 10) MOD 10): RomanReplace num$, n$, Tens$
n$ = Ones((x% \ 100) MOD 10): RomanReplace num$, n$, Hund$
num$ = STRING$(x% \ 1000, "M") + num$
END SUB
SUB RomanReplace (num$, Frm$, r$)
FOR t% = 1 TO LEN(Frm$)
MID$(Frm$, t%, 1) = MID$(r$, INSTR("IVX", MID$(Frm$, t%, 1)))
NEXT: num$ = Frm$ + num$
END SUB
FUNCTION Romans$ (n%)
t$ = STR$(n%): RomanNumerals t$: Romans$ = t$
END FUNCTION
and the 4 solutions are
Solution # 1:
CCXXX
CLXXX
LXXXI
XXVII
XVIII
Row Values top to bottom: 230 180 81 27 18
Column Values left to right: 270 175 36 32 23
Total: 1072
Solution # 2:
CCLXX
CLXXX
XXXVI
XXXII
XVIII
Row Values top to bottom: 270 180 36 32 18
Column Values left to right: 230 175 81 27 23
Total: 1072
Solution # 3:
CCXXX
CLXXV
LXXXI
XXVII
XXIII
Row Values top to bottom: 230 175 81 27 23
Column Values left to right: 270 180 36 32 18
Total: 1072
Solution # 4:
CCLXX
CLXXV
XXXVI
XXXII
XXIII
Row Values top to bottom: 270 175 36 32 23
Column Values left to right: 230 180 81 27 18
Total: 1072
and 1,4 and 2,3 are reflections of each other so really there are 2 unique solutions.
Edited on May 17, 2009, 5:00 pm
|
Posted by Daniel
on 2009-05-17 16:57:44 |