Re: Can this be made cleaner and more efficient?
- To: mathgroup at smc.vnet.net
- Subject: [mg29066] Re: [mg29022] Can this be made cleaner and more efficient?
- From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
- Date: Sat, 26 May 2001 21:54:02 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
You can certainly write simpler code, e.g: RandomOdd3[d_] := Module[{res = Random[Integer, {10^(d - 1), 10^d}], primes = Table[Prime[n], {n, 25}]}, While[Or @@ Thread[Mod[res, primes] == 0], res = Random[Integer, {10^(d - 1), 10^d}]]; res] or: RandomOdd4[d_] := Module[{res = Random[Integer, {10^(d - 1), 10^d}], primes = Table[Prime[n], {n, 25}]}, If[And @@ Thread[Mod[res, primes] != 0], res, RandomOdd4[d]]] However, none of these will be more efficient than yours because they use the same algorithm. (In fact they should be somewhat less efficient). However, if instead of the first 24 primes you wanted to sieve out the first 10000 you might find it rather tedious to write out a version of your code .... -- Andrzej Kozlowski Toyama International University JAPAN http://platon.c.u-tokyo.ac.jp/andrzej/ http://sigma.tuins.ac.jp/~andrzej/ on 01.5.25 2:47 PM, Flip at nospam at newsranger.com wrote: > Hi All, > > I was wondering if the following module (it is small) can be made simpler and > faster ... and thoughts would be appreciated. > > RandomOdd2[d_] := Module[{res}, > res = Random[Integer, {10^(d - 1), 10^d}]; > While[( > > Mod[res, 02] == 0 || Mod[res, 03] == 0 || Mod[res, 05] == 0 || > Mod[res, 07] == 0 || Mod[res, 11] == 0 || Mod[res, 13] == 0 || > Mod[res, 17] == 0 || Mod[res, 19] == 0 || Mod[res, 23] == 0 || > Mod[res, 29] == 0 || Mod[res, 31] == 0 || Mod[res, 37] == 0 || > Mod[res, 41] == 0 || Mod[res, 43] == 0 || Mod[res, 47] == 0 || > Mod[res, 53] == 0 || Mod[res, 59] == 0 || Mod[res, 61] == 0 || > Mod[res, 71] == 0 || Mod[res, 73] == 0 || Mod[res, 79] == 0 || > Mod[res, 83] == 0 || Mod[res, 89] == 0 || Mod[res, 97] == 0), > res = Random[Integer, {10^(d - 1), 10^d}]]; > res] > > It is basically sieving out the first 24 primes (primes <= 100) from the > result. > > Thank you ... Flip > > >