Re: Help with "If" statement within "Table"
- To: mathgroup at smc.vnet.net
- Subject: [mg90552] Re: Help with "If" statement within "Table"
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Sat, 12 Jul 2008 05:34:59 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <g56t9c$3qo$1@smc.vnet.net>
Diana wrote:
> 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.
<snip>
I give you a functional approach to achieve what you are looking for.
The function ccp (count coprimes} works with one and two coprime
neighbors and can be easily modified to compute many of the integer
sequences related to A136164 [1].
ccp[n_Integer, neighbors_: 1] :=
Module[{c, p},
p = Partition[Divisors[n], neighbors + 1, 1];
c = CoprimeQ[Sequence @@ #] & /@ p;
Pick[p, c] // Flatten // Union // Length
]
SetAttributes[ccp, Listable]
ccp@Range[30]
ccp[60]
ccp[Range[30], 2]
ccp[60, 2]
{0, 2, 2, 2, 2, 3, 2, 2, 2, 3, 2, 4, 2, 3, 3, 2, 2, 3, 2, 4, \
3, 3, 2, 4, 2, 3, 2, 4, 2, 5}
6
{0, 0, 0, 0, 0, 3, 0, 0, 0, 3, 0, 3, 0, 3, 3, 0, 0, 3, 0, 0, \
3, 3, 0, 3, 0, 3, 0, 0, 0, 4}
5
[1] A136164, *The On-Line Encyclopedia of Integer Sequences*,
"a(n) = the number of divisors of n that are each coprime to the
previous or the following divisor of n."
http://www.research.att.com/~njas/sequences/A136164
Regards,
-- Jean-Marc