Magical Modulo SquaresImplement decimal-multiplication moduloPretty Smooth MovesGenerate a modulo rosaceGenerate n-ary numbersCoprimes up to NHow many Latin Squares?Count of relatively prime partitionsAm I a Pillai prime?Find a Rocco numberMake some Prime Squares!

Did Ham the Chimp follow commands, or did he just randomly push levers?

How to increase speed on my hybrid bike with flat handlebars and 700X35C tyres?

Extracting the parent, leaf, and extension from a valid path

Why is there a cap on 401k contributions?

Why doesn't increasing the temperature of something like wood or paper set them on fire?

Why did Dr. Strange keep looking into the future after the snap?

How could a humanoid creature completely form within the span of 24 hours?

What is the meaning of "matter" in physics?

Does restarting the SQL Services (on the machine) clear the server cache (for things like query plans and statistics)?

Crime rates in a post-scarcity economy

Searching for a sentence that I only know part of it using Google's operators

Make me a minimum magic sum

Is there any optimization for thread safety in for loop of Java?

What's the 2-minute timer on mobile Deutsche Bahn tickets?

My C Drive is full without reason

Why did Gendry call himself Gendry Rivers?

If quadruped mammals evolve to become bipedal will their breast or nipple change position?

Why was Gemini VIII terminated after recovering from the OAMS thruster failure?

What detail can Hubble see on Mars?

What calendar would the Saturn nation use?

And now you see it

Translation of "invincible independence"

Displaying an Estimated Execution Plan generates CXPACKET, PAGELATCH_SH, and LATCH_EX [ACCESS_METHODS_DATASET_PARENT] waits

Are modes in jazz primarily a melody thing?



Magical Modulo Squares


Implement decimal-multiplication moduloPretty Smooth MovesGenerate a modulo rosaceGenerate n-ary numbersCoprimes up to NHow many Latin Squares?Count of relatively prime partitionsAm I a Pillai prime?Find a Rocco numberMake some Prime Squares!













3












$begingroup$


I'm a big fan of number theory. A big thing in number theory is modular arithmetic; the definition being $aequiv bmod m$ if and only if $mmid a-b$. A fun thing to do is raising to powers: especially when the modulus is a prime number. In particular, it has been proven that if $a$ and $m$ are relatively prime (share no common factors besides $1$) then there exists a number $e$ such that $a^eequiv 1mod m$.



I will explain what the exercise is by an example. Let's take a modulus $m=7$. The output of the program or function would be



3 2 6 4 5 1
2 4 1 2 4 1
6 1 6 1 6 1
4 2 1 4 2 1
5 4 6 2 3 1
1 1 1 1 1 1


Each row is a list of the powers of the first number in that row: the first row is $3, 3^2, 3^3, cdots, 3^6$, which is equivalent to $3,2,6,4,5,1$ modulo $7$. We only put numbers between $0$ and $m$ in the square, so these numbers are the only possibility. The second row of the square above is the powers of $2$, etcetera, up to the last row, which are just powers of $1$. Also, the square is symmetric; that is, the $i$th column is the same as the $i$th row. We only have the powers of the numbers $1$ through $6$, as those are the only numbers relatively prime to $7$ that lie between $0$ (inclusive) and $7$ (exclusive).



So, create a function or program that given a prime p outputs a magical modulo square, that is, a square with side lengths p-1, such that each row is a list of the consecutive powers of the first element in the row, and the same for the columns. All numbers between 0 and p must occur, and the square can only contain numbers in that range.



This is code-golf, so the shortest code wins.




P.S.: this is my first question on this stackexchange site, so if I accidentally left out important information, please do let me know. Constructive criticism on the question is much appreciated.








share|improve this question









$endgroup$











  • $begingroup$
    This is a nice first question, but just so you know, we have a sandbox whose purpose is to help with what you wrote in your post script.
    $endgroup$
    – FryAmTheEggman
    4 hours ago










  • $begingroup$
    To go along with that (and so I can delete this when you answer) is output in the form of a list of lists of numbers acceptable or is this ascii-art?
    $endgroup$
    – FryAmTheEggman
    4 hours ago






  • 1




    $begingroup$
    Could the first row be 5 4 6 2 3 1?
    $endgroup$
    – Arnauld
    4 hours ago






  • 4




    $begingroup$
    Why is the first row powers of 3?
    $endgroup$
    – J42161217
    4 hours ago






  • 1




    $begingroup$
    @J42161217 I assume this is because that is one of the rows that makes a magic square with the other rows possible. For example, if you started with the row 6 1 6 1 6 1, then you wouldn't be able to form the square, since there aren't multiple rows that start with 6 nor 1. As to why it starts with powers of 3 rather than say, the also valid powers of 5 as Arnauld comments, I don't know, and I hope the asker specifies this, as I quite like the challenge.
    $endgroup$
    – Jo King
    33 mins ago
















3












$begingroup$


I'm a big fan of number theory. A big thing in number theory is modular arithmetic; the definition being $aequiv bmod m$ if and only if $mmid a-b$. A fun thing to do is raising to powers: especially when the modulus is a prime number. In particular, it has been proven that if $a$ and $m$ are relatively prime (share no common factors besides $1$) then there exists a number $e$ such that $a^eequiv 1mod m$.



I will explain what the exercise is by an example. Let's take a modulus $m=7$. The output of the program or function would be



3 2 6 4 5 1
2 4 1 2 4 1
6 1 6 1 6 1
4 2 1 4 2 1
5 4 6 2 3 1
1 1 1 1 1 1


Each row is a list of the powers of the first number in that row: the first row is $3, 3^2, 3^3, cdots, 3^6$, which is equivalent to $3,2,6,4,5,1$ modulo $7$. We only put numbers between $0$ and $m$ in the square, so these numbers are the only possibility. The second row of the square above is the powers of $2$, etcetera, up to the last row, which are just powers of $1$. Also, the square is symmetric; that is, the $i$th column is the same as the $i$th row. We only have the powers of the numbers $1$ through $6$, as those are the only numbers relatively prime to $7$ that lie between $0$ (inclusive) and $7$ (exclusive).



So, create a function or program that given a prime p outputs a magical modulo square, that is, a square with side lengths p-1, such that each row is a list of the consecutive powers of the first element in the row, and the same for the columns. All numbers between 0 and p must occur, and the square can only contain numbers in that range.



This is code-golf, so the shortest code wins.




P.S.: this is my first question on this stackexchange site, so if I accidentally left out important information, please do let me know. Constructive criticism on the question is much appreciated.








share|improve this question









$endgroup$











  • $begingroup$
    This is a nice first question, but just so you know, we have a sandbox whose purpose is to help with what you wrote in your post script.
    $endgroup$
    – FryAmTheEggman
    4 hours ago










  • $begingroup$
    To go along with that (and so I can delete this when you answer) is output in the form of a list of lists of numbers acceptable or is this ascii-art?
    $endgroup$
    – FryAmTheEggman
    4 hours ago






  • 1




    $begingroup$
    Could the first row be 5 4 6 2 3 1?
    $endgroup$
    – Arnauld
    4 hours ago






  • 4




    $begingroup$
    Why is the first row powers of 3?
    $endgroup$
    – J42161217
    4 hours ago






  • 1




    $begingroup$
    @J42161217 I assume this is because that is one of the rows that makes a magic square with the other rows possible. For example, if you started with the row 6 1 6 1 6 1, then you wouldn't be able to form the square, since there aren't multiple rows that start with 6 nor 1. As to why it starts with powers of 3 rather than say, the also valid powers of 5 as Arnauld comments, I don't know, and I hope the asker specifies this, as I quite like the challenge.
    $endgroup$
    – Jo King
    33 mins ago














3












3








3





$begingroup$


I'm a big fan of number theory. A big thing in number theory is modular arithmetic; the definition being $aequiv bmod m$ if and only if $mmid a-b$. A fun thing to do is raising to powers: especially when the modulus is a prime number. In particular, it has been proven that if $a$ and $m$ are relatively prime (share no common factors besides $1$) then there exists a number $e$ such that $a^eequiv 1mod m$.



I will explain what the exercise is by an example. Let's take a modulus $m=7$. The output of the program or function would be



3 2 6 4 5 1
2 4 1 2 4 1
6 1 6 1 6 1
4 2 1 4 2 1
5 4 6 2 3 1
1 1 1 1 1 1


Each row is a list of the powers of the first number in that row: the first row is $3, 3^2, 3^3, cdots, 3^6$, which is equivalent to $3,2,6,4,5,1$ modulo $7$. We only put numbers between $0$ and $m$ in the square, so these numbers are the only possibility. The second row of the square above is the powers of $2$, etcetera, up to the last row, which are just powers of $1$. Also, the square is symmetric; that is, the $i$th column is the same as the $i$th row. We only have the powers of the numbers $1$ through $6$, as those are the only numbers relatively prime to $7$ that lie between $0$ (inclusive) and $7$ (exclusive).



So, create a function or program that given a prime p outputs a magical modulo square, that is, a square with side lengths p-1, such that each row is a list of the consecutive powers of the first element in the row, and the same for the columns. All numbers between 0 and p must occur, and the square can only contain numbers in that range.



This is code-golf, so the shortest code wins.




P.S.: this is my first question on this stackexchange site, so if I accidentally left out important information, please do let me know. Constructive criticism on the question is much appreciated.








share|improve this question









$endgroup$




I'm a big fan of number theory. A big thing in number theory is modular arithmetic; the definition being $aequiv bmod m$ if and only if $mmid a-b$. A fun thing to do is raising to powers: especially when the modulus is a prime number. In particular, it has been proven that if $a$ and $m$ are relatively prime (share no common factors besides $1$) then there exists a number $e$ such that $a^eequiv 1mod m$.



I will explain what the exercise is by an example. Let's take a modulus $m=7$. The output of the program or function would be



3 2 6 4 5 1
2 4 1 2 4 1
6 1 6 1 6 1
4 2 1 4 2 1
5 4 6 2 3 1
1 1 1 1 1 1


Each row is a list of the powers of the first number in that row: the first row is $3, 3^2, 3^3, cdots, 3^6$, which is equivalent to $3,2,6,4,5,1$ modulo $7$. We only put numbers between $0$ and $m$ in the square, so these numbers are the only possibility. The second row of the square above is the powers of $2$, etcetera, up to the last row, which are just powers of $1$. Also, the square is symmetric; that is, the $i$th column is the same as the $i$th row. We only have the powers of the numbers $1$ through $6$, as those are the only numbers relatively prime to $7$ that lie between $0$ (inclusive) and $7$ (exclusive).



So, create a function or program that given a prime p outputs a magical modulo square, that is, a square with side lengths p-1, such that each row is a list of the consecutive powers of the first element in the row, and the same for the columns. All numbers between 0 and p must occur, and the square can only contain numbers in that range.



This is code-golf, so the shortest code wins.




P.S.: this is my first question on this stackexchange site, so if I accidentally left out important information, please do let me know. Constructive criticism on the question is much appreciated.





code-golf number-theory






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 5 hours ago









vrugtehagelvrugtehagel

1365




1365











  • $begingroup$
    This is a nice first question, but just so you know, we have a sandbox whose purpose is to help with what you wrote in your post script.
    $endgroup$
    – FryAmTheEggman
    4 hours ago










  • $begingroup$
    To go along with that (and so I can delete this when you answer) is output in the form of a list of lists of numbers acceptable or is this ascii-art?
    $endgroup$
    – FryAmTheEggman
    4 hours ago






  • 1




    $begingroup$
    Could the first row be 5 4 6 2 3 1?
    $endgroup$
    – Arnauld
    4 hours ago






  • 4




    $begingroup$
    Why is the first row powers of 3?
    $endgroup$
    – J42161217
    4 hours ago






  • 1




    $begingroup$
    @J42161217 I assume this is because that is one of the rows that makes a magic square with the other rows possible. For example, if you started with the row 6 1 6 1 6 1, then you wouldn't be able to form the square, since there aren't multiple rows that start with 6 nor 1. As to why it starts with powers of 3 rather than say, the also valid powers of 5 as Arnauld comments, I don't know, and I hope the asker specifies this, as I quite like the challenge.
    $endgroup$
    – Jo King
    33 mins ago

















  • $begingroup$
    This is a nice first question, but just so you know, we have a sandbox whose purpose is to help with what you wrote in your post script.
    $endgroup$
    – FryAmTheEggman
    4 hours ago










  • $begingroup$
    To go along with that (and so I can delete this when you answer) is output in the form of a list of lists of numbers acceptable or is this ascii-art?
    $endgroup$
    – FryAmTheEggman
    4 hours ago






  • 1




    $begingroup$
    Could the first row be 5 4 6 2 3 1?
    $endgroup$
    – Arnauld
    4 hours ago






  • 4




    $begingroup$
    Why is the first row powers of 3?
    $endgroup$
    – J42161217
    4 hours ago






  • 1




    $begingroup$
    @J42161217 I assume this is because that is one of the rows that makes a magic square with the other rows possible. For example, if you started with the row 6 1 6 1 6 1, then you wouldn't be able to form the square, since there aren't multiple rows that start with 6 nor 1. As to why it starts with powers of 3 rather than say, the also valid powers of 5 as Arnauld comments, I don't know, and I hope the asker specifies this, as I quite like the challenge.
    $endgroup$
    – Jo King
    33 mins ago
















$begingroup$
This is a nice first question, but just so you know, we have a sandbox whose purpose is to help with what you wrote in your post script.
$endgroup$
– FryAmTheEggman
4 hours ago




$begingroup$
This is a nice first question, but just so you know, we have a sandbox whose purpose is to help with what you wrote in your post script.
$endgroup$
– FryAmTheEggman
4 hours ago












$begingroup$
To go along with that (and so I can delete this when you answer) is output in the form of a list of lists of numbers acceptable or is this ascii-art?
$endgroup$
– FryAmTheEggman
4 hours ago




$begingroup$
To go along with that (and so I can delete this when you answer) is output in the form of a list of lists of numbers acceptable or is this ascii-art?
$endgroup$
– FryAmTheEggman
4 hours ago




1




1




$begingroup$
Could the first row be 5 4 6 2 3 1?
$endgroup$
– Arnauld
4 hours ago




$begingroup$
Could the first row be 5 4 6 2 3 1?
$endgroup$
– Arnauld
4 hours ago




4




4




$begingroup$
Why is the first row powers of 3?
$endgroup$
– J42161217
4 hours ago




$begingroup$
Why is the first row powers of 3?
$endgroup$
– J42161217
4 hours ago




1




1




$begingroup$
@J42161217 I assume this is because that is one of the rows that makes a magic square with the other rows possible. For example, if you started with the row 6 1 6 1 6 1, then you wouldn't be able to form the square, since there aren't multiple rows that start with 6 nor 1. As to why it starts with powers of 3 rather than say, the also valid powers of 5 as Arnauld comments, I don't know, and I hope the asker specifies this, as I quite like the challenge.
$endgroup$
– Jo King
33 mins ago





$begingroup$
@J42161217 I assume this is because that is one of the rows that makes a magic square with the other rows possible. For example, if you started with the row 6 1 6 1 6 1, then you wouldn't be able to form the square, since there aren't multiple rows that start with 6 nor 1. As to why it starts with powers of 3 rather than say, the also valid powers of 5 as Arnauld comments, I don't know, and I hope the asker specifies this, as I quite like the challenge.
$endgroup$
– Jo King
33 mins ago











6 Answers
6






active

oldest

votes


















1












$begingroup$


Wolfram Language (Mathematica), 46 bytes



Mod[r=Range[#-1];#^r&/@(PrimitiveRoot@#^r),#]&


Try it online!






share|improve this answer









$endgroup$




















    1












    $begingroup$


    Charcoal, 36 bytes



    ≔E…¹θ﹪Xι…¹θIθηE⊟Φη⁼¹№ι¹⪫E§η⊖ι◧IλL⊖θ 


    Try it online! Link is to verbose version of code. Note: Trailing space. Explanation:



    ≔E…¹θ﹪Xι…¹θIθη


    Create a p-1 by p-1 array of powers of 1..p-1 to indices 1..p-1 (modulo p).



    E⊟Φη⁼¹№ι¹


    Map over one of the rows which has exactly one 1.



    ⪫E§η⊖ι◧IλL⊖θ 


    Rearrange the rows into the order given by the selected row and format the output.






    share|improve this answer









    $endgroup$




















      0












      $begingroup$


      Wolfram Language (Mathematica), 67 bytes



      (s=Thread@Mod[x^#&/@(x=Range[#-1]),#])[[#&@@Select[s,Sort@#==x&]]]&


      Try it online!






      share|improve this answer











      $endgroup$




















        0












        $begingroup$

        JavaScript (ES7), 91 bytes



        This version attempts to compute the powers before applying the modulo and will fail for $pge11$ because of loss of precision.





        f=(p,k)=>(g=k=>b=[...Array(i=p-1)].map(_=>k**++i%p))(k).every(x=>g[x]^=1)?b.map(g):f(p,-~k)


        Try it online!




        JavaScript (ES6), 92 bytes



        This version uses modular exponentiation to support higher input values.





        f=(p,k)=>(g=k=>b=[...Array(p-1)].map(_=>n=n*k%p,n=1))(k).every(x=>g[x]^=1)?b.map(g):f(p,-~k)


        Try it online!






        share|improve this answer











        $endgroup$












        • $begingroup$
          .indexOf(1)>p-3 saves 3 bytes over .every.
          $endgroup$
          – Neil
          2 hours ago


















        0












        $begingroup$


        Perl 6, 65 57 bytes





        .first(*.Set+2>$_)]o.&@=(($++X**1..^$_)X%$_)xx$_


        Try it online!



        There's probably some way to just output the square itself, but this does the same process outlined in the question, sorting the lists by their positions in the first list that is just a permutation of 1 to input-1. Returns as a list of lists.



        BTW, there's a lot of jockeying around, trying to get around some of Perl 6's annoying limitations involving sequences vs arrays, and anonymous variables.



        Explanation:



         $++ xx$_ # Map 0 to i-1 to
        ( X**1..^$_) # n, n^2, n^3... n^(i-1)
        ( X%$_) # All modulo i
        o.& # Pass to the next function
        .[ ] # Index into that list of lists
        |.first( ) # The list of the first list that
        *.Set+2>$_ # Has all the elements in the range 1 to i-1





        share|improve this answer











        $endgroup$




















          0












          $begingroup$


          Jelly, 13 bytes



          Feels like the repeated code should be golf-able, but I have not managed it...



          *€Ṗ%⁸QƑƇḢ*€Ṗ%


          Try it online! (footer pretty-formats as a grid)



          How?



          *€Ṗ%⁸QƑƇḢ*€Ṗ% - Link: integer, p
          € - for each n in [1..p]
          * - exponentiate with:
          Ṗ - pop = [1..p-1]
          - ...i.e [[1^1,1^2,...,1^(p-1)],[2^1,2^2,...,2^(p-1)],...,[....,p^(p-1)]]
          % - modulo:
          ⁸ - chain's left argument, p
          Ƈ - keep those which:
          Ƒ - are invariant under:
          Q - de-duplicate
          Ḣ - head
          € - for each v in that list:
          * - exponentiate with:
          Ṗ - pop = [1..p-1]
          % - modulo (p)



          Less repeated code, also 13:



          *€%³
          çṖQƑƇḢçṖ





          share|improve this answer









          $endgroup$













            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "200"
            ;
            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%2fcodegolf.stackexchange.com%2fquestions%2f185250%2fmagical-modulo-squares%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            6 Answers
            6






            active

            oldest

            votes








            6 Answers
            6






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1












            $begingroup$


            Wolfram Language (Mathematica), 46 bytes



            Mod[r=Range[#-1];#^r&/@(PrimitiveRoot@#^r),#]&


            Try it online!






            share|improve this answer









            $endgroup$

















              1












              $begingroup$


              Wolfram Language (Mathematica), 46 bytes



              Mod[r=Range[#-1];#^r&/@(PrimitiveRoot@#^r),#]&


              Try it online!






              share|improve this answer









              $endgroup$















                1












                1








                1





                $begingroup$


                Wolfram Language (Mathematica), 46 bytes



                Mod[r=Range[#-1];#^r&/@(PrimitiveRoot@#^r),#]&


                Try it online!






                share|improve this answer









                $endgroup$




                Wolfram Language (Mathematica), 46 bytes



                Mod[r=Range[#-1];#^r&/@(PrimitiveRoot@#^r),#]&


                Try it online!







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 hours ago









                attinatattinat

                78917




                78917





















                    1












                    $begingroup$


                    Charcoal, 36 bytes



                    ≔E…¹θ﹪Xι…¹θIθηE⊟Φη⁼¹№ι¹⪫E§η⊖ι◧IλL⊖θ 


                    Try it online! Link is to verbose version of code. Note: Trailing space. Explanation:



                    ≔E…¹θ﹪Xι…¹θIθη


                    Create a p-1 by p-1 array of powers of 1..p-1 to indices 1..p-1 (modulo p).



                    E⊟Φη⁼¹№ι¹


                    Map over one of the rows which has exactly one 1.



                    ⪫E§η⊖ι◧IλL⊖θ 


                    Rearrange the rows into the order given by the selected row and format the output.






                    share|improve this answer









                    $endgroup$

















                      1












                      $begingroup$


                      Charcoal, 36 bytes



                      ≔E…¹θ﹪Xι…¹θIθηE⊟Φη⁼¹№ι¹⪫E§η⊖ι◧IλL⊖θ 


                      Try it online! Link is to verbose version of code. Note: Trailing space. Explanation:



                      ≔E…¹θ﹪Xι…¹θIθη


                      Create a p-1 by p-1 array of powers of 1..p-1 to indices 1..p-1 (modulo p).



                      E⊟Φη⁼¹№ι¹


                      Map over one of the rows which has exactly one 1.



                      ⪫E§η⊖ι◧IλL⊖θ 


                      Rearrange the rows into the order given by the selected row and format the output.






                      share|improve this answer









                      $endgroup$















                        1












                        1








                        1





                        $begingroup$


                        Charcoal, 36 bytes



                        ≔E…¹θ﹪Xι…¹θIθηE⊟Φη⁼¹№ι¹⪫E§η⊖ι◧IλL⊖θ 


                        Try it online! Link is to verbose version of code. Note: Trailing space. Explanation:



                        ≔E…¹θ﹪Xι…¹θIθη


                        Create a p-1 by p-1 array of powers of 1..p-1 to indices 1..p-1 (modulo p).



                        E⊟Φη⁼¹№ι¹


                        Map over one of the rows which has exactly one 1.



                        ⪫E§η⊖ι◧IλL⊖θ 


                        Rearrange the rows into the order given by the selected row and format the output.






                        share|improve this answer









                        $endgroup$




                        Charcoal, 36 bytes



                        ≔E…¹θ﹪Xι…¹θIθηE⊟Φη⁼¹№ι¹⪫E§η⊖ι◧IλL⊖θ 


                        Try it online! Link is to verbose version of code. Note: Trailing space. Explanation:



                        ≔E…¹θ﹪Xι…¹θIθη


                        Create a p-1 by p-1 array of powers of 1..p-1 to indices 1..p-1 (modulo p).



                        E⊟Φη⁼¹№ι¹


                        Map over one of the rows which has exactly one 1.



                        ⪫E§η⊖ι◧IλL⊖θ 


                        Rearrange the rows into the order given by the selected row and format the output.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered 2 hours ago









                        NeilNeil

                        83.6k745179




                        83.6k745179





















                            0












                            $begingroup$


                            Wolfram Language (Mathematica), 67 bytes



                            (s=Thread@Mod[x^#&/@(x=Range[#-1]),#])[[#&@@Select[s,Sort@#==x&]]]&


                            Try it online!






                            share|improve this answer











                            $endgroup$

















                              0












                              $begingroup$


                              Wolfram Language (Mathematica), 67 bytes



                              (s=Thread@Mod[x^#&/@(x=Range[#-1]),#])[[#&@@Select[s,Sort@#==x&]]]&


                              Try it online!






                              share|improve this answer











                              $endgroup$















                                0












                                0








                                0





                                $begingroup$


                                Wolfram Language (Mathematica), 67 bytes



                                (s=Thread@Mod[x^#&/@(x=Range[#-1]),#])[[#&@@Select[s,Sort@#==x&]]]&


                                Try it online!






                                share|improve this answer











                                $endgroup$




                                Wolfram Language (Mathematica), 67 bytes



                                (s=Thread@Mod[x^#&/@(x=Range[#-1]),#])[[#&@@Select[s,Sort@#==x&]]]&


                                Try it online!







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited 3 hours ago

























                                answered 4 hours ago









                                J42161217J42161217

                                14.9k21457




                                14.9k21457





















                                    0












                                    $begingroup$

                                    JavaScript (ES7), 91 bytes



                                    This version attempts to compute the powers before applying the modulo and will fail for $pge11$ because of loss of precision.





                                    f=(p,k)=>(g=k=>b=[...Array(i=p-1)].map(_=>k**++i%p))(k).every(x=>g[x]^=1)?b.map(g):f(p,-~k)


                                    Try it online!




                                    JavaScript (ES6), 92 bytes



                                    This version uses modular exponentiation to support higher input values.





                                    f=(p,k)=>(g=k=>b=[...Array(p-1)].map(_=>n=n*k%p,n=1))(k).every(x=>g[x]^=1)?b.map(g):f(p,-~k)


                                    Try it online!






                                    share|improve this answer











                                    $endgroup$












                                    • $begingroup$
                                      .indexOf(1)>p-3 saves 3 bytes over .every.
                                      $endgroup$
                                      – Neil
                                      2 hours ago















                                    0












                                    $begingroup$

                                    JavaScript (ES7), 91 bytes



                                    This version attempts to compute the powers before applying the modulo and will fail for $pge11$ because of loss of precision.





                                    f=(p,k)=>(g=k=>b=[...Array(i=p-1)].map(_=>k**++i%p))(k).every(x=>g[x]^=1)?b.map(g):f(p,-~k)


                                    Try it online!




                                    JavaScript (ES6), 92 bytes



                                    This version uses modular exponentiation to support higher input values.





                                    f=(p,k)=>(g=k=>b=[...Array(p-1)].map(_=>n=n*k%p,n=1))(k).every(x=>g[x]^=1)?b.map(g):f(p,-~k)


                                    Try it online!






                                    share|improve this answer











                                    $endgroup$












                                    • $begingroup$
                                      .indexOf(1)>p-3 saves 3 bytes over .every.
                                      $endgroup$
                                      – Neil
                                      2 hours ago













                                    0












                                    0








                                    0





                                    $begingroup$

                                    JavaScript (ES7), 91 bytes



                                    This version attempts to compute the powers before applying the modulo and will fail for $pge11$ because of loss of precision.





                                    f=(p,k)=>(g=k=>b=[...Array(i=p-1)].map(_=>k**++i%p))(k).every(x=>g[x]^=1)?b.map(g):f(p,-~k)


                                    Try it online!




                                    JavaScript (ES6), 92 bytes



                                    This version uses modular exponentiation to support higher input values.





                                    f=(p,k)=>(g=k=>b=[...Array(p-1)].map(_=>n=n*k%p,n=1))(k).every(x=>g[x]^=1)?b.map(g):f(p,-~k)


                                    Try it online!






                                    share|improve this answer











                                    $endgroup$



                                    JavaScript (ES7), 91 bytes



                                    This version attempts to compute the powers before applying the modulo and will fail for $pge11$ because of loss of precision.





                                    f=(p,k)=>(g=k=>b=[...Array(i=p-1)].map(_=>k**++i%p))(k).every(x=>g[x]^=1)?b.map(g):f(p,-~k)


                                    Try it online!




                                    JavaScript (ES6), 92 bytes



                                    This version uses modular exponentiation to support higher input values.





                                    f=(p,k)=>(g=k=>b=[...Array(p-1)].map(_=>n=n*k%p,n=1))(k).every(x=>g[x]^=1)?b.map(g):f(p,-~k)


                                    Try it online!







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited 2 hours ago

























                                    answered 3 hours ago









                                    ArnauldArnauld

                                    83.1k798340




                                    83.1k798340











                                    • $begingroup$
                                      .indexOf(1)>p-3 saves 3 bytes over .every.
                                      $endgroup$
                                      – Neil
                                      2 hours ago
















                                    • $begingroup$
                                      .indexOf(1)>p-3 saves 3 bytes over .every.
                                      $endgroup$
                                      – Neil
                                      2 hours ago















                                    $begingroup$
                                    .indexOf(1)>p-3 saves 3 bytes over .every.
                                    $endgroup$
                                    – Neil
                                    2 hours ago




                                    $begingroup$
                                    .indexOf(1)>p-3 saves 3 bytes over .every.
                                    $endgroup$
                                    – Neil
                                    2 hours ago











                                    0












                                    $begingroup$


                                    Perl 6, 65 57 bytes





                                    .first(*.Set+2>$_)]o.&@=(($++X**1..^$_)X%$_)xx$_


                                    Try it online!



                                    There's probably some way to just output the square itself, but this does the same process outlined in the question, sorting the lists by their positions in the first list that is just a permutation of 1 to input-1. Returns as a list of lists.



                                    BTW, there's a lot of jockeying around, trying to get around some of Perl 6's annoying limitations involving sequences vs arrays, and anonymous variables.



                                    Explanation:



                                     $++ xx$_ # Map 0 to i-1 to
                                    ( X**1..^$_) # n, n^2, n^3... n^(i-1)
                                    ( X%$_) # All modulo i
                                    o.& # Pass to the next function
                                    .[ ] # Index into that list of lists
                                    |.first( ) # The list of the first list that
                                    *.Set+2>$_ # Has all the elements in the range 1 to i-1





                                    share|improve this answer











                                    $endgroup$

















                                      0












                                      $begingroup$


                                      Perl 6, 65 57 bytes





                                      .first(*.Set+2>$_)]o.&@=(($++X**1..^$_)X%$_)xx$_


                                      Try it online!



                                      There's probably some way to just output the square itself, but this does the same process outlined in the question, sorting the lists by their positions in the first list that is just a permutation of 1 to input-1. Returns as a list of lists.



                                      BTW, there's a lot of jockeying around, trying to get around some of Perl 6's annoying limitations involving sequences vs arrays, and anonymous variables.



                                      Explanation:



                                       $++ xx$_ # Map 0 to i-1 to
                                      ( X**1..^$_) # n, n^2, n^3... n^(i-1)
                                      ( X%$_) # All modulo i
                                      o.& # Pass to the next function
                                      .[ ] # Index into that list of lists
                                      |.first( ) # The list of the first list that
                                      *.Set+2>$_ # Has all the elements in the range 1 to i-1





                                      share|improve this answer











                                      $endgroup$















                                        0












                                        0








                                        0





                                        $begingroup$


                                        Perl 6, 65 57 bytes





                                        .first(*.Set+2>$_)]o.&@=(($++X**1..^$_)X%$_)xx$_


                                        Try it online!



                                        There's probably some way to just output the square itself, but this does the same process outlined in the question, sorting the lists by their positions in the first list that is just a permutation of 1 to input-1. Returns as a list of lists.



                                        BTW, there's a lot of jockeying around, trying to get around some of Perl 6's annoying limitations involving sequences vs arrays, and anonymous variables.



                                        Explanation:



                                         $++ xx$_ # Map 0 to i-1 to
                                        ( X**1..^$_) # n, n^2, n^3... n^(i-1)
                                        ( X%$_) # All modulo i
                                        o.& # Pass to the next function
                                        .[ ] # Index into that list of lists
                                        |.first( ) # The list of the first list that
                                        *.Set+2>$_ # Has all the elements in the range 1 to i-1





                                        share|improve this answer











                                        $endgroup$




                                        Perl 6, 65 57 bytes





                                        .first(*.Set+2>$_)]o.&@=(($++X**1..^$_)X%$_)xx$_


                                        Try it online!



                                        There's probably some way to just output the square itself, but this does the same process outlined in the question, sorting the lists by their positions in the first list that is just a permutation of 1 to input-1. Returns as a list of lists.



                                        BTW, there's a lot of jockeying around, trying to get around some of Perl 6's annoying limitations involving sequences vs arrays, and anonymous variables.



                                        Explanation:



                                         $++ xx$_ # Map 0 to i-1 to
                                        ( X**1..^$_) # n, n^2, n^3... n^(i-1)
                                        ( X%$_) # All modulo i
                                        o.& # Pass to the next function
                                        .[ ] # Index into that list of lists
                                        |.first( ) # The list of the first list that
                                        *.Set+2>$_ # Has all the elements in the range 1 to i-1






                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited 1 hour ago

























                                        answered 3 hours ago









                                        Jo KingJo King

                                        27.9k366134




                                        27.9k366134





















                                            0












                                            $begingroup$


                                            Jelly, 13 bytes



                                            Feels like the repeated code should be golf-able, but I have not managed it...



                                            *€Ṗ%⁸QƑƇḢ*€Ṗ%


                                            Try it online! (footer pretty-formats as a grid)



                                            How?



                                            *€Ṗ%⁸QƑƇḢ*€Ṗ% - Link: integer, p
                                            € - for each n in [1..p]
                                            * - exponentiate with:
                                            Ṗ - pop = [1..p-1]
                                            - ...i.e [[1^1,1^2,...,1^(p-1)],[2^1,2^2,...,2^(p-1)],...,[....,p^(p-1)]]
                                            % - modulo:
                                            ⁸ - chain's left argument, p
                                            Ƈ - keep those which:
                                            Ƒ - are invariant under:
                                            Q - de-duplicate
                                            Ḣ - head
                                            € - for each v in that list:
                                            * - exponentiate with:
                                            Ṗ - pop = [1..p-1]
                                            % - modulo (p)



                                            Less repeated code, also 13:



                                            *€%³
                                            çṖQƑƇḢçṖ





                                            share|improve this answer









                                            $endgroup$

















                                              0












                                              $begingroup$


                                              Jelly, 13 bytes



                                              Feels like the repeated code should be golf-able, but I have not managed it...



                                              *€Ṗ%⁸QƑƇḢ*€Ṗ%


                                              Try it online! (footer pretty-formats as a grid)



                                              How?



                                              *€Ṗ%⁸QƑƇḢ*€Ṗ% - Link: integer, p
                                              € - for each n in [1..p]
                                              * - exponentiate with:
                                              Ṗ - pop = [1..p-1]
                                              - ...i.e [[1^1,1^2,...,1^(p-1)],[2^1,2^2,...,2^(p-1)],...,[....,p^(p-1)]]
                                              % - modulo:
                                              ⁸ - chain's left argument, p
                                              Ƈ - keep those which:
                                              Ƒ - are invariant under:
                                              Q - de-duplicate
                                              Ḣ - head
                                              € - for each v in that list:
                                              * - exponentiate with:
                                              Ṗ - pop = [1..p-1]
                                              % - modulo (p)



                                              Less repeated code, also 13:



                                              *€%³
                                              çṖQƑƇḢçṖ





                                              share|improve this answer









                                              $endgroup$















                                                0












                                                0








                                                0





                                                $begingroup$


                                                Jelly, 13 bytes



                                                Feels like the repeated code should be golf-able, but I have not managed it...



                                                *€Ṗ%⁸QƑƇḢ*€Ṗ%


                                                Try it online! (footer pretty-formats as a grid)



                                                How?



                                                *€Ṗ%⁸QƑƇḢ*€Ṗ% - Link: integer, p
                                                € - for each n in [1..p]
                                                * - exponentiate with:
                                                Ṗ - pop = [1..p-1]
                                                - ...i.e [[1^1,1^2,...,1^(p-1)],[2^1,2^2,...,2^(p-1)],...,[....,p^(p-1)]]
                                                % - modulo:
                                                ⁸ - chain's left argument, p
                                                Ƈ - keep those which:
                                                Ƒ - are invariant under:
                                                Q - de-duplicate
                                                Ḣ - head
                                                € - for each v in that list:
                                                * - exponentiate with:
                                                Ṗ - pop = [1..p-1]
                                                % - modulo (p)



                                                Less repeated code, also 13:



                                                *€%³
                                                çṖQƑƇḢçṖ





                                                share|improve this answer









                                                $endgroup$




                                                Jelly, 13 bytes



                                                Feels like the repeated code should be golf-able, but I have not managed it...



                                                *€Ṗ%⁸QƑƇḢ*€Ṗ%


                                                Try it online! (footer pretty-formats as a grid)



                                                How?



                                                *€Ṗ%⁸QƑƇḢ*€Ṗ% - Link: integer, p
                                                € - for each n in [1..p]
                                                * - exponentiate with:
                                                Ṗ - pop = [1..p-1]
                                                - ...i.e [[1^1,1^2,...,1^(p-1)],[2^1,2^2,...,2^(p-1)],...,[....,p^(p-1)]]
                                                % - modulo:
                                                ⁸ - chain's left argument, p
                                                Ƈ - keep those which:
                                                Ƒ - are invariant under:
                                                Q - de-duplicate
                                                Ḣ - head
                                                € - for each v in that list:
                                                * - exponentiate with:
                                                Ṗ - pop = [1..p-1]
                                                % - modulo (p)



                                                Less repeated code, also 13:



                                                *€%³
                                                çṖQƑƇḢçṖ






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered 12 mins ago









                                                Jonathan AllanJonathan Allan

                                                55.4k537177




                                                55.4k537177



























                                                    draft saved

                                                    draft discarded
















































                                                    If this is an answer to a challenge…



                                                    • …Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.


                                                    • …Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
                                                      Explanations of your answer make it more interesting to read and are very much encouraged.


                                                    • …Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.


                                                    More generally…



                                                    • …Please make sure to answer the question and provide sufficient detail.


                                                    • …Avoid asking for help, clarification or responding to other answers (use comments instead).




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function ()
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f185250%2fmagical-modulo-squares%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

                                                    Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                                                    Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                                                    Tom Holland Mục lục Đầu đời và giáo dục | Sự nghiệp | Cuộc sống cá nhân | Phim tham gia | Giải thưởng và đề cử | Chú thích | Liên kết ngoài | Trình đơn chuyển hướngProfile“Person Details for Thomas Stanley Holland, "England and Wales Birth Registration Index, 1837-2008" — FamilySearch.org”"Meet Tom Holland... the 16-year-old star of The Impossible""Schoolboy actor Tom Holland finds himself in Oscar contention for role in tsunami drama"“Naomi Watts on the Prince William and Harry's reaction to her film about the late Princess Diana”lưu trữ"Holland and Pflueger Are West End's Two New 'Billy Elliots'""I'm so envious of my son, the movie star! British writer Dominic Holland's spent 20 years trying to crack Hollywood - but he's been beaten to it by a very unlikely rival"“Richard and Margaret Povey of Jersey, Channel Islands, UK: Information about Thomas Stanley Holland”"Tom Holland to play Billy Elliot""New Billy Elliot leaving the garage"Billy Elliot the Musical - Tom Holland - Billy"A Tale of four Billys: Tom Holland""The Feel Good Factor""Thames Christian College schoolboys join Myleene Klass for The Feelgood Factor""Government launches £600,000 arts bursaries pilot""BILLY's Chapman, Holland, Gardner & Jackson-Keen Visit Prime Minister""Elton John 'blown away' by Billy Elliot fifth birthday" (video with John's interview and fragments of Holland's performance)"First News interviews Arrietty's Tom Holland"“33rd Critics' Circle Film Awards winners”“National Board of Review Current Awards”Bản gốc"Ron Howard Whaling Tale 'In The Heart Of The Sea' Casts Tom Holland"“'Spider-Man' Finds Tom Holland to Star as New Web-Slinger”lưu trữ“Captain America: Civil War (2016)”“Film Review: ‘Captain America: Civil War’”lưu trữ“‘Captain America: Civil War’ review: Choose your own avenger”lưu trữ“The Lost City of Z reviews”“Sony Pictures and Marvel Studios Find Their 'Spider-Man' Star and Director”“‘Mary Magdalene’, ‘Current War’ & ‘Wind River’ Get 2017 Release Dates From Weinstein”“Lionsgate Unleashing Daisy Ridley & Tom Holland Starrer ‘Chaos Walking’ In Cannes”“PTA's 'Master' Leads Chicago Film Critics Nominations, UPDATED: Houston and Indiana Critics Nominations”“Nominaciones Goya 2013 Telecinco Cinema – ENG”“Jameson Empire Film Awards: Martin Freeman wins best actor for performance in The Hobbit”“34th Annual Young Artist Awards”Bản gốc“Teen Choice Awards 2016—Captain America: Civil War Leads Second Wave of Nominations”“BAFTA Film Award Nominations: ‘La La Land’ Leads Race”“Saturn Awards Nominations 2017: 'Rogue One,' 'Walking Dead' Lead”Tom HollandTom HollandTom HollandTom Hollandmedia.gettyimages.comWorldCat Identities300279794no20130442900000 0004 0355 42791085670554170004732cb16706349t(data)XX5557367