Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Can this be made cleaner and more efficient?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg29070] Re: [mg29022] Can this be made cleaner and more efficient?
  • From: BobHanlon at aol.com
  • Date: Sat, 26 May 2001 21:54:09 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

You left out 67 in your prime factor check. 

The only change (a minor one) which I can recommend is to streamline the 
range designation for Random, i.e., calculate it only once per call.

RandomOdd2[d_] := 
    Module[{res, rng = 10^(d-1)*{1, 10}},
      res = Random[Integer, rng];
      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, 67] == 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, rng]];
      res];


Bob Hanlon

In a message dated 2001/5/25 2:12:31 AM, nospam at newsranger.com writes:

>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.
>


  • Prev by Date: Re: Can this be made cleaner and more efficient?
  • Next by Date: RE: Showing intermediate steps in calculations
  • Previous by thread: Re: Can this be made cleaner and more efficient?
  • Next by thread: Re: Can this be made cleaner and more efficient?