-
-
Save krishnanraman/f0a599f611d77e8366608cd199fa4842 to your computer and use it in GitHub Desktop.
24.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n=10 | |
op = c('+', '-', '*', '/') | |
perms = c() | |
while(length(perms) < 24) { | |
s = paste(sample(op,4), collapse='') | |
if (length(perms[perms==s]) == 0) { | |
perms =c(perms,s) | |
print(length(perms)) | |
} | |
} | |
is.wholenumber <- | |
function(x, tol = .Machine$double.eps^0.5) abs(x - round(x)) < tol | |
mat=matrix(NA,nrow=100000,ncol=6) | |
do_r = function(r,j,e) { | |
if( r==-1) return(-1) | |
if (e=='+') r=r+j | |
else if (e=='-') r=r-j | |
else if (e=='*') r=r*j | |
else r=r/j | |
if (!is.wholenumber(r) | (r < 0)) | |
return(-1) | |
else | |
return(r) | |
} | |
do_ops <- function(i,j,k,l,m,ops) { | |
if (anyDuplicated(c(i,j,k,l,m)) > 0) return (-1) | |
a = substr(ops,1,1) | |
b = substr(ops,2,2) | |
c = substr(ops,3,3) | |
d = substr(ops,4,4) | |
r = do_r(i,j,a) | |
r = do_r(r,k,b) | |
r = do_r(r,l,c) | |
r = do_r(r,m,d) | |
return(r) | |
} | |
seq = 1:n | |
count = 1 | |
for (i in seq) | |
for (j in seq) | |
for(k in seq) | |
for(l in seq) | |
for(m in seq) { | |
for(ops in perms) { | |
r = do_ops(i,j,k,l,m,ops) | |
if (r == 24) { | |
expr = paste(i,substr(ops,1,1),j,substr(ops,2,2),k,substr(ops,3,3),l,substr(ops,4,4),m,collapse='') | |
mat[count,] = c(expr,i,j,k,l,m) | |
print(mat[count,]) | |
count = count + 1 | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Find all the ways to make 24 using 5 different integers & 4 different arithmetic operators. | |
eg. 2 + 10 / 1 - 4 * 3 = 24 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
paste(mat[1:2799,1], sep=",", collapse=",") | |
[1] "1 + 5 / 2 * 9 - 3,1 + 5 / 2 * 10 - 6,1 + 5 * 9 / 2 - 3,1 + 5 * 9 - 6 / 2,1 + 5 * 10 / 2 - 6,1 + 6 * 8 / 2 - 4,1 + 7 * 10 - 8 / 3,1 + 8 / 3 * 10 - 6,1 + 8 * 6 / 2 - 3,1 + 8 * 10 / 3 - 6,1 + 9 - 4 / 2 * 8,1 + 9 - 4 * 8 / 2,1 + 10 - 2 / 3 * 8,1 + 10 - 2 * 8 / 3,1 + 10 - 3 / 2 * 6,1 + 10 - 3 * 6 / 2,1 + 10 - 5 / 2 * 8,1 + 10 * 5 - 7 / 2,1 + 10 - 5 * 8 / 2,1 + 10 * 6 / 2 - 9,1 + 10 * 7 - 5 / 3,1 + 10 * 9 - 3 / 4,2 / 1 + 4 - 3 * 8,2 / 1 + 4 * 5 - 6,2 / 1 + 5 - 3 * 6,2 / 1 + 5 - 4 * 8,2 / 1 + 6 * 4 - 8,2 / 1 + 6 - 5 * 8,2 / 1 + 7 - 3 * 4,2 / 1 + 7 - 5 * 6,2 / 1 + 7 - 6 * 8,2 - 1 + 7 * 9 / 3,2 / 1 + 8 * 3 - 6,2 / 1 * 9 - 4 + 10,2 / 1 + 9 - 5 * 4,2 / 1 + 9 - 7 * 6,2 / 1 * 9 + 10 - 4,2 / 1 * 10 - 3 + 7,2 / 1 + 10 - 4 * 3,2 / 1 * 10 - 4 + 8,2 / 1 * 10 - 5 + 9,2 / 1 + 10 - 6 * 4,2 / 1 * 10 + 7 - 3,2 / 1 * 10 + 8 - 4,2 / 1 + 10 - 8 * 6,2 / 1 * 10 + 9 - 5,2 / 1 + 10 - 9 * 8,2 + 4 / 1 - 3 * 8,2 + 4 / 1 * 5 - 6,2 + 4 - 3 / 1 * 8,2 + 4 - 3 * 8 / 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