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!
$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.
code-golf number-theory
$endgroup$
add a comment |
$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.
code-golf number-theory
$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 be5 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 row6 1 6 1 6 1
, then you wouldn't be able to form the square, since there aren't multiple rows that start with6
nor1
. 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
add a comment |
$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.
code-golf number-theory
$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
code-golf number-theory
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 be5 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 row6 1 6 1 6 1
, then you wouldn't be able to form the square, since there aren't multiple rows that start with6
nor1
. 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
add a comment |
$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 be5 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 row6 1 6 1 6 1
, then you wouldn't be able to form the square, since there aren't multiple rows that start with6
nor1
. 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
add a comment |
6 Answers
6
active
oldest
votes
$begingroup$
Wolfram Language (Mathematica), 46 bytes
Mod[r=Range[#-1];#^r&/@(PrimitiveRoot@#^r),#]&
Try it online!
$endgroup$
add a comment |
$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.
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 67 bytes
(s=Thread@Mod[x^#&/@(x=Range[#-1]),#])[[#&@@Select[s,Sort@#==x&]]]&
Try it online!
$endgroup$
add a comment |
$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!
$endgroup$
$begingroup$
.indexOf(1)>p-3
saves 3 bytes over.every
.
$endgroup$
– Neil
2 hours ago
add a comment |
$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
$endgroup$
add a comment |
$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ƑƇḢçṖ
$endgroup$
add a comment |
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
);
);
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%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
$begingroup$
Wolfram Language (Mathematica), 46 bytes
Mod[r=Range[#-1];#^r&/@(PrimitiveRoot@#^r),#]&
Try it online!
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 46 bytes
Mod[r=Range[#-1];#^r&/@(PrimitiveRoot@#^r),#]&
Try it online!
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 46 bytes
Mod[r=Range[#-1];#^r&/@(PrimitiveRoot@#^r),#]&
Try it online!
$endgroup$
Wolfram Language (Mathematica), 46 bytes
Mod[r=Range[#-1];#^r&/@(PrimitiveRoot@#^r),#]&
Try it online!
answered 2 hours ago
attinatattinat
78917
78917
add a comment |
add a comment |
$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.
$endgroup$
add a comment |
$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.
$endgroup$
add a comment |
$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.
$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.
answered 2 hours ago
NeilNeil
83.6k745179
83.6k745179
add a comment |
add a comment |
$begingroup$
Wolfram Language (Mathematica), 67 bytes
(s=Thread@Mod[x^#&/@(x=Range[#-1]),#])[[#&@@Select[s,Sort@#==x&]]]&
Try it online!
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 67 bytes
(s=Thread@Mod[x^#&/@(x=Range[#-1]),#])[[#&@@Select[s,Sort@#==x&]]]&
Try it online!
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 67 bytes
(s=Thread@Mod[x^#&/@(x=Range[#-1]),#])[[#&@@Select[s,Sort@#==x&]]]&
Try it online!
$endgroup$
Wolfram Language (Mathematica), 67 bytes
(s=Thread@Mod[x^#&/@(x=Range[#-1]),#])[[#&@@Select[s,Sort@#==x&]]]&
Try it online!
edited 3 hours ago
answered 4 hours ago
J42161217J42161217
14.9k21457
14.9k21457
add a comment |
add a comment |
$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!
$endgroup$
$begingroup$
.indexOf(1)>p-3
saves 3 bytes over.every
.
$endgroup$
– Neil
2 hours ago
add a comment |
$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!
$endgroup$
$begingroup$
.indexOf(1)>p-3
saves 3 bytes over.every
.
$endgroup$
– Neil
2 hours ago
add a comment |
$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!
$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!
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
add a comment |
$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
add a comment |
$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
$endgroup$
add a comment |
$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
$endgroup$
add a comment |
$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
$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
edited 1 hour ago
answered 3 hours ago
Jo KingJo King
27.9k366134
27.9k366134
add a comment |
add a comment |
$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ƑƇḢçṖ
$endgroup$
add a comment |
$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ƑƇḢçṖ
$endgroup$
add a comment |
$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ƑƇḢçṖ
$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ƑƇḢçṖ
answered 12 mins ago
Jonathan AllanJonathan Allan
55.4k537177
55.4k537177
add a comment |
add a comment |
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).
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%2fcodegolf.stackexchange.com%2fquestions%2f185250%2fmagical-modulo-squares%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
$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 with6
nor1
. 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