Re: Help with "If" statement within "Table"
- To: mathgroup at smc.vnet.net
- Subject: [mg90550] Re: [mg90500] Help with "If" statement within "Table"
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Sat, 12 Jul 2008 05:34:36 -0400 (EDT)
- References: <200807110602.CAA03738@smc.vnet.net>
On 11 Jul 2008, at 15:02, Diana wrote: > Folks, > > I am trying to write a small program to count the number of divisors > of a number which have at least one coprime neighbor, and secondly the > number of divisors which have two coprime neighbors, (one on each > side). > > For example, the divisors of 60 are {1, 2, 3, 4, 5, 6, 10, 12, 15, 20, > 30, 60}. 6 of the divisors have at least one coprime neighbor, and 5 > of the divisors have two coprime neighbors. > > In doing this, I have written the following statement. "n" is the > number for which I am calculating divisors, and "A136164" is my first > list, the of number of divisors in the "at least one neighbor" case. > For some reason, A136164 is not updating when the "If" statement is > executed within the "Table" statement. Can someone help me? Thanks, > Diana M. > > Table[If[CoprimeQ[Divisors[n][[j]], Divisors[n][[j - 1]]] || > CoprimeQ[Divisors[n][[j]], Divisors[n][[j + 1]]], > A136164[[n]] = A136164[[n]] + 1,], {j, 3, > Length[Divisors[n]] - 2}]; > It's hard to tell what is wrong (if anything) because you do not tell us how you initialized A136164. But with the initialization n = 60; A136164 = ConstantArray[0, 100]; your program certainly works fine. It is, however, rather inefficient. There are many better ways to do this, for example: OCN[n_] := Module[{k = Divisors[n], l}, l = Partition[k, 2, 1]; Union[Flatten[Pick[l, CoprimeQ @@@ l]]]] will return a list of divisors which have a coprime neighbour, so in your case: OCN[60] {1, 2, 3, 4, 5, 6} (your program does not count 1 and 2). Andrzej Kozlowski
- References:
- Help with "If" statement within "Table"
- From: Diana <diana.mecum@gmail.com>
- Help with "If" statement within "Table"