Generate numbers relatively prime with a given number












4












$begingroup$


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?










share|improve this question











$endgroup$












  • $begingroup$
    What is the range of $ i $?
    $endgroup$
    – Αλέξανδρος Ζεγγ
    Dec 19 '18 at 13:52










  • $begingroup$
    @ΑλέξανδροςΖεγγ Arbitrary positive integer.
    $endgroup$
    – Kiro
    Dec 19 '18 at 13:54
















4












$begingroup$


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?










share|improve this question











$endgroup$












  • $begingroup$
    What is the range of $ i $?
    $endgroup$
    – Αλέξανδρος Ζεγγ
    Dec 19 '18 at 13:52










  • $begingroup$
    @ΑλέξανδροςΖεγγ Arbitrary positive integer.
    $endgroup$
    – Kiro
    Dec 19 '18 at 13:54














4












4








4


2



$begingroup$


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?










share|improve this question











$endgroup$




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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 19 '18 at 13:52









Αλέξανδρος Ζεγγ

4,2241929




4,2241929










asked Dec 19 '18 at 13:39









KiroKiro

916315




916315












  • $begingroup$
    What is the range of $ i $?
    $endgroup$
    – Αλέξανδρος Ζεγγ
    Dec 19 '18 at 13:52










  • $begingroup$
    @ΑλέξανδροςΖεγγ Arbitrary positive integer.
    $endgroup$
    – Kiro
    Dec 19 '18 at 13:54


















  • $begingroup$
    What is the range of $ i $?
    $endgroup$
    – Αλέξανδρος Ζεγγ
    Dec 19 '18 at 13:52










  • $begingroup$
    @ΑλέξανδροςΖεγγ Arbitrary positive integer.
    $endgroup$
    – Kiro
    Dec 19 '18 at 13:54
















$begingroup$
What is the range of $ i $?
$endgroup$
– Αλέξανδρος Ζεγγ
Dec 19 '18 at 13:52




$begingroup$
What is the range of $ i $?
$endgroup$
– Αλέξανδρος Ζεγγ
Dec 19 '18 at 13:52












$begingroup$
@ΑλέξανδροςΖεγγ Arbitrary positive integer.
$endgroup$
– Kiro
Dec 19 '18 at 13:54




$begingroup$
@ΑλέξανδροςΖεγγ Arbitrary positive integer.
$endgroup$
– Kiro
Dec 19 '18 at 13:54










3 Answers
3






active

oldest

votes


















6












$begingroup$

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







share|improve this answer









$endgroup$





















    5












    $begingroup$

    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]]
    ]





    share|improve this answer











    $endgroup$





















      1












      $begingroup$

      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 ms, 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 ms, but you still have too many different ms, discarding "old" fTables is a good idea.






      share|improve this answer









      $endgroup$













        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
        });


        }
        });














        draft saved

        draft discarded


















        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









        6












        $begingroup$

        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







        share|improve this answer









        $endgroup$


















          6












          $begingroup$

          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







          share|improve this answer









          $endgroup$
















            6












            6








            6





            $begingroup$

            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







            share|improve this answer









            $endgroup$



            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








            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 19 '18 at 15:56









            KennyColnagoKennyColnago

            12.1k1754




            12.1k1754























                5












                $begingroup$

                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]]
                ]





                share|improve this answer











                $endgroup$


















                  5












                  $begingroup$

                  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]]
                  ]





                  share|improve this answer











                  $endgroup$
















                    5












                    5








                    5





                    $begingroup$

                    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]]
                    ]





                    share|improve this answer











                    $endgroup$



                    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]]
                    ]






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 20 '18 at 7:45

























                    answered Dec 19 '18 at 14:03









                    Αλέξανδρος ΖεγγΑλέξανδρος Ζεγγ

                    4,2241929




                    4,2241929























                        1












                        $begingroup$

                        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 ms, 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 ms, but you still have too many different ms, discarding "old" fTables is a good idea.






                        share|improve this answer









                        $endgroup$


















                          1












                          $begingroup$

                          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 ms, 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 ms, but you still have too many different ms, discarding "old" fTables is a good idea.






                          share|improve this answer









                          $endgroup$
















                            1












                            1








                            1





                            $begingroup$

                            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 ms, 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 ms, but you still have too many different ms, discarding "old" fTables is a good idea.






                            share|improve this answer









                            $endgroup$



                            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 ms, 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 ms, but you still have too many different ms, discarding "old" fTables is a good idea.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 20 '18 at 6:47









                            Eric TowersEric Towers

                            2,336613




                            2,336613






























                                draft saved

                                draft discarded




















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                Bressuire

                                Cabo Verde

                                Gyllenstierna