Re: Selecting numbers with all odd digits
- To: mathgroup at smc.vnet.net
- Subject: [mg22212] Re: [mg22170] Selecting numbers with all odd digits
- From: Daniel Lichtblau <danl at wolfram.com>
- Date: Fri, 18 Feb 2000 02:34:48 -0500 (EST)
- References: <200002170623.BAA04359@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Tom De Vries wrote: > > 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?? > > Thanks for any suggestions might have! > > Tom De Vries > Edmonton, Alberta > Canada Some small improvements: Use And rather than Union and only bother with numbers that are odd to begin with. Select[Range[1,699,2], (And @@ OddQ[IntegerDigits[#]])&] An alternative approach is to generate all the values explicitly from odd digits. Since we want to allow numbers of fewer than 3 digits (in effect allowing zero padding on the left) we need to iterate over all digit lengths. Also we'd want to remove numbers afterward that are three digits but larger than 700. We can generate the numbers using FromDigits and Outer, as below. There are no doubt other nice(r?) ways to do this. allOddDigits[n_] := Module[ {odigits={1,3,5,7,9}, j}, Flatten[ Table[Map[FromDigits, Flatten[Outer[List, Apply[Sequence,Table[odigits,{j}]]], j-1]], {j,1,n}] ] ] Select[allOddDigits[3], #<700&] Daniel Lichtblau Wolfram Research
- References:
- Selecting numbers with all odd digits
- From: "Tom De Vries" <tdevries@shop.westworld.ca>
- Selecting numbers with all odd digits