seven digit addition

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.

2 Responses to “seven digit addition”

  1. […] 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 on topics such as: Data […]

  2. […] article was first published on R – Xi'an's Og, and kindly contributed to […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.