Calculate the characters from a combination
$begingroup$
Given a table with position - 2 character combination pairs like this:
1. aa
2. ab
3. ac
4. ad
...
27. ba
28. bb
29. bc
30. bd
Assuming there are unique combinations with two characters from the alphabet following the same pattern.
How do I calculate what is the first and second character of the combination, if I know the position. That is, how do I find position 28 holds bb
?
EDIT:
The answer that also describes how to do the reverse, i.e find the position of a combination based on the pair is going to be the most useful answer.
arithmetic index-notation
$endgroup$
add a comment |
$begingroup$
Given a table with position - 2 character combination pairs like this:
1. aa
2. ab
3. ac
4. ad
...
27. ba
28. bb
29. bc
30. bd
Assuming there are unique combinations with two characters from the alphabet following the same pattern.
How do I calculate what is the first and second character of the combination, if I know the position. That is, how do I find position 28 holds bb
?
EDIT:
The answer that also describes how to do the reverse, i.e find the position of a combination based on the pair is going to be the most useful answer.
arithmetic index-notation
$endgroup$
1
$begingroup$
shouldba
be $27$ if you are starting from $1$ and includingaa
?
$endgroup$
– gt6989b
Dec 10 '18 at 14:03
$begingroup$
@gt6989b Oh, I missed one character when counting. (Missed to write it). Sorry.
$endgroup$
– Edenia
Dec 10 '18 at 14:05
add a comment |
$begingroup$
Given a table with position - 2 character combination pairs like this:
1. aa
2. ab
3. ac
4. ad
...
27. ba
28. bb
29. bc
30. bd
Assuming there are unique combinations with two characters from the alphabet following the same pattern.
How do I calculate what is the first and second character of the combination, if I know the position. That is, how do I find position 28 holds bb
?
EDIT:
The answer that also describes how to do the reverse, i.e find the position of a combination based on the pair is going to be the most useful answer.
arithmetic index-notation
$endgroup$
Given a table with position - 2 character combination pairs like this:
1. aa
2. ab
3. ac
4. ad
...
27. ba
28. bb
29. bc
30. bd
Assuming there are unique combinations with two characters from the alphabet following the same pattern.
How do I calculate what is the first and second character of the combination, if I know the position. That is, how do I find position 28 holds bb
?
EDIT:
The answer that also describes how to do the reverse, i.e find the position of a combination based on the pair is going to be the most useful answer.
arithmetic index-notation
arithmetic index-notation
edited Dec 11 '18 at 0:10
Edenia
asked Dec 10 '18 at 14:00
EdeniaEdenia
1085
1085
1
$begingroup$
shouldba
be $27$ if you are starting from $1$ and includingaa
?
$endgroup$
– gt6989b
Dec 10 '18 at 14:03
$begingroup$
@gt6989b Oh, I missed one character when counting. (Missed to write it). Sorry.
$endgroup$
– Edenia
Dec 10 '18 at 14:05
add a comment |
1
$begingroup$
shouldba
be $27$ if you are starting from $1$ and includingaa
?
$endgroup$
– gt6989b
Dec 10 '18 at 14:03
$begingroup$
@gt6989b Oh, I missed one character when counting. (Missed to write it). Sorry.
$endgroup$
– Edenia
Dec 10 '18 at 14:05
1
1
$begingroup$
should
ba
be $27$ if you are starting from $1$ and including aa
?$endgroup$
– gt6989b
Dec 10 '18 at 14:03
$begingroup$
should
ba
be $27$ if you are starting from $1$ and including aa
?$endgroup$
– gt6989b
Dec 10 '18 at 14:03
$begingroup$
@gt6989b Oh, I missed one character when counting. (Missed to write it). Sorry.
$endgroup$
– Edenia
Dec 10 '18 at 14:05
$begingroup$
@gt6989b Oh, I missed one character when counting. (Missed to write it). Sorry.
$endgroup$
– Edenia
Dec 10 '18 at 14:05
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
Assuming you have an alphabet A = ['a','b',...'z']
and a permutation list L = ['aa', 'ab', ..., 'az', 'ba', ... , 'zz']
, with the first index starting from 1. (BTW the arithmetic would be much cleaner if you start indexing from $0$, like in the C language, for example.)
Then, the permutations come in groups of $26$, so if you are given an index $k$, then
- the second letter is given by the offset of $k$ in the current 26-long block, so it is $A[(k-1)pmod{26} + 1]$ and
- the first letter is given by $Aleft[lfloor(k-1)/26rfloor+1right]$.
If you were to number starting with $0$, the formulae become
- the second letter is given by $A[k pmod{26}]$ and
- the first letter is given by $Aleft[lfloor k/26rfloorright]$.
Another free update you get with C, for example, is that if you declare int k, d = 26;
then k/d
computes $lfloor k/d rfloor$ automatically, no extra flooring is needed.
UPDATE
The inversion is simple. If first letter has index $F$ and last has index $L$, the offset is given by
$26F + L$ for $0$-based indexing (like C)
$26(F+1) + (L+1) = 26F + L + 27$ for $1$-based indexing
$endgroup$
1
$begingroup$
Haha, I am implementing algorithms in C, bt dubs.
$endgroup$
– Edenia
Dec 10 '18 at 14:17
$begingroup$
@Edenia see updates
$endgroup$
– gt6989b
Dec 10 '18 at 14:20
$begingroup$
HmmA[pos % 26]
doesn't return the correct first character.Afirst[(pos / sz) - 1]
andAsecond[pos % sz]
works though
$endgroup$
– Edenia
Dec 10 '18 at 14:32
$begingroup$
sz being the 26.
$endgroup$
– Edenia
Dec 10 '18 at 14:38
$begingroup$
@gt6989b I think you wrote the reverse: the first letter is given by $A[lfloor k/26rfloor]$
$endgroup$
– Shubham Johri
Dec 10 '18 at 14:46
|
show 7 more comments
$begingroup$
For the index $n$, the $(lfloor frac{n-1}{26}rfloor+1)^{th}$ letter of the alphabet is the first character, $(nmod26)^{th}$ letter of the alphabet is the second character.
$endgroup$
$begingroup$
so for the second, index's reminder of the length of the alphabet. Got it. Really useful, thank you. What is this called though? Didn't know what to search for.
$endgroup$
– Edenia
Dec 10 '18 at 14:12
$begingroup$
It's even nicer with 0-based numbering (and doesn't require the unusual interpretation of $26 bmod 26$ as 26).
$endgroup$
– LSpice
Dec 10 '18 at 14:13
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "69"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
},
noCode: 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%2fmath.stackexchange.com%2fquestions%2f3033946%2fcalculate-the-characters-from-a-combination%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Assuming you have an alphabet A = ['a','b',...'z']
and a permutation list L = ['aa', 'ab', ..., 'az', 'ba', ... , 'zz']
, with the first index starting from 1. (BTW the arithmetic would be much cleaner if you start indexing from $0$, like in the C language, for example.)
Then, the permutations come in groups of $26$, so if you are given an index $k$, then
- the second letter is given by the offset of $k$ in the current 26-long block, so it is $A[(k-1)pmod{26} + 1]$ and
- the first letter is given by $Aleft[lfloor(k-1)/26rfloor+1right]$.
If you were to number starting with $0$, the formulae become
- the second letter is given by $A[k pmod{26}]$ and
- the first letter is given by $Aleft[lfloor k/26rfloorright]$.
Another free update you get with C, for example, is that if you declare int k, d = 26;
then k/d
computes $lfloor k/d rfloor$ automatically, no extra flooring is needed.
UPDATE
The inversion is simple. If first letter has index $F$ and last has index $L$, the offset is given by
$26F + L$ for $0$-based indexing (like C)
$26(F+1) + (L+1) = 26F + L + 27$ for $1$-based indexing
$endgroup$
1
$begingroup$
Haha, I am implementing algorithms in C, bt dubs.
$endgroup$
– Edenia
Dec 10 '18 at 14:17
$begingroup$
@Edenia see updates
$endgroup$
– gt6989b
Dec 10 '18 at 14:20
$begingroup$
HmmA[pos % 26]
doesn't return the correct first character.Afirst[(pos / sz) - 1]
andAsecond[pos % sz]
works though
$endgroup$
– Edenia
Dec 10 '18 at 14:32
$begingroup$
sz being the 26.
$endgroup$
– Edenia
Dec 10 '18 at 14:38
$begingroup$
@gt6989b I think you wrote the reverse: the first letter is given by $A[lfloor k/26rfloor]$
$endgroup$
– Shubham Johri
Dec 10 '18 at 14:46
|
show 7 more comments
$begingroup$
Assuming you have an alphabet A = ['a','b',...'z']
and a permutation list L = ['aa', 'ab', ..., 'az', 'ba', ... , 'zz']
, with the first index starting from 1. (BTW the arithmetic would be much cleaner if you start indexing from $0$, like in the C language, for example.)
Then, the permutations come in groups of $26$, so if you are given an index $k$, then
- the second letter is given by the offset of $k$ in the current 26-long block, so it is $A[(k-1)pmod{26} + 1]$ and
- the first letter is given by $Aleft[lfloor(k-1)/26rfloor+1right]$.
If you were to number starting with $0$, the formulae become
- the second letter is given by $A[k pmod{26}]$ and
- the first letter is given by $Aleft[lfloor k/26rfloorright]$.
Another free update you get with C, for example, is that if you declare int k, d = 26;
then k/d
computes $lfloor k/d rfloor$ automatically, no extra flooring is needed.
UPDATE
The inversion is simple. If first letter has index $F$ and last has index $L$, the offset is given by
$26F + L$ for $0$-based indexing (like C)
$26(F+1) + (L+1) = 26F + L + 27$ for $1$-based indexing
$endgroup$
1
$begingroup$
Haha, I am implementing algorithms in C, bt dubs.
$endgroup$
– Edenia
Dec 10 '18 at 14:17
$begingroup$
@Edenia see updates
$endgroup$
– gt6989b
Dec 10 '18 at 14:20
$begingroup$
HmmA[pos % 26]
doesn't return the correct first character.Afirst[(pos / sz) - 1]
andAsecond[pos % sz]
works though
$endgroup$
– Edenia
Dec 10 '18 at 14:32
$begingroup$
sz being the 26.
$endgroup$
– Edenia
Dec 10 '18 at 14:38
$begingroup$
@gt6989b I think you wrote the reverse: the first letter is given by $A[lfloor k/26rfloor]$
$endgroup$
– Shubham Johri
Dec 10 '18 at 14:46
|
show 7 more comments
$begingroup$
Assuming you have an alphabet A = ['a','b',...'z']
and a permutation list L = ['aa', 'ab', ..., 'az', 'ba', ... , 'zz']
, with the first index starting from 1. (BTW the arithmetic would be much cleaner if you start indexing from $0$, like in the C language, for example.)
Then, the permutations come in groups of $26$, so if you are given an index $k$, then
- the second letter is given by the offset of $k$ in the current 26-long block, so it is $A[(k-1)pmod{26} + 1]$ and
- the first letter is given by $Aleft[lfloor(k-1)/26rfloor+1right]$.
If you were to number starting with $0$, the formulae become
- the second letter is given by $A[k pmod{26}]$ and
- the first letter is given by $Aleft[lfloor k/26rfloorright]$.
Another free update you get with C, for example, is that if you declare int k, d = 26;
then k/d
computes $lfloor k/d rfloor$ automatically, no extra flooring is needed.
UPDATE
The inversion is simple. If first letter has index $F$ and last has index $L$, the offset is given by
$26F + L$ for $0$-based indexing (like C)
$26(F+1) + (L+1) = 26F + L + 27$ for $1$-based indexing
$endgroup$
Assuming you have an alphabet A = ['a','b',...'z']
and a permutation list L = ['aa', 'ab', ..., 'az', 'ba', ... , 'zz']
, with the first index starting from 1. (BTW the arithmetic would be much cleaner if you start indexing from $0$, like in the C language, for example.)
Then, the permutations come in groups of $26$, so if you are given an index $k$, then
- the second letter is given by the offset of $k$ in the current 26-long block, so it is $A[(k-1)pmod{26} + 1]$ and
- the first letter is given by $Aleft[lfloor(k-1)/26rfloor+1right]$.
If you were to number starting with $0$, the formulae become
- the second letter is given by $A[k pmod{26}]$ and
- the first letter is given by $Aleft[lfloor k/26rfloorright]$.
Another free update you get with C, for example, is that if you declare int k, d = 26;
then k/d
computes $lfloor k/d rfloor$ automatically, no extra flooring is needed.
UPDATE
The inversion is simple. If first letter has index $F$ and last has index $L$, the offset is given by
$26F + L$ for $0$-based indexing (like C)
$26(F+1) + (L+1) = 26F + L + 27$ for $1$-based indexing
edited Dec 11 '18 at 4:13
answered Dec 10 '18 at 14:16
gt6989bgt6989b
34.2k22455
34.2k22455
1
$begingroup$
Haha, I am implementing algorithms in C, bt dubs.
$endgroup$
– Edenia
Dec 10 '18 at 14:17
$begingroup$
@Edenia see updates
$endgroup$
– gt6989b
Dec 10 '18 at 14:20
$begingroup$
HmmA[pos % 26]
doesn't return the correct first character.Afirst[(pos / sz) - 1]
andAsecond[pos % sz]
works though
$endgroup$
– Edenia
Dec 10 '18 at 14:32
$begingroup$
sz being the 26.
$endgroup$
– Edenia
Dec 10 '18 at 14:38
$begingroup$
@gt6989b I think you wrote the reverse: the first letter is given by $A[lfloor k/26rfloor]$
$endgroup$
– Shubham Johri
Dec 10 '18 at 14:46
|
show 7 more comments
1
$begingroup$
Haha, I am implementing algorithms in C, bt dubs.
$endgroup$
– Edenia
Dec 10 '18 at 14:17
$begingroup$
@Edenia see updates
$endgroup$
– gt6989b
Dec 10 '18 at 14:20
$begingroup$
HmmA[pos % 26]
doesn't return the correct first character.Afirst[(pos / sz) - 1]
andAsecond[pos % sz]
works though
$endgroup$
– Edenia
Dec 10 '18 at 14:32
$begingroup$
sz being the 26.
$endgroup$
– Edenia
Dec 10 '18 at 14:38
$begingroup$
@gt6989b I think you wrote the reverse: the first letter is given by $A[lfloor k/26rfloor]$
$endgroup$
– Shubham Johri
Dec 10 '18 at 14:46
1
1
$begingroup$
Haha, I am implementing algorithms in C, bt dubs.
$endgroup$
– Edenia
Dec 10 '18 at 14:17
$begingroup$
Haha, I am implementing algorithms in C, bt dubs.
$endgroup$
– Edenia
Dec 10 '18 at 14:17
$begingroup$
@Edenia see updates
$endgroup$
– gt6989b
Dec 10 '18 at 14:20
$begingroup$
@Edenia see updates
$endgroup$
– gt6989b
Dec 10 '18 at 14:20
$begingroup$
Hmm
A[pos % 26]
doesn't return the correct first character. Afirst[(pos / sz) - 1]
and Asecond[pos % sz]
works though$endgroup$
– Edenia
Dec 10 '18 at 14:32
$begingroup$
Hmm
A[pos % 26]
doesn't return the correct first character. Afirst[(pos / sz) - 1]
and Asecond[pos % sz]
works though$endgroup$
– Edenia
Dec 10 '18 at 14:32
$begingroup$
sz being the 26.
$endgroup$
– Edenia
Dec 10 '18 at 14:38
$begingroup$
sz being the 26.
$endgroup$
– Edenia
Dec 10 '18 at 14:38
$begingroup$
@gt6989b I think you wrote the reverse: the first letter is given by $A[lfloor k/26rfloor]$
$endgroup$
– Shubham Johri
Dec 10 '18 at 14:46
$begingroup$
@gt6989b I think you wrote the reverse: the first letter is given by $A[lfloor k/26rfloor]$
$endgroup$
– Shubham Johri
Dec 10 '18 at 14:46
|
show 7 more comments
$begingroup$
For the index $n$, the $(lfloor frac{n-1}{26}rfloor+1)^{th}$ letter of the alphabet is the first character, $(nmod26)^{th}$ letter of the alphabet is the second character.
$endgroup$
$begingroup$
so for the second, index's reminder of the length of the alphabet. Got it. Really useful, thank you. What is this called though? Didn't know what to search for.
$endgroup$
– Edenia
Dec 10 '18 at 14:12
$begingroup$
It's even nicer with 0-based numbering (and doesn't require the unusual interpretation of $26 bmod 26$ as 26).
$endgroup$
– LSpice
Dec 10 '18 at 14:13
add a comment |
$begingroup$
For the index $n$, the $(lfloor frac{n-1}{26}rfloor+1)^{th}$ letter of the alphabet is the first character, $(nmod26)^{th}$ letter of the alphabet is the second character.
$endgroup$
$begingroup$
so for the second, index's reminder of the length of the alphabet. Got it. Really useful, thank you. What is this called though? Didn't know what to search for.
$endgroup$
– Edenia
Dec 10 '18 at 14:12
$begingroup$
It's even nicer with 0-based numbering (and doesn't require the unusual interpretation of $26 bmod 26$ as 26).
$endgroup$
– LSpice
Dec 10 '18 at 14:13
add a comment |
$begingroup$
For the index $n$, the $(lfloor frac{n-1}{26}rfloor+1)^{th}$ letter of the alphabet is the first character, $(nmod26)^{th}$ letter of the alphabet is the second character.
$endgroup$
For the index $n$, the $(lfloor frac{n-1}{26}rfloor+1)^{th}$ letter of the alphabet is the first character, $(nmod26)^{th}$ letter of the alphabet is the second character.
answered Dec 10 '18 at 14:08
Shubham JohriShubham Johri
5,177717
5,177717
$begingroup$
so for the second, index's reminder of the length of the alphabet. Got it. Really useful, thank you. What is this called though? Didn't know what to search for.
$endgroup$
– Edenia
Dec 10 '18 at 14:12
$begingroup$
It's even nicer with 0-based numbering (and doesn't require the unusual interpretation of $26 bmod 26$ as 26).
$endgroup$
– LSpice
Dec 10 '18 at 14:13
add a comment |
$begingroup$
so for the second, index's reminder of the length of the alphabet. Got it. Really useful, thank you. What is this called though? Didn't know what to search for.
$endgroup$
– Edenia
Dec 10 '18 at 14:12
$begingroup$
It's even nicer with 0-based numbering (and doesn't require the unusual interpretation of $26 bmod 26$ as 26).
$endgroup$
– LSpice
Dec 10 '18 at 14:13
$begingroup$
so for the second, index's reminder of the length of the alphabet. Got it. Really useful, thank you. What is this called though? Didn't know what to search for.
$endgroup$
– Edenia
Dec 10 '18 at 14:12
$begingroup$
so for the second, index's reminder of the length of the alphabet. Got it. Really useful, thank you. What is this called though? Didn't know what to search for.
$endgroup$
– Edenia
Dec 10 '18 at 14:12
$begingroup$
It's even nicer with 0-based numbering (and doesn't require the unusual interpretation of $26 bmod 26$ as 26).
$endgroup$
– LSpice
Dec 10 '18 at 14:13
$begingroup$
It's even nicer with 0-based numbering (and doesn't require the unusual interpretation of $26 bmod 26$ as 26).
$endgroup$
– LSpice
Dec 10 '18 at 14:13
add a comment |
Thanks for contributing an answer to Mathematics Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
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%2fmath.stackexchange.com%2fquestions%2f3033946%2fcalculate-the-characters-from-a-combination%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
1
$begingroup$
should
ba
be $27$ if you are starting from $1$ and includingaa
?$endgroup$
– gt6989b
Dec 10 '18 at 14:03
$begingroup$
@gt6989b Oh, I missed one character when counting. (Missed to write it). Sorry.
$endgroup$
– Edenia
Dec 10 '18 at 14:05