C51, in the SSLE exercises of the Linear Algebra book Exercise C51: “Find all of the six-digit numbers in which the first digit is one less than the second, the third digit is half the second, the fourth digit is three times the third and the last two digits form a number that equals the sum of the fourth and fifth. The sum of all the digits is 24. (From The MENSA Puzzle Calendar for January 9, 2006.)”
Conditions:
Solution attempt #1:
conditions <- function(number) {
digits <- as.numeric(strsplit(as.character(number), "")[[1]])
if (length(digits) != 6) { return (FALSE) }
if (digits[1] + digits[2] + digits[3] + digits[4] + digits[5] + digits[6] != 24) { return(FALSE)}
if (digits[1] != digits[2] - 1) { return(FALSE)}
if (digits[2] != 2 * digits[3]) { return(FALSE)}
if (digits[4] != 3 * digits[3]) {return(FALSE)}
if (as.numeric(paste(digits[5], digits[6], sep = "")) != digits[4] + digits[5]) {return(FALSE)}
return(TRUE)
}
solution <- numeric()
for (number in 100000:999999) {
if (conditions(number)) { solution <- c(solution, number) }
}
solution
## [1] 563910
Solution attempt #2
first_digit <- 1:9
second_digit <- 2:9
third_digit <- 1:9
fourth_digit <- 3:9
fifth_digit <- 0:9
sixth_digit <- 0:9
combinations <- expand.grid(first_digit, second_digit, third_digit, fourth_digit, fifth_digit, sixth_digit)
names(combinations) <- c("first", "second", "third", "fourth", "fifth", "sixth")
filtered_combinations <- combinations[
combinations$first == combinations$second - 1 &
combinations$third == combinations$second / 2 &
combinations$fourth == 3 * combinations$third &
as.numeric(paste0(combinations$fifth, combinations$sixth)) == combinations$fourth + combinations$fifth &
combinations$first + combinations$second + combinations$third + combinations$fourth + combinations$fifth + combinations$sixth == 24,
]
solution <- apply(filtered_combinations, 1, function(x) paste0(x, collapse = ""))
solution <- as.numeric(solution)
solution
## [1] 563910
Solution attempt 3:
Trying linear equations
Trying to work on these equations:
I’ll try to substitute each of the digits (x1-x4) with their value using x2:
Then:
(x2-1) + x2 + (x2/2) + (3*(x2/2)) + x5 + x6 = 24
5.5 * x2 + x5 + x6 = 19 (first equation to work with)
And we have:
x5 * 10 + x6 = (3 * (x2/2)) + x5
x5 * 11 + x6 = 3 * x2 + 6
x6 = 3 * x2 + 6 - 11 * x5
To substitute that into the other (first) equation:
5.5 * x2 + x5 + 3 * x2 + 6 - 11 * x5 = 19
8.5 * x2 - 10 * x5 = 13 (the final equation after taking all conditions into account)
solution <- numeric()
for (x2 in 2:9) {
for (x5 in 0:9) {
if (8.5 * x2 - 10 * x5 == 13) {
x1 <- x2 - 1
x3 <- x2 / 2
x4 <- 3 * x3
x6 <- 24 - (x1 + x2 + x3 + x4 + x5)
if (x6 >= 0 && x6 <= 9) {
solution <- as.numeric(paste0(x1, x2, x3, x4, x5, x6))
solutions <- c(solutions, solution)
}
}
}
}
solution
## numeric(0)
Strange that this did not give the solution I got in other ways. Trying a slightly different way (relying less on linear equations): Solution attempt #4
solutions <- numeric()
for (x2 in 1:9) {
for (x5 in 0:9) {
x1 <- x2 - 1
x3 <- x2 / 2
x4 <- 3 * x3
x6 <- x4 + x5 - 10 * x5
if (x6 == floor(x6) && x6 >= 0 && x6 <= 9) {
if (x1 + x2 + x3 + x4 + x5 + x6 == 24) {
solution <- as.numeric(paste0(x1, x2, x3, x4, x5, x6))
solutions <- c(solutions, solution)
}
}
}
}
solutions
## [1] 563910