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

MathGroup Archive 2008

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

Search the Archive

Re: Nested If

  • To: mathgroup at smc.vnet.net
  • Subject: [mg92836] Re: [mg92779] Nested If
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Tue, 14 Oct 2008 05:00:11 -0400 (EDT)
  • Reply-to: hanlonr at cox.net

f1[k_Integer?Positive] := Module[{m1, m2, m3, m4, m5, m6, m7, m8, m9},
   {m1, m2, m3, m4, m5, m6, m7, m8, m9} = Table[-1, {9}];
   Do[If[Mod[n, 2] == 0, m1 = m1 + 1,
     If[Mod[n, 3] == 0, m2 = m2 + 1,
      If[Mod[n, 5] == 0, m3 = m3 + 1,
       If[Mod[n, 7] == 0, m4 = m4 + 1,
        If[Mod[n, 11] == 0, m5 = m5 + 1,
         If[Mod[n, 13] == 0, m6 = m6 + 1,
          If[Mod[n, 17] == 0, m7 = m7 + 1,
           If[Mod[n, 19] == 0, m8 = m8 + 1,
            If[Mod[n, 23] == 0, m9 = m9 + 1]]]]]]]]],
    {n, 1, k}];
   {m1, m2, m3, m4, m5, m6, m7, m8, m9}];

t1 = f1[6!] // Timing

{0.003438,{359,119,47,26,14,11,7,5,3}}

This can be made more flexible; however, much slower.

f2[k_Integer?Positive, i_Integer?Positive] := Module[{f, m},
   f[x_] := Catch[Fold[
      If[Mod[x, Prime[#2]] == 0, Throw[m[#2] = m[#2] + 1], #1] &,
      If[Mod[x, 2] == 0, Throw[m[1] = m[1] + 1]],
      Range[2, i]]];
   Table[m[n] = -1, {n, i}];
   f /@ Range[k];
   Table[m[n], {n, i}]];

t2 = f2[6!, 9] // Timing

{0.018913,{359,119,47,26,14,11,7,5,3}}

t2[[1]]/t1[[1]]

5.50116

This can be made slightly faster

f3[k_Integer?Positive, i_Integer?Positive] := Module[{f, m, r, p},
   p = Transpose[{r = Range[2, i], Prime[r]}];
   f[x_] := Catch[Fold[
      If[Mod[x, #2[[2]]] == 0, Throw[m[#2[[1]]] = m[#2[[1]]] + 1], #1] &,
      If[Mod[x, 2] == 0, Throw[m[1] = m[1] + 1]],
      p]];
   Table[m[n] = -1, {n, i}];
   f /@ Range[k];
   Table[m[n], {n, i}]];

t3 = f3[6!, 9] // Timing

{0.01468,{359,119,47,26,14,11,7,5,3}}

t3[[1]]/{t1[[1]], t2[[1]]}

{4.26992,0.776186}

Verifying that the results are the same:

t1[[2]] == t2[[2]] == t3[[2]]

True

For a larger range and more primes:

f3[7!, 15]

{2519,839,335,191,104,79,57,49,39,31,27,21,18,17,14}


Bob Hanlon

---- Artur <grafix at csl.pl> wrote: 

=============
Dear Mathematica Gurus,
Who know how nested or folded multiple If procedure in following:

{m1, m2, m3, m4, m5, m6, m7, m8,  m9} = {-1, -1, -1, -1, -1, -1, -1, -1, 
-1}; Do[
 If[Mod[n, 2] == 0, m1 = m1 + 1,
  If[Mod[n, 3] == 0, m2 = m2 + 1,
   If[Mod[n, 5] == 0, m3 = m3 + 1,
    If[Mod[n, 7] == 0, m4 = m4 + 1,
     If[Mod[n, 11] == 0, m5 = m5 + 1,
      If[Mod[n, 13] == 0, m6 = m6 + 1,
       If[Mod[n, 17] == 0, m7 = m7 + 1,
        If[Mod[n, 19] == 0, m8 = m8 + 1,
         If[Mod[n, 23] == 0, m9 = m9 + 1]]]]]]]]], {n, 1,   6!}];
Print[{m1, m2, m3, m4, m5, m6, m7, m8, m9}]

I want nested 9 times If[Mod[n,Prime[k]]==0,m[k]=m[k]+1],{k,1,9}]

I will be greatfull for any idea!

Best wishes
Artur



--

Bob Hanlon



  • Prev by Date: Re: Solve vs. NSolve
  • Next by Date: Re: Nested If
  • Previous by thread: Re: loops?
  • Next by thread: Multiple Slaves in Parallel Computing Toolkit