(x=scan())%in%(2*4^(n=0:x)-2^n-1)
[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.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
One challenge on code golf is to find the shortest possible code to identify whether or not an integer belongs to the binary cyclops numbers which binary expansion is 0, 101, 11011, 1110111, 111101111, &tc. The n-th such number being
this leads to the above solution in R (26 bits). The same length as the C solution [which I do not get]
f(n){n=~n==(n^=-~n)*~n/2;}
And with shorter versions in many esoteric languages I had never heard of, like the 8 bits Brachylog code
ḃDDḍ×ᵐ≠
or the 7 bits Jelly
B¬ŒḂ⁼SƊ
As a side remark, since this was not the purpose of the game, the R code is most inefficient in creating a set of size (x+1), with most terms being Inf.
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.