Archive for intToDigits()

Le Monde puzzle [#1111]

Posted in R, Statistics with tags , , , , , , on September 18, 2019 by xi'an

Another low-key arithmetic problem as Le Monde current mathematical puzzle:

Notice that there are 10 numbers less than, and prime with 11, 100 less than and prime with 101, 1000 less than, and prime with 1111? What is the smallest integer N such that the cardinal of the set of M<N prime with N is 10⁴? What is the smallest multiple of 1111 using only different digits? And what is the largest?

As it is indeed a case for brute force resolution as in the following R code

library(numbers)
homanycoprim=function(n){
  many=1
  for (i in 2:(n-1)) many=many+coprime(i,n)
  return(many)}

smallest=function(){
  n=1e4
  many=homanycoprim(n)
  while (many!=1e4) many=homanycoprim(n<-n+1)
  return(n)}

which returns n=10291 as the smallest value of N.  For the other two questions, the usual integer2digit function is necessary

smalldiff=function(){
  n=1111;mul=1
  while (mul<1e6) {
    x=as.numeric(strsplit(as.character(n*mul), "")[[1]])
    while (sum(duplicated(x))!=0){
     mul=mul+1
     x=as.numeric(strsplit(as.character(n*mul), "")[[1]])}
  print(n*mul);mul=mul+1}}

leading to 241,087 as the smallest and 9,875,612,340 as the largest (with 10 digits).

Le Monde puzzle [#1110]

Posted in Books, Kids, R, Travel with tags , , , , , , , , , on September 16, 2019 by xi'an

A low-key sorting problem as Le Monde current mathematical puzzle:

If the numbers from 1 to 67 are randomly permuted and if the sorting algorithm consists in picking a number i with a position higher than its rank i and moving it at the correct i-th position, what is the maximal number of steps to sort this set of integers when the selected integer is chosen optimaly?

As the question does not clearly state what happens to the number j that stood in the i-th position, I made the assumption that the entire sequence from position i to position n is moved one position upwards (rather than having i and j exchanged). In which case my intuition was that moving the smallest moveable number was optimal, resulting in the following R code

sorite<-function(permu){ n=length(permu) p=0 while(max(abs(permu-(1:n)))>0){
    j=min(permu[permu<(1:n)])
    p=p+1
    permu=unique(c(permu[(1:n)<j],j,permu[j:n]))} 
  return(p)}

which takes at most n-1 steps to reorder the sequence. I however checked this insertion sort was indeed the case through a recursive function

resorite<-function(permu){ n=length(permu);p=0 while(max(abs(permu-(1:n)))>0){
    j=cand=permu[permu<(1:n)]
    if (length(cand)==1){
      p=p+1
      permu=unique(c(permu[(1:n)<j],j,permu[j:n]))
    }else{
      sol=n^3
      for (i in cand){
        qermu=unique(c(permu[(1:n)<i],i,permu[i:n]))
        rol=resorite(qermu)
        if (rol<sol)sol=rol}
      p=p+1+sol;break()}} 
  return(p)}

which did confirm my intuition.

Le Monde puzzle [#1109]

Posted in Kids, R with tags , , , , , on September 4, 2019 by xi'an

A digital problem as Le Monde current mathematical puzzle:

Noble numbers are such that they only involve different digits and are multiple of all their digits. What is the largest noble number?

Hmmmm…. Brute force? Since the maximal number of digits is 10, one may as well try:

k=soz=9
for (t in 1:1e3){
sol=1
while (sol<10^(k-1)){ 
 u=sample(0:8,k);i=digit2int(u) 
 if (max(i%%u[u>0])==0) soz=max(soz,sol<-i)}}

which returns 9875643120… (I made the conscious choice to exclude zero from the dividers. Which was not a choice spelled out in the original question.)

Le Monde puzzle [#1075]

Posted in Books, Kids, R with tags , , , , on December 12, 2018 by xi'an

A new Le Monde mathematical puzzle in the digit category:

Find the largest number such that each of its internal digits is strictly less than the average of its two neighbours. Same question when all digits differ.

For instance, n=96433469 is such a number. When trying pure brute force (with the usual integer2digits function!)

le=solz=3
while (length(solz)>0){
 solz=NULL
 for (i in (10^(le+1)-1):(9*10^le+9)){
  x=as.numeric(strsplit(as.character(i), "")[[1]])
 if (min(x[-c(1,le+1)]<(x[-c(1,2)]+x[-c(le,le+1)])/2)==1){ print(i);solz=c(solz,i); break()}}
 le=le+1}

this is actually the largest number returned by the R code. There is no solution with 9 digits. Adding an extra condition

le=solz=3
while (length(solz)>0){
 solz=NULL
 for (i in (10^(le+1)-1):(9*10^le+9)){
  x=as.numeric(strsplit(as.character(i), "")[[1]])
 if ((min(x[-c(1,le+1)]<(x[-c(1,2)]+x[-c(le,le+1)])/2)==1)&
    (length(unique(x))==le+1)){ print(i);solz=c(solz,i); break()}}
 le=le+1}

produces n=9520148 (seven digits) as the largest possible integer.

Le Monde puzzle [#854]

Posted in Books, Kids, Statistics with tags , , on February 21, 2014 by xi'an

A Le Monde mathematical puzzle that sounds similar to earlier ones:

Find all integers x between 1000 and 9999 and N≥1 such Nx has the reverse sequence of digits compared with i.  

For N=1, the appropriate integers x are such that the four digits are symmetrical, as in x=3553. For N≥1, I ran the following R code:

for (i in 10^3:(5*10^3)){
 dig=rev(intToDigits(i))
 for (N in 2:8){
  if (N*i>9999) break()
  if (max(abs(intToDigits(N*i)-dig))<.1)
      print(c(i,N,N*i))
      }}

and only found the single entry

[1] 2178    4 8712

where the intToDigits function was suggested to me by Pierre Pudlo:

intToDigits <- function(x) {
 if (length(x) != 1) {
   warning( "x should be of length 1. Using only x[1]")
   x <- x[1]
   }
 m <- floor(log10(x)) + 1
 pow10 <- 10^(1:m)
 xpow <- x * pow10 / (10^m)
 xrep <- trunc(xpow) / pow10
 digits <- c(xrep[1], xrep[-1]-xrep[-m]) * pow10
 digits
 }