       Re: Finding a relative prime (corrected)

• To: mathgroup at smc.vnet.net
• Subject: [mg19697] Re: [mg19682] Finding a relative prime (corrected)
• From: "Andrzej Kozlowski" <andrzej at tuins.ac.jp>
• Date: Sat, 11 Sep 1999 16:36:04 -0400
• Sender: owner-wri-mathgroup at wolfram.com

```Sorry, I answered to quickly and misunderstood your question. Of course you
want a number relatively prime to q , not a prime number. So the correct
code is:

randompick[q_?PrimeQ] :=
Module[{l}, (l = Select[Range[2, q - 1], GCD[q, #] == 1 &])[[Random[
Integer, {1, Length[l]}]]]]

E.g:

In:=
randompick[Prime]
Out=
552

Actually one can simplify this a bit by using th ebuilt in function
EulerPhi, where EulerPhi[n] is the number of positive integers less than n
which are relatively prime to n. Then the code becomes:

randompick[q_?PrimeQ] :=
Select[Range[2, q - 1],
GCD[q, #] == 1 &][[Random[Integer, {1, EulerPhi[q] - 1}]]]

--
Andrzej Kozlowski
Toyama International University
JAPAN
http://sigma.tuins.ac.jp
http://eri2.tuins.ac.jp

----------
>From: Timur Tabi <nospam_timur at tabi.org>
>To: mathgroup at smc.vnet.net
>Subject: [mg19697] [mg19682] Finding a relative prime
>Date: Thu, Sep 9, 1999, 3:19 PM
>

> I'm using Mathematica 3.0 for the Mac, and I'm trying to figure out how
> to get it to pick a random number that is relatively prime to another
> number, p-1, where p is prime.  In other words, pick a random number k
> such that 1) k is between 2 and p-1, and 2) k is relatively prime to p-1.
> How can I do that in Mathematica 3.0?
>
> --
> Remove "nospam_" from my email address when replying
> Timur "too sexy for my code" Tabi, timur at tabi.org
>
>
> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.
>
>

```

• Prev by Date: Don't want to calculate scalar product
• Next by Date: Q: how do I get the TCP port number for \$ParentLink
• Previous by thread: Re: Don't want to calculate scalar product
• Next by thread: Re: Re: Finding a relative prime (corrected)