[Fwd: Selecting numbers with all odd digits]

*To*: mathgroup at smc.vnet.net*Subject*: [mg22274] [Fwd: [mg22170] Selecting numbers with all odd digits]*From*: Hartmut Wolf <hwolf at debis.com>*Date*: Mon, 21 Feb 2000 20:00:42 -0500 (EST)*Organization*: debis Systemhaus*Sender*: owner-wri-mathgroup at wolfram.com

Hartmut Wolf schrieb: Tom De Vries schrieb: > > Hello all, > > In one of the math classes I am teaching we are doing some work with > permutations and combinations. One question in the text was, "how many > positive integers less than 700 have all odd digits?" > > I had done the question "by hand" and then wondered how to use Mathematica > to demonstrate that the solution was the correct one. > > I am still very far down on the learning curve in most things, but I managed > to bludgeon my way to an answer (thankfully the same as mine!) > > Select[ > > Range[699], > > Union[ OddQ [ IntegerDigits[#]]] == {True} & > > ] > > I am sure my trial and error method can be improved upon. I am not talking > speed as much as simply using Mathematica in a way that makes sense and > would be more logical?? > Hello Tom, as for the programming, your In[1]:= Select[Range[699], Union[OddQ[IntegerDigits[#]]] == {True} &] // Length Out[1]= 105 is equivalent to In[2]:= Count[Range[699], _?(And @@ OddQ[IntegerDigits[#]] &)] Out[2]= 105 So this counts all Integers in the specified range for that all digits are odd -- literal Translation. But you also could calculate simply In[3]:= 5^^300 + 5^^100 + 5^^10 Out[3]= 105 or if you prefer In[4]:= 3 5 5 + 5 5 + 5 Out[4]= 105 The example is somewhat very special, perhaps tricking. Better try to solve the general case "by hand". After the experience with that you might end up with that algorithm: caodd3[n_Integer?Positive] := Block[{aodd = True, f = #1*5 + If[aodd, If[OddQ[#2], (#2 + 1)/2, aodd = False; #2/2], 5] & }, Fold[f, 0, IntegerDigits[n]]] In[9]:= caodd3[699] Out[9]= 105 To give you a hint how I discovered this, I took the arbitrary number 13461: (1) caodd[13461] == caodd[13399] (2) 1_3_3_9_9 1 5 5 5 5 + 2 5 5 5 + 2 5 5 + 5 5 + 5 The new algorithm has quite a different efficiency. Compare with caodd2[n_Integer?Positive] := Count[Range[n], _?(And @@ OddQ[IntegerDigits[#]] &)] In[13]:= caodd2[315225] // Timing Out[13]= {101.816 Second, 7305} In[14]:= caodd3[315225] // Timing Out[14]= {0.01 Second, 7305} Kind regards, Hartmut