Generate numbers relatively prime with a given number
Clash Royale CLAN TAG#URR8PPP
I am interested in a function such that f[m, i] = n
where m, n
are positive integers and n
is the i
-th number relatively prime with m
.
Getting a sample of the possible outputs of f
is straightforward. For example, let m = 30
. Now we can use
list = 2 Range[0,29] + 1;
list = Pick[list, GCD[30, list], 1]
(*1, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53, 59*)
where I'm picking from odd numbers since m
happens to be even. There should be a pattern in these numbers given by EulerPhi[30]
(this is 8
) and indeed, list[[;;8]] + 30
coincides with list[[9;;16]]
. How to continue from here?
number-theory code-request
add a comment |
I am interested in a function such that f[m, i] = n
where m, n
are positive integers and n
is the i
-th number relatively prime with m
.
Getting a sample of the possible outputs of f
is straightforward. For example, let m = 30
. Now we can use
list = 2 Range[0,29] + 1;
list = Pick[list, GCD[30, list], 1]
(*1, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53, 59*)
where I'm picking from odd numbers since m
happens to be even. There should be a pattern in these numbers given by EulerPhi[30]
(this is 8
) and indeed, list[[;;8]] + 30
coincides with list[[9;;16]]
. How to continue from here?
number-theory code-request
What is the range of $ i $?
– Αλέξανδρος Ζεγγ
Dec 19 '18 at 13:52
@ΑλέξανδροςΖεγγ Arbitrary positive integer.
– Kiro
Dec 19 '18 at 13:54
add a comment |
I am interested in a function such that f[m, i] = n
where m, n
are positive integers and n
is the i
-th number relatively prime with m
.
Getting a sample of the possible outputs of f
is straightforward. For example, let m = 30
. Now we can use
list = 2 Range[0,29] + 1;
list = Pick[list, GCD[30, list], 1]
(*1, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53, 59*)
where I'm picking from odd numbers since m
happens to be even. There should be a pattern in these numbers given by EulerPhi[30]
(this is 8
) and indeed, list[[;;8]] + 30
coincides with list[[9;;16]]
. How to continue from here?
number-theory code-request
I am interested in a function such that f[m, i] = n
where m, n
are positive integers and n
is the i
-th number relatively prime with m
.
Getting a sample of the possible outputs of f
is straightforward. For example, let m = 30
. Now we can use
list = 2 Range[0,29] + 1;
list = Pick[list, GCD[30, list], 1]
(*1, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53, 59*)
where I'm picking from odd numbers since m
happens to be even. There should be a pattern in these numbers given by EulerPhi[30]
(this is 8
) and indeed, list[[;;8]] + 30
coincides with list[[9;;16]]
. How to continue from here?
number-theory code-request
number-theory code-request
edited Dec 19 '18 at 13:52
Αλέξανδρος Ζεγγ
4,0441928
4,0441928
asked Dec 19 '18 at 13:39
Kiro
916315
916315
What is the range of $ i $?
– Αλέξανδρος Ζεγγ
Dec 19 '18 at 13:52
@ΑλέξανδροςΖεγγ Arbitrary positive integer.
– Kiro
Dec 19 '18 at 13:54
add a comment |
What is the range of $ i $?
– Αλέξανδρος Ζεγγ
Dec 19 '18 at 13:52
@ΑλέξανδροςΖεγγ Arbitrary positive integer.
– Kiro
Dec 19 '18 at 13:54
What is the range of $ i $?
– Αλέξανδρος Ζεγγ
Dec 19 '18 at 13:52
What is the range of $ i $?
– Αλέξανδρος Ζεγγ
Dec 19 '18 at 13:52
@ΑλέξανδροςΖεγγ Arbitrary positive integer.
– Kiro
Dec 19 '18 at 13:54
@ΑλέξανδροςΖεγγ Arbitrary positive integer.
– Kiro
Dec 19 '18 at 13:54
add a comment |
3 Answers
3
active
oldest
votes
To find relative primes, I've found Complement
to be generally faster than GCD
or CoprimeQ
.
RelativePrimes[m_Integer] :=
Complement[
Range[m - 1],
Apply[Sequence, Map[Range[#, m - 1, #] &, FactorInteger[m][[All, 1]]]]]
Your function f
becomes the following.
f[m_, i_] :=
Block[n = RelativePrimes[m], e = EulerPhi[m],
n[[Mod[i, e, 1]]] + m * Quotient[i - 1, e]
]
SetAttributes[f,Listable]
Thus,
f[30,Range[20]]
1, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53, 59, 61,
67, 71, 73
f[902,555]
1251
add a comment |
I give a naive implementation
ithCoprime[m_, i_] := Module[coprimes, j = 1,
coprimes = 1;
While[Length[coprimes] < i,
j++;
If[CoprimeQ[m, j], AppendTo[coprimes, j]]
];
Last[coprimes]
]
ithCoprime[30, #] & /@ Range[16]
1, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53, 59
Update
Here is a better version:
ithCoprime2[m_, i_] := Module[j = 1, k = 1,
While[k < i, j++;
If[CoprimeQ[m, j], k++]
];
j
]
Update 2
Another version
ithCoprime3[m_, i_] := Module[iterate, predicate, initial,
iterate = # + 1, Boole[CoprimeQ[m, First[#]]] &;
predicate = Last[#] <= i &;
initial = 1, 1;
NestWhile[iterate, initial, predicate, 1, [Infinity], -1][[1]]
]
add a comment |
f[m_, i_] := (
m*#[[1]] +
Select[Range[m], GCD[#, m] == 1 &][[ #[[2]] ]]
)& [ QuotientRemainder[i, EulerPhi[m]] ]
RepeatedTiming[f[223 227, 4021987]]
(* 0.058, 4057980 *)
As long as m
is not too big and you repeat m
s, you can trade some memory for time.
fTable[m_] := fSmall[m] =
Select[Range[m], GCD[#, m] == 1 &];
f[m_, i_] := (m*#[[1]] + fTable[m][[#[[2]] ]]
)& [ QuotientRemainder[i, EulerPhi[m]] ]
RepeatedTiming[f[223 227, 4021987]]
(* 0.0000110, 4057980 *)
If you are repeating m
s, but you still have too many different m
s, discarding "old" fTable
s is a good idea.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
);
);
, "mathjax-editing");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "387"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f188158%2fgenerate-numbers-relatively-prime-with-a-given-number%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
To find relative primes, I've found Complement
to be generally faster than GCD
or CoprimeQ
.
RelativePrimes[m_Integer] :=
Complement[
Range[m - 1],
Apply[Sequence, Map[Range[#, m - 1, #] &, FactorInteger[m][[All, 1]]]]]
Your function f
becomes the following.
f[m_, i_] :=
Block[n = RelativePrimes[m], e = EulerPhi[m],
n[[Mod[i, e, 1]]] + m * Quotient[i - 1, e]
]
SetAttributes[f,Listable]
Thus,
f[30,Range[20]]
1, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53, 59, 61,
67, 71, 73
f[902,555]
1251
add a comment |
To find relative primes, I've found Complement
to be generally faster than GCD
or CoprimeQ
.
RelativePrimes[m_Integer] :=
Complement[
Range[m - 1],
Apply[Sequence, Map[Range[#, m - 1, #] &, FactorInteger[m][[All, 1]]]]]
Your function f
becomes the following.
f[m_, i_] :=
Block[n = RelativePrimes[m], e = EulerPhi[m],
n[[Mod[i, e, 1]]] + m * Quotient[i - 1, e]
]
SetAttributes[f,Listable]
Thus,
f[30,Range[20]]
1, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53, 59, 61,
67, 71, 73
f[902,555]
1251
add a comment |
To find relative primes, I've found Complement
to be generally faster than GCD
or CoprimeQ
.
RelativePrimes[m_Integer] :=
Complement[
Range[m - 1],
Apply[Sequence, Map[Range[#, m - 1, #] &, FactorInteger[m][[All, 1]]]]]
Your function f
becomes the following.
f[m_, i_] :=
Block[n = RelativePrimes[m], e = EulerPhi[m],
n[[Mod[i, e, 1]]] + m * Quotient[i - 1, e]
]
SetAttributes[f,Listable]
Thus,
f[30,Range[20]]
1, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53, 59, 61,
67, 71, 73
f[902,555]
1251
To find relative primes, I've found Complement
to be generally faster than GCD
or CoprimeQ
.
RelativePrimes[m_Integer] :=
Complement[
Range[m - 1],
Apply[Sequence, Map[Range[#, m - 1, #] &, FactorInteger[m][[All, 1]]]]]
Your function f
becomes the following.
f[m_, i_] :=
Block[n = RelativePrimes[m], e = EulerPhi[m],
n[[Mod[i, e, 1]]] + m * Quotient[i - 1, e]
]
SetAttributes[f,Listable]
Thus,
f[30,Range[20]]
1, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53, 59, 61,
67, 71, 73
f[902,555]
1251
answered Dec 19 '18 at 15:56
KennyColnago
12.1k1754
12.1k1754
add a comment |
add a comment |
I give a naive implementation
ithCoprime[m_, i_] := Module[coprimes, j = 1,
coprimes = 1;
While[Length[coprimes] < i,
j++;
If[CoprimeQ[m, j], AppendTo[coprimes, j]]
];
Last[coprimes]
]
ithCoprime[30, #] & /@ Range[16]
1, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53, 59
Update
Here is a better version:
ithCoprime2[m_, i_] := Module[j = 1, k = 1,
While[k < i, j++;
If[CoprimeQ[m, j], k++]
];
j
]
Update 2
Another version
ithCoprime3[m_, i_] := Module[iterate, predicate, initial,
iterate = # + 1, Boole[CoprimeQ[m, First[#]]] &;
predicate = Last[#] <= i &;
initial = 1, 1;
NestWhile[iterate, initial, predicate, 1, [Infinity], -1][[1]]
]
add a comment |
I give a naive implementation
ithCoprime[m_, i_] := Module[coprimes, j = 1,
coprimes = 1;
While[Length[coprimes] < i,
j++;
If[CoprimeQ[m, j], AppendTo[coprimes, j]]
];
Last[coprimes]
]
ithCoprime[30, #] & /@ Range[16]
1, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53, 59
Update
Here is a better version:
ithCoprime2[m_, i_] := Module[j = 1, k = 1,
While[k < i, j++;
If[CoprimeQ[m, j], k++]
];
j
]
Update 2
Another version
ithCoprime3[m_, i_] := Module[iterate, predicate, initial,
iterate = # + 1, Boole[CoprimeQ[m, First[#]]] &;
predicate = Last[#] <= i &;
initial = 1, 1;
NestWhile[iterate, initial, predicate, 1, [Infinity], -1][[1]]
]
add a comment |
I give a naive implementation
ithCoprime[m_, i_] := Module[coprimes, j = 1,
coprimes = 1;
While[Length[coprimes] < i,
j++;
If[CoprimeQ[m, j], AppendTo[coprimes, j]]
];
Last[coprimes]
]
ithCoprime[30, #] & /@ Range[16]
1, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53, 59
Update
Here is a better version:
ithCoprime2[m_, i_] := Module[j = 1, k = 1,
While[k < i, j++;
If[CoprimeQ[m, j], k++]
];
j
]
Update 2
Another version
ithCoprime3[m_, i_] := Module[iterate, predicate, initial,
iterate = # + 1, Boole[CoprimeQ[m, First[#]]] &;
predicate = Last[#] <= i &;
initial = 1, 1;
NestWhile[iterate, initial, predicate, 1, [Infinity], -1][[1]]
]
I give a naive implementation
ithCoprime[m_, i_] := Module[coprimes, j = 1,
coprimes = 1;
While[Length[coprimes] < i,
j++;
If[CoprimeQ[m, j], AppendTo[coprimes, j]]
];
Last[coprimes]
]
ithCoprime[30, #] & /@ Range[16]
1, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53, 59
Update
Here is a better version:
ithCoprime2[m_, i_] := Module[j = 1, k = 1,
While[k < i, j++;
If[CoprimeQ[m, j], k++]
];
j
]
Update 2
Another version
ithCoprime3[m_, i_] := Module[iterate, predicate, initial,
iterate = # + 1, Boole[CoprimeQ[m, First[#]]] &;
predicate = Last[#] <= i &;
initial = 1, 1;
NestWhile[iterate, initial, predicate, 1, [Infinity], -1][[1]]
]
edited Dec 20 '18 at 7:45
answered Dec 19 '18 at 14:03
Αλέξανδρος Ζεγγ
4,0441928
4,0441928
add a comment |
add a comment |
f[m_, i_] := (
m*#[[1]] +
Select[Range[m], GCD[#, m] == 1 &][[ #[[2]] ]]
)& [ QuotientRemainder[i, EulerPhi[m]] ]
RepeatedTiming[f[223 227, 4021987]]
(* 0.058, 4057980 *)
As long as m
is not too big and you repeat m
s, you can trade some memory for time.
fTable[m_] := fSmall[m] =
Select[Range[m], GCD[#, m] == 1 &];
f[m_, i_] := (m*#[[1]] + fTable[m][[#[[2]] ]]
)& [ QuotientRemainder[i, EulerPhi[m]] ]
RepeatedTiming[f[223 227, 4021987]]
(* 0.0000110, 4057980 *)
If you are repeating m
s, but you still have too many different m
s, discarding "old" fTable
s is a good idea.
add a comment |
f[m_, i_] := (
m*#[[1]] +
Select[Range[m], GCD[#, m] == 1 &][[ #[[2]] ]]
)& [ QuotientRemainder[i, EulerPhi[m]] ]
RepeatedTiming[f[223 227, 4021987]]
(* 0.058, 4057980 *)
As long as m
is not too big and you repeat m
s, you can trade some memory for time.
fTable[m_] := fSmall[m] =
Select[Range[m], GCD[#, m] == 1 &];
f[m_, i_] := (m*#[[1]] + fTable[m][[#[[2]] ]]
)& [ QuotientRemainder[i, EulerPhi[m]] ]
RepeatedTiming[f[223 227, 4021987]]
(* 0.0000110, 4057980 *)
If you are repeating m
s, but you still have too many different m
s, discarding "old" fTable
s is a good idea.
add a comment |
f[m_, i_] := (
m*#[[1]] +
Select[Range[m], GCD[#, m] == 1 &][[ #[[2]] ]]
)& [ QuotientRemainder[i, EulerPhi[m]] ]
RepeatedTiming[f[223 227, 4021987]]
(* 0.058, 4057980 *)
As long as m
is not too big and you repeat m
s, you can trade some memory for time.
fTable[m_] := fSmall[m] =
Select[Range[m], GCD[#, m] == 1 &];
f[m_, i_] := (m*#[[1]] + fTable[m][[#[[2]] ]]
)& [ QuotientRemainder[i, EulerPhi[m]] ]
RepeatedTiming[f[223 227, 4021987]]
(* 0.0000110, 4057980 *)
If you are repeating m
s, but you still have too many different m
s, discarding "old" fTable
s is a good idea.
f[m_, i_] := (
m*#[[1]] +
Select[Range[m], GCD[#, m] == 1 &][[ #[[2]] ]]
)& [ QuotientRemainder[i, EulerPhi[m]] ]
RepeatedTiming[f[223 227, 4021987]]
(* 0.058, 4057980 *)
As long as m
is not too big and you repeat m
s, you can trade some memory for time.
fTable[m_] := fSmall[m] =
Select[Range[m], GCD[#, m] == 1 &];
f[m_, i_] := (m*#[[1]] + fTable[m][[#[[2]] ]]
)& [ QuotientRemainder[i, EulerPhi[m]] ]
RepeatedTiming[f[223 227, 4021987]]
(* 0.0000110, 4057980 *)
If you are repeating m
s, but you still have too many different m
s, discarding "old" fTable
s is a good idea.
answered Dec 20 '18 at 6:47
Eric Towers
2,246613
2,246613
add a comment |
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f188158%2fgenerate-numbers-relatively-prime-with-a-given-number%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is the range of $ i $?
– Αλέξανδρος Ζεγγ
Dec 19 '18 at 13:52
@ΑλέξανδροςΖεγγ Arbitrary positive integer.
– Kiro
Dec 19 '18 at 13:54