formulaIsRight <- function( x ){
left <- x[1]*(10*x[2]+x[3])+100*x[4]+10*x[5]+x[6]
right <- 1000*x[7]+100*x[8]+10*x[9]+x[10]
return( left == right )
}
# count <<- 0, permutation( 0:9, NULL ) で呼び出す
permutation <- function( s, a ){
if( length( s ) <= 0 ){
if( formulaIsRight( a ) ){
count <<- count + 1
}
}else{
for( i in s ){
Recall( s[ s != i ], c( a, i ) )
}
}
}
count <<- 0
permutation( 0:9, NULL )
print( count ) # 350
Zm9ybXVsYUlzUmlnaHQgPC0gZnVuY3Rpb24oIHggKXsKCWxlZnQgPC0geFsxXSooMTAqeFsyXSt4WzNdKSsxMDAqeFs0XSsxMCp4WzVdK3hbNl0KCXJpZ2h0IDwtIDEwMDAqeFs3XSsxMDAqeFs4XSsxMCp4WzldK3hbMTBdCglyZXR1cm4oIGxlZnQgPT0gcmlnaHQgKQp9CgojIGNvdW50IDw8LSAwLCBwZXJtdXRhdGlvbiggMDo5LCBOVUxMICkg44Gn5ZG844Gz5Ye644GZCnBlcm11dGF0aW9uIDwtIGZ1bmN0aW9uKCBzLCBhICl7CglpZiggbGVuZ3RoKCBzICkgPD0gMCApewoJCWlmKCBmb3JtdWxhSXNSaWdodCggYSApICl7CgkJCWNvdW50IDw8LSBjb3VudCArIDEKCQl9Cgl9ZWxzZXsKCQlmb3IoIGkgaW4gcyApewoJCQlSZWNhbGwoIHNbIHMgIT0gaSBdLCBjKCBhLCBpICkgKQoJCX0KCX0KfQoKY291bnQgPDwtIDAKcGVybXV0YXRpb24oIDA6OSwgTlVMTCApCnByaW50KCBjb3VudCApICMgMzUw