Particular Nim game variant (2 or 3 sticks) - algorithm

You start with a stack of n sticks. Each player removes 2 or 3 sticks each turn. The player who removes the last stick wins. If their is only one stick left, the game is a draw.
I need to determine who will win a game of n stacks in general, expressed as a function of n. However, unless we start the game with 2, 3 or 5 sticks, it is always possible to direct the game so that it ends in a draw. I drew the game tree for 9 sticks and more and it is always possible to prevent a loss by making choices that lead to one stick remaining. How can I write a winning rule for this given problem?

However, unless we start the game with 2, 3 or 5 sticks, it is always possible to direct the game so that it ends in a draw.
I don't think this is true.
For example, suppose we start with 10 sticks. If you remove x sticks, I will always remove 5-x sticks. This will mean that after one turn each there are 5 sticks left, and after two turns each I have won.
The same will apply for any multiple of 5.
Now consider other possible values modulo 5.

Here's the rule:
Losing Position: n = 5k
Draw: n = 5k+1 or n = 5k+4
Winning Position: n = 5k+2 or n = 5k+3
You can observe the pattern by building a table like the one shown below:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 .....
L D W W D L D W W D L D W W D L D
Steps for Building the table:
Observe that you lose if n = 0, mark it as L.
When n = 1, it is draw, mark it as D.
When n = 2, you can only draw 2 sticks. So, your opponent happens to face n=0 which is losing, so you win, mark it as W.
For n = 3, you can take 2 or 3 sticks, so, your opponent can end up at 1 or 0 sticks. 1 is a draw and 0 is loss for him and therefore win for us. So, we will chose win, mark it as W.
For n=4, similarily, opponent can end up at 2 or 1. So, we can draw or lose. We will choose draw, mark it as D.
For n = 5, we can either make our opponent end up at 2 or 3. Both of them are win for him. So, we lose. Mark it as L.
Basically, to determine the state(L, W or D) of a number n, we need to look at states of already computed states n-2 and n-3.
Observe that this pattern (LDWWD) repeats after that.

Related

Assignment regarding, dynamic programming. Making my code more efficient?

I've got an assignment regarding dynamic programming.
I'm to design an efficient algorithm that does the following:
There is a path, covered in spots. The user can move forward to the end of the path using a series of push buttons. There are 3 buttons. One moves you forward 2 spots, one moves you forward 3 spots, one moves you forward 5 spots. The spots on the path are either black or white, and you cannot land on a black spot. The algorithm finds the smallest number of button pushes needed to reach the end (past the last spot, can overshoot it).
The user inputs are for "n", the number of spots. And fill the array with n amount of B or W (Black or white). The first spot must be white. Heres what I have so far (Its only meant to be pseudo):
int x = 0
int totalsteps = 0
n = user input
int countAtIndex[n-1] <- Set all values to -1 // I'll do the nitty gritty stuff like this after
int spots[n-1] = user input
pressButton(totalSteps, x) {
if(countAtIndex[x] != -1 AND totalsteps >= countAtIndex[x]) {
FAILED } //Test to see if the value has already been modified (not -1 or not better)
else
if (spots[x] = "B") {
countAtIndex[x] = -2 // Indicator of invalid spot
FAILED }
else if (x >= n-5) { // Reached within 5 of the end, press 5 so take a step and win
GIVE VALUE OF TOTALSTEPS + 1 A SUCCESSFUL SHORTEST OUTPUT
FINISH }
else
countAtIndex[x] = totalsteps
pressButton(totalsteps + 1, x+5) //take 5 steps
pressButton(totalsteps + 1, x+3) //take 3 steps
pressButton(totalsteps + 1, x+2) //take 2 steps
}
I appreciate this may look quite bad but I hope it comes across okay, I just want to make sure the theory is sound before I write it out better. I'm wondering if this is not the most efficient way of doing this problem. In addition to this, where there are capitals, I'm unsure on how to "Fail" the program, or how to return the "Successful" value.
Any help would be greatly appreciated.
I should add incase its unclear, I'm using countAtIndex[] to store the number of moves to get to that index in the path. I.e at position 3 (countAtIndex[2]) could have a value 1, meaning its taken 1 move to get there.
I'm converting my comment into an answer since this will be too long for a comment.
There are always two ways to solve a dynamic programming problem: top-down with memoization, or bottom-up by systematically filling an output array. My intuition says that the implementation of the bottom-up approach will be simpler. And my intent with this answer is to provide an example of that approach. I'll leave it as an exercise for the reader to write the formal algorithm, and then implement the algorithm.
So, as an example, let's say that the first 11 elements of the input array are:
index: 0 1 2 3 4 5 6 7 8 9 10 ...
spot: W B W B W W W B B W B ...
To solve the problem, we create an output array (aka the DP table), to hold the information we know about the problem. Initially all values in the output array are set to infinity, except for the first element which is set to 0. So the output array looks like this:
index: 0 1 2 3 4 5 6 7 8 9 10 ...
spot: W B W B W W W B B W B
output: 0 - x - x x x - - x -
where - is a black space (not allowed), and x is being used as the symbol for infinity (a spot that's either unreachable, or hasn't been reached yet).
Then we iterate from the beginning of the table, updating entries as we go.
From index 0, we can reach 2 and 5 with one move. We can't move to 3 because that spot is black. So the updated output array looks like this:
index: 0 1 2 3 4 5 6 7 8 9 10 ...
spot: W B W B W W W B B W B
output: 0 - 1 - x 1 x - - x -
Next, we skip index 1 because the spot is black. So we move on to index 2. From 2, we can reach 4,5, and 7. Index 4 hasn't been reached yet, but now can be reached in two moves. The jump from 2 to 5 would reach 5 in two moves. But 5 can already be reached in one move, so we won't change it (this is where the recurrence relation comes in). We can't move to 7 because it's black. So after processing index 2, the output array looks like this:
index: 0 1 2 3 4 5 6 7 8 9 10 ...
spot: W B W B W W W B B W B
output: 0 - 1 - 2 1 x - - x -
After skipping index 3 (black) and processing index 4 (can reach 6 and 9), we have:
index: 0 1 2 3 4 5 6 7 8 9 10 ...
spot: W B W B W W W B B W B
output: 0 - 1 - 2 1 3 - - 3 -
Processing index 5 won't change anything because 7,8,10 are all black. Index 6 doesn't change anything because 8 is black, 9 can already be reached in three moves, and we aren't showing index 11. Indexes 7 and 8 are skipped because they're black. And all jumps from 9 are into parts of the array that aren't shown.
So if the goal was to reach index 11, the number of moves would be 4, and the possible paths would be 2,4,6,11 or 2,4,9,11. Or if the array continued, we would simply keep iterating through the array, and then check the last five elements of the array to see which has the smallest number of moves.

Index of winning player in a game

N players play a game. They stand in a way such that they form a regular N-gon. Players are numbered from 1 to N. The players throw boomerangs in clockwise order, in turns. At first player 1 throws a boomerang through the center of the polygon. If N is even, then the boomerang hits the player on the opposite side, and the player who got hit leaves the game. If N is odd then the opposite point has no player, so the boomerang flies back to the player who threw it and hits him, making him leave the game. After a player leaves, the game continues with N-1 players in the same way (i.e. they again make a regular (N-1)-gon). The player who's clockwisely closest to the last player who moved, has the turn now.
The game continues until only one player is left. Is there any closed form expression for the index of the winning player in terms of N? Or any fast (logarithmic or less) approach to calculate the index?
If f(N) denotes the answer, then I figured out a trivial recursion that f(2n+1)=f(2n)+1 since in the odd case, player 1 is out in the first move and then player 2 moves. But nothing for the even case. Thanks for any help.
First few values: n --> f(n)
2 --> 1
3 --> 2
4 --> 4
5 --> 5
6 --> 1
7 --> 2
8 --> 3
9 --> 4
10 --> 5
11 --> 6
12 --> 8
13 --> 9
14 --> 11
15 --> 12
16 --> 14
The other recursion goes thusly:
if f(2n-1) >= n, f(2n)=f(2n-1)+2 (mod 2n).
Otherwise, f(2n)=f(2n)+1 (mod 2n).
It's easy to see this if you write down the survival times of each index as a permutation.
Applying the recursion to the zero crossings, we find that the n_th zero-crossing is at 4^(n+2)/3
In light of all this, here is the algorithm for finding the winner of the n_th game:
k <- floor((log(3)+log(n))/log(4))+2
m <- 4^(k+2)/3
if n < 2m:
f(n) <- (n-m+1)%n
otherwise:
f(n) <- (n+2-m+(n-2*m)/2)%n
First few values: 1,1,2,4,5,1,2,3,4,5,6,8,9,11,12...
f(100)=15, f(1000)=818, f(10000)=4539, f(100000)=12619, etc, etc

Combinatorial Game. Who wins if both players play optimally

Players A and B play a game optimally and move alternately. They
start with 1. Each player in his turn multiplies the current number
with any integer from [2,9]. If after a player's turn, the number is
more than or equal to n, he wins.
A starts. Given n, who wins?
For example,
Numbers 2,3..,9 are winning numbers (Player A will win)
Numbers 10,11,..,18 are losing numbers (Player A will lose)
Numbers 19,20,..,162 are winning numbers
What would be the winning strategy? How can the Sprague-Grundy theorem be applied to solve this?
According to Sprague-Grundy theorem each state of an impartial game can be assigned a non-negative integer called Grundy number, such that the player who moves in this state will lose iff this number is 0, and win iff this number is non-zero.
If the Grundy numbers for the states are known, then the winning strategy is to always make a move to a state in which Grundy number is 0.
The algorithm for computing Grundy number for some state of a general game is as follows:
if current player can't make a valid move:
Grundy number := 0 (this player has lost)
else:
for each move in this state:
for each sub-game the game splits into after that move:
compute Grundy number of the sub-game
compute XOR of Grundy numbers of the sub-games
Grundy number := MEX of those XORs
MEX is minimum excludant function. MEX of a set of non-negative integers is equal to the smallest non-negative integer, that does not belong to this set.
For example:
MEX(0) = 1
MEX(0, 1) = 2
MEX(0, 2) = 1
MEX(0, 1, 2) = 3
MEX(0, 1, 3) = 2
MEX(1, 2, 3) = 0
MEX(10, 100, 1000) = 0
Naive implementation of this algorithm for this game in Python 3 may look like this:
import functools
from itertools import count
def mex(s):
for i in count():
if i not in s:
return i
#functools.lru_cache(10000)
def sprague_grundy(n, cur=1):
if cur >= n:
return 0
move_results = {sprague_grundy(n, cur*move) for move in range(2, 9+1)}
return mex(move_results)
for i in count(1):
print(sprague_grundy(i))
Often the easiest way to understand the general formula for the Grundy number is to just look at the sequence and try to notice the relationships.
In this game you can figure out the general formula by simply looking at n numbers for games in which player A wins in inital state, without actually calculating Grundy numbers.
But we can still look at the counts of Grundy numbers of the initial state of the game for consecutive n (0 means player A loses in the initial state, 1,2,3,4 mean player A wins):
$ python3 sprague_grundy.py | uniq -c
1 0
1 1
2 2
4 3
1 4
9 0
18 1
36 2
72 3
18 4
162 0
324 1
648 2
1296 3
324 4
2916 0
It is possible to notice that for player A all the losing initial states are for
Or in other words the initial state for player A is losing iff
Basically you make an array A[] where A[i] stores whether number i is a winning position or losing with respect to the player who starts the game.Let it be player A. Basic rule, from a losing position you can go only to a winning one and a winning position is such that there is always a losing position reachable from it.Following code is explanatory ( 1 means winning w.r.t to A and 0 means losing).
for each i from 1 to 9
A[i]=1
for each i from 10 to n
flag=0
A[i]=0
for each j from 2 to 9
if i is divisible j and A[i/j] is 0
flag=1
if flag is 1
A[i]=1
Now if A[n] is 1 it is winning for him else he loses.
This is an O(n) solution both in time and memory.You can reduce memory, but
time I can't come up with a better solution. There might be a O(1) solution but I am unaware of it.

Strategy with regard to how to approach this algorithm?

I was asked this question in a test and I need help with regards to how I should approach the solution, not the actual answer. The question is
You have been given a 7 digit number(with each digit being distinct and 0-9). The number has this property
product of first 3 digits = product of last 3 digits = product of central 3 digits
Identify the middle digit.
Now, I can do this on paper by brute force(trial and error), the product is 72 and digits being
8,1,9,2,4,3,6
Now how do I approach the problem in a no brute force way?
Let the number is: a b c d e f g
So as per the rule(1):
axbxc = cxdxe = exfxg
more over we have(2):
axb = dxe and
cxd = fxg
This question can be solved with factorization and little bit of hit/trial.
Out of the digits from 1 to 9, 5 and 7 can rejected straight-away since these are prime numbers and would not fit in the above two equations.
The digits 1 to 9 can be factored as:
1 = 1, 2 = 2, 3 = 3, 4 = 2X2, 6 = 2X3, 8 = 2X2X2, 9 = 3X3
After factorization we are now left with total 7 - 2's, 4 - 3's and the number 1.
As for rule 2 we are left with only 4 possibilities, these 4 equations can be computed by factorization logic since we know we have overall 7 2's and 4 3's with us.
1: 1X8(2x2x2) = 2X4(2x2)
2: 1X6(3x2) = 3X2
3: 4(2x2)X3 = 6(3x2)X2
4: 9(3x3)X2 = 6(3x2)X3
Skipping 5 and 7 we are left with 7 digits.
With above equations we have 4 digits with us and are left with remaining 3 digits which can be tested through hit and trial. For example, if we consider the first case we have:
1X8 = 2X4 and are left with 3,6,9.
we have axbxc = cxdxe we can opt c with these 3 options in that case the products would be 24, 48 and 72.
24 cant be correct since for last three digits we are left with are 6,9,4(=216)
48 cant be correct since for last three digits we are left with 3,9,4(=108)
72 could be a valid option since the last three digits in that case would be 3,6,4 (=72)
This question is good to solve with Relational Programming. I think it very clearly lets the programmer see what's going on and how the problem is solved. While it may not be the most efficient way to solve problems, it can still bring desired clarity and handle problems up to a certain size. Consider this small example from Oz:
fun {FindDigits}
D1 = {Digit}
D2 = {Digit}
D3 = {Digit}
D4 = {Digit}
D5 = {Digit}
D6 = {Digit}
D7 = {Digit}
L = [D1 D2 D3] M = [D3 D4 D5] E= [D5 D6 D7] TotL in
TotL = [D1 D2 D3 D4 D5 D6 D7]
{Unique TotL} = true
{ProductList L} = {ProductList M} = {ProductList E}
TotL
end
(Now this would be possible to parameterize furthermore, but non-optimized to illustrate the point).
Here you first pick 7 digits with a function Digit/0. Then you create three lists, L, M and E consisting of the segments, as well as a total list to return (you could also return the concatenation, but I found this better for illustration).
Then comes the point, you specify relations that have to be intact. First, that the TotL is unique (distinct in your tasks wording). Then the next one, that the segment products have to be equal.
What now happens is that a search is conducted for your answers. This is a depth-first search strategy, but could also be breadth-first, and a solver is called to bring out all solutions. The search strategy is found inside the SolveAll/1 function.
{Browse {SolveAll FindDigits}}
Which in turns returns this list of answers:
[[1 8 9 2 4 3 6] [1 8 9 2 4 6 3] [3 6 4 2 9 1 8]
[3 6 4 2 9 8 1] [6 3 4 2 9 1 8] [6 3 4 2 9 8 1]
[8 1 9 2 4 3 6] [8 1 9 2 4 6 3]]
At least this way forward is not using brute force. Essentially you are searching for answers here. There might be heuristics that let you find the correct answer sooner (some mathematical magic, perhaps), or you can use genetic algorithms to search the space or other well-known strategies.
Prime factor of distinct digit (if possible)
0 = 0
1 = 1
2 = 2
3 = 3
4 = 2 x 2
5 = 5
6 = 2 x 3
7 = 7
8 = 2 x 2 x 2
9 = 3 x 3
In total:
7 2's + 4 3's + 1 5's + 1 7's
With the fact that When A=B=C, composition of prime factor of A must be same as composition of prime factor of B and that of C, 0 , 5 and 7 are excluded since they have unique prime factor that can never match with the fact.
Hence, 7 2's + 4 3's are left and we have 7 digit (1,2,3,4,6,8,9). As there are 7 digits only, the number is formed by these digits only.
Recall the fact, A, B and C must have same composition of prime factors. This implies that A, B and C have same number of 2's and 3's in their composition. So, we should try to achieve (in total for A and B and C):
9 OR 12 2's AND
6 3's
(Must be product of 3, lower bound is total number of prime factor of all digits, upper bound is lower bound * 2)
Consider point 2 (as it has one possibility), A has 2 3's and same for B and C. To have more number of prime factor in total, we need to put digit in connection digit between two product (third or fifth digit). Extract digits with prime factor 3 into two groups {3,6} and {9} and put digit into connection digit. The only possible way is to put 9 in connection digit and 3,6 on unconnected product. That mean xx9xx36 or 36xx9xx (order of 3,6 is not important)
With this result, we get 9 x middle x connection digit = connection digit x 3 x 6. Thus, middle = (3 x 6) / 9 = 2
My answer actually extends #Ansh's answer.
Let abcdefg be the digits of the number. Then
ab=de
cd=fg
From these relations we can exclude 0, 5 and 7 because there are no other multipliers of these numbers between 0 and 9. So we are left with seven numbers and each number is included once in each answer. We are going to examine how we can pair the numbers (ab, de, cd, fg).
What happens with 9? It can't be combined with 3 or 6 since then their product will have three times the factor 3 and we have at total 4 factors of 3. Similarly, 3 and 6 must be combined at least one time together in response to the two factors of 9. This gives a product of 18 and so 9 must be combined at least once with 2.
Now if 9x2 is in a corner then 3x6 must be in the middle. Meaning in the other corner there must be another multiplier of 3. So 9 and 2 are in the middle.
Let's suppose ab=3x6 (The other case is symmetric). Then d must be 9 or 2. But if d is 9 then f or g must be multiplier of 3. So d is 2 and e is 9. We can stop here and answer the middle digit is
2
Now we have 2c = fg and the remaining choices are 1, 4, 8. We see that the only solutions are c = 4, f = 1, g = 8 and c = 4, f = 8, g = 1.
So if is 3x6 is in the left corner we have the following solutions:
3642918, 3642981, 6342918, 6342981
If 3x6 is in the right corner we have the following solutions which are the reverse of the above:
8192463, 1892463, 8192436, 1892436
Here is how you can consider the problem:
Let's note the final solution N1 N2 N3 N4 N5 N6 N7 for the 3 numbers N1N2N3, N3N4N5 and N5N6N7
0, 5 and 7 are to exclude because they are prime and no other ciphers is a multiple of them. So if they had divided one of the 3 numbers, no other number could have divided the others.
So we get the 7 remaining ciphers : 1234689
where the product of the ciphers is 2^7*3^4
(N1*N2*N3) and (N5*N6*N7) are equals so their product is a square number. We can then remove, one of the number (N4) from the product of the previous point to find a square number (i.e. even exponents on both numbers)
N4 can't be 1, 3, 4, 6, 9.
We conclude N4 is 2 or 8
If N4 is 8 and it divides (N3*N4*N5), we can't use the remaining even numbers (2, 4, 6) to divides
both (N1*N2*N3) and (N6*N7*N8) by 8. So N4 is 2 and 8 does not belong to the second group (let's put it in N1).
Now, we have: 1st grp: 8XX, 2nd group: X2X 3rd group: XXX
Note: at this point we know that the product is 72 because it is 2^3*3^2 (the square root of 2^6*3^4) but the result is not really important. We have made the difficult part knowing the 7 numbers and the middle position.
Then, we know that we have to distribute 2^3 on (N1*N2*N3), (N3*N4*N5), (N5*N6*N7) because 2^3*2*2^3=2^7
We already gave 8 to N1, 2 to N4 and we place 6 to N6, and 4 to N5 position, resulting in each of the 3 numbers being a multiple of 8.
Now, we have: 1st grp: 8XX, 2nd group: X24 3rd group: 46X
We have the same way of thinking considering the odd number, we distribute 3^2, on each part knowing that we already have a 6 in the last group.
Last group will then get the 3. And first and second ones the 9.
Now, we have: 1st grp: 8X9, 2nd group: 924 3rd group: 463
And, then 1 at N2, which is the remaining position.
This problem is pretty easy if you look at the number 72 more carefully.
We have our number with this form abcdefg
and abc = cde = efg, with those digits 8,1,9,2,4,3,6
So, first, we can conclude that 8,1,9 must be one of the triple, because, there is no way 1 can go with other two numbers to form 72.
We can also conclude that 1 must be in the start/end of the whole number or middle of the triple.
So now we have 819defg or 918defg ...
Using some calculations with the rest of those digits, we can see that only 819defg is possible, because, we need 72/9 = 8,so only 2,4 is valid, while we cannot create 72/8 = 9 from those 2,4,3,6 digits, so -> 81924fg or 81942fg and 819 must be the triple that start or end our number.
So the rest of the job is easy, we need either 72/4 = 18 or 72/2 = 36, now, we can have our answers: 8192436 or 8192463.
7 digits: 8,1,9,2,4,3,6
say XxYxZ = 72
1) pick any two from above 7 digits. say X,Y
2) divide 72 by X and then Y.. you will get the 3rd number i.e Z.
we found XYZ set of 3-digits which gives result 72.
now repeat 1) and 2) with remaining 4 digits.
this time we found ABC which multiplies to 72.
lets say, 7th digit left out is I.
3) divide 72 by I. result R
4) divide R by one of XYZ. check if result is in ABC.
if No, repeat the step 3)
if yes, found the third pair.(assume you divided R by Y and the result is B)
YIB is the third pair.
so... solution will be.
XZYIBAC
You have your 7 numbers - instead of looking at it in groups of 3 divide up the number as such:
AB | C | D | E | FG
Get the value of AB and use it to get the value of C like so: C = ABC/AB
Next you want to do the same thing with the trailing 2 digits to find E using FG. E = EFG/FG
Now that you have C & E you can solve for D
Since CDE = ABC then D = ABC/CE
Remember your formulas - instead of looking at numbers create a formula aka an algorithm that you know will work every time.
ABC = CDE = EFG However, you have to remember that your = signs have to balance. You can see that D = ABC/CE = EFG/CE Once you know that, you can figure out what you need in order to solve the problem.
Made a quick example in a fiddle of the code:
http://jsfiddle.net/4ykxx9ve/1/
var findMidNum = function() {
var num = [8, 1, 9, 2, 4, 3, 6];
var ab = num[0] * num[1];
var fg = num[5] * num[6];
var abc = num[0] * num[1] * num[2];
var cde = num[2] * num[3] * num[4];
var efg = num[4] * num[5] * num[6];
var c = abc/ab;
var e = efg/fg;
var ce = c * e
var d = abc/ce;
console.log(d); //2
}();
You have been given a 7 digit number(with each digit being distinct and 0-9). The number has this property
product of first 3 digits = product of last 3 digits = product of central 3 digits
Identify the middle digit.
Now, I can do this on paper by brute force(trial and error), the product is 72 and digits being
8,1,9,2,4,3,6
Now how do I approach the problem in a no brute force way?
use linq and substring functions
example var item = array.Skip(3).Take(3) in such a way that you have a loop
for(f =0;f<charlen.length;f++){
var xItemSum = charlen[f].Skip(f).Take(f).Sum(f => f.Value);
}
// untested code

Juggling algorithm of string rotation

There are several ways to deal with string rotation.
"Programming Pearls" talks about string rotation in deep, with three linear algorithms.(click here to check it)
The first one is called "Juggling algorithm", which I spent a lot time to study it, but I still can't understand the role that Great Common Divisor plays in it. Can anybody explain it in detail ?
You rotate the elements by moving them in steps of d. This process loops back after a certain number of moves, so that you need to apply m cycles of length l=n/m in total.
l is the first value that solves the equation l.d = 0 (mod n), so that m is precisely gcd(n, d).
Example 1: for n=12, d=3, 3 cycles of length 4:
0 3 6 9
1 4 7 10
2 5 8 11
Example 2: for n=12, d=10, 2 cycles of length 6:
0 10 8 6 4 2
1 11 9 7 5 3
thanks for your explanation! However, it was not very intuitive to me that you can jump to the conclusion
m=gcd(n,d)
, so I just want to share my reasoning here:
Since you want to find the smallest value such that
((n/m)*d)%n == 0, it means u want to find the biggest m that can satisfy this requirement. Starting from m = n, u can try it one by one and find out that when m=gcd(n,d) the equation is fulfilled. This is because: (n/m)*d = (n/m)*((d/m)*m) = n*(d/m). We need to ensure that d/m as well as n/m are both valid integers, and for the maximum such m, it must be the case that m=gcd(n,d).

Resources