Le Monde puzzle [#939]
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
A Le Monde mathematical puzzle about special integers:
Find all integers with less than 11 digits that are perfect squares and can be written as a(a+6), a being an integer.
Eleven digits being too much for a brute force exploration of the form `for (t in 1:1e11)`…, some preliminary analysis is needed, but I could not figure out a reason why there is no solution apart from 2… (I checked up to 1e8!)
Since I had “guessed” the above puzzle from the solution published one week later (!), I checked the quality of my guesswork with my friend Jean-Louis Fouley, who gave me the genuine question, based on a different interpretation of a(a+6):
Find all integers with less than 11 digits that are perfect squares and can be written as x concatenated with (x+6), x being an integer.
This is more open to brute-force R exploration (with some help from stack overflow) since x only has five digits at most!
perfect=function(b){ x=FALSE a=trunc(sqrt(b)) for (i in a:(a+1)) if (i^2==b) x=TRUE return(x)} for (x in 1:(1e6-1)) if (perfect( as.numeric(paste(c(as.numeric(strsplit(as.character(x), "")[[1]]), as.numeric(strsplit(as.character(x+6), "")[[1]])),collapse="")))) print(x)
Which returns
[1] 15 [1] 38
and then crashes for x=99994, because
strsplit(as.character(1e+05), "")
does not return the six digits of 1e+05 but
[[1]] [1] "1" "e" "+" "0" "5"
instead. Except for this value of x, no other solution is found using this R code. And for x=99994, y=99994100000 is not a perfect square.
Filed under: Books, Kids, R Tagged: arithmetics, as.character(), as.numeric(), Le Monde, mathematical puzzle, R, strsplit()
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.