%I #53 Jan 25 2022 07:46:15
%S 1,1152,131081,131110,131111,11011112,111151110,111151111,1111511111,
%T 111711111181,1111416111111,11151111111171,1131141411111111,
%U 10111111111115011,117111183111111111,1011411111118111111,11111111181111141121,111112811111101111111
%N Smallest number with exactly n 1's in its digits both in decimal and binary representation.
%H Scott R. Shannon, <a href="/A350691/b350691.txt">Table of n, a(n) for n = 1..45</a> (terms 1..25 from Michael S. Branicky, terms 26..35 from Chai Wah Wu)
%e a(2) = 1152 which in binary is 10010000000. Both representations contain exactly 2 1's. And there is no smaller number satisfying this constraint.
%t Join[{1},Table[t=0;While[!IntegerQ[k=Min@Flatten[Select[FromDigits/@Select[Permutations[#],First@#!=0&],Count[IntegerDigits[#,2],1]==n&]&/@(Join[Table[1,n],#]&/@Tuples[Join[{0},Range[2,9]],++t])]]];k,{n,2,12}]] (* _Giorgos Kalogeropoulos_, Jan 13 2022 *)
%o (Python)
%o numbers = [0,2,3,4,5,6,7,8,9]
%o terms = []
%o for ones in range(1,13):
%o if ones == 3: # needs more than 2 digits differing from "1"
%o terms.append(131081)
%o else:
%o x = 1
%o min_solution = 999999999999999999
%o for i in range(ones+1):
%o x += 10 ** (i + 1)
%o for i in range(ones+2):
%o for j in numbers:
%o candidatej = x + (j - 1) * 10**i
%o for k in range(i+1,ones+2):
%o for l in numbers:
%o candidate = candidatej + (l - 1) * 10 ** k
%o if bin(candidate)[2:].count('1') == ones:
%o if candidate < min_solution:
%o min_solution = candidate
%o terms.append(min_solution)
%o (Python)
%o from itertools import count
%o def A350691_helper(n,m): # generator in order of numbers with n decimal digits and m 1's. Leading zeros are allowed.
%o if n >= m:
%o if n == 1:
%o if m == 1:
%o yield 1
%o else:
%o yield 0
%o yield from range(2,10)
%o elif n == m:
%o yield (10**m-1)//9
%o else:
%o for b in A350691_helper(n-1,m):
%o yield b
%o r = 10**(n-1)
%o for b in A350691_helper(n-1,m-1):
%o yield r+b
%o for a in range(2,10):
%o k = a*r
%o for b in A350691_helper(n-1,m):
%o yield k+b
%o def A350691(n):
%o for l in count(n):
%o r = 10**(l-1)
%o for a in range(1,10):
%o n2 = n-1 if a == 1 else n
%o k = a*r
%o for s in A350691_helper(l-1,n2):
%o m = k+s
%o if bin(m)[2:].count('1') == n:
%o return m # _Chai Wah Wu_, Jan 13 2022
%Y Cf. A000120, A268643, A350692.
%K nonn,base
%O 1,2
%A _Ruediger Jehn_, Jan 12 2022
%E a(13)-a(18) from _Michael S. Branicky_, Jan 12 2022
|