Re: Nested If
- To: mathgroup at smc.vnet.net
- Subject: [mg92823] Re: [mg92779] Nested If
- From: Daniel Lichtblau <danl at wolfram.com>
- Date: Tue, 14 Oct 2008 04:57:43 -0400 (EDT)
- References: <gcn4ge$7ad$1@smc.vnet.net> <200810120833.EAA08815@smc.vnet.net> <200810131018.GAA14254@smc.vnet.net>
Artur 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
>
len = 9;
mlist = ConstantArray[-1,len]
Could do this procedurally with a nested loop.
Do [Do [If [Mod[n,Prime[k]]==0, mlist[[k]]=mlist[[k]]+1;Break[]],
{k,len}], {n,6!}]
Or use NestWhile to keep looking for a prime divisor,
Do[NestWhile[#+1&,1,(Mod[n,Prime[#]]!=0||(mlist[[#]]=mlist[[#]]+1;False))&,
1,len], {n,6!}]
Me, I'd do it the first way.
Daniel Lichtblau
Wolfram Research
- Follow-Ups:
- Re: Re: Nested If
- From: Daniel Lichtblau <danl@wolfram.com>
- Re: Re: Nested If
- References:
- Re: error region in parametric plot
- From: m.r@inbox.ru
- Nested If
- From: Artur <grafix@csl.pl>
- Re: error region in parametric plot