One approach could be like below:
num = 1729
sum_of_digits <- sum(as.numeric(unlist(strsplit(as.character(num), split = ""))))
rev_of_sum_of_digits <- as.numeric(paste(rev(strsplit(as.character(sum_of_digits),"")[[1]]),collapse=""))
ifelse(rev_of_sum_of_digits * sum_of_digits == num, "Magic Number!", "Not a Magic Number!")
Hope this helps!
5
solved Magic number in R [closed]