seven digit addition

[This article was first published on R – Xi'an's Og, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Another quick riddle from the riddler: solve the equation

EXMREEK + EHKREKK = ?K?H?X?E

which involves every digit between 0 and 9. While the puzzle can be unravelled by considering first E and K, which must be equal to 6 and 3, a simple R code also leads to the conclusion

isok <- function(a,b){
 s=as.numeric(unlist(strsplit(as.character(sum(10^(6:0)*a)+
   sum(10^(6:0)*b)),"")))
 if (length(s)==7){ goal=FALSE}else{
   goal=(length(unique(c(a,b,s)))==10)&(a[2]==s[6])&
         (s[8]==a[6])&(s[2]==a[7])&(b[2]==s[4])}
 return(goal)}

pasok <- function(T=1e3){ 
for (t in 1:T){ 
  a[1]=a[5]=a[6]=6;a[7]=3 
  digs=sample(c(0:2,4,5,7:9),4) 
  a[2:4]=digs[1:3] b[1]=a[1];b[2]=digs[4];
  b[3]=a[7];b[4]=a[4];b[5]=a[1];b[6:7]=a[7] 
  if (isok(a=a,b=b)) 
     print(rbind(a,b))}} 

> pasok()
  [,1] [,2] [,3] [,4] [,5] [,6] [,7]
a    6    2    4    7    6    6    3
b    6    8    3    7    6    3    3

which sum is 13085296.

To leave a comment for the author, please follow the link and comment on their blog: R – Xi'an's Og.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)