MathGroup Archive 2008

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

Search the Archive

Re: Help with "If" statement within "Table"

  • To: mathgroup at smc.vnet.net
  • Subject: [mg90545] Re: Help with "If" statement within "Table"
  • From: "Jean-Marc Gulliet" <jeanmarc.gulliet at gmail.com>
  • Date: Sat, 12 Jul 2008 05:33:38 -0400 (EDT)
  • References: <g56t9c$3qo$1@smc.vnet.net> <48773D4A.9010205@gmail.com>

On Fri, Jul 11, 2008 at 1:00 PM, Jean-Marc Gulliet
<jeanmarc.gulliet at gmail.com> wrote:
> 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

Please, disregard the above message, since I was not carreful enough
to correclty check the fucntion. The code below returns the correct
sequences.

Here are two functions ccp1 and ccp2 that gives the sequences A136164
and A136165, respectively.

In[1]:= ccp1[n_Integer] :=
 Module[{c, p}, p = Partition[Divisors[n], 2, 1];
 c = CoprimeQ[Sequence @@ #] & /@ p;
 Pick[p, c] // Flatten // Union // Length]

SetAttributes[ccp1, Listable]

ccp1@Range[30]
% === {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}
ccp1[60]

Out[3]= {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}

Out[4]= True

Out[5]= 6

In[6]:= ccp2[n_Integer] := Module[{c, p},
 p = Partition[Divisors[n], 3, 1, {2, 2}, 1];
 Cases[p, {x_Integer, y_Integer, z_Integer} /;
       CoprimeQ[y, x] && CoprimeQ[y, z] -> y] // Flatten // Union //
  Length]

SetAttributes[ccp2, Listable]

ccp2@Range[30]
% === {1, 2, 2, 1, 2, 2, 2, 1, 1, 2, 2, 3, 2, 2, 2, 1, 2, 2, 2, 1, 2,
 2, 2, 3, 1, 2, 1, 1, 2, 4}
ccp2[60]

Out[8]= {1, 2, 2, 1, 2, 2, 2, 1, 1, 2, 2, 3, 2, 2, 2, 1, 2, 2, 2, 1, \
2, 2, 2, 3, 1, 2, 1, 1, 2, 4}

Out[9]= True

Out[10]= 5

Regards,
-- 
Jean-Marc


  • Prev by Date: Re: Help with "If" statement within "Table"
  • Next by Date: Re: Help with "If" statement within "Table"
  • Previous by thread: Re: Help with "If" statement within "Table"
  • Next by thread: Re: Help with "If" statement within "Table"