Message Boards Message Boards

0
|
2893 Views
|
1 Reply
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

How do i get a random coprime from a range

Posted 9 years ago

So i want to get a random relatively prime number from a defined range. I've found the individual functions like: Range[], CoprimeQ[], Select[] and RandomChoise[]. But cant figure out how to combine them.

Lets assume the range is: r = Range[2,100]; My number to compare if relative prime is: a=100;

So desired output would be s single random number that is relatively prime to 100 say 77.

My failed attempt looks like this: RandomChoise[Select[Range[2, 100], CoprimeQ[#, 100] & /@ Range[2, 100]]]

POSTED BY: Mikael K

Your attempt was not so far away from the solution:

In[1]:= Select[Range[2, 100], CoprimeQ[#, 100] &]
Out[1]= {3, 7, 9, 11, 13, 17, 19, 21, 23, 27, 29, 31, 33, 37, 39, 41, \
43, 47, 49, 51, 53, 57, 59, 61, 63, 67, 69, 71, 73, 77, 79, 81, 83, \
87, 89, 91, 93, 97, 99}

In[5]:= RandomChoice[Select[Range[2, 100], CoprimeQ[#, 100] &], 5]
Out[5]= {77, 99, 73, 9, 57}
POSTED BY: Udo Krause
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract