Calculate the characters from a combination
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
add a comment |
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
1
shouldba
be $27$ if you are starting from $1$ and includingaa
?
– gt6989b
Dec 10 '18 at 14:03
@gt6989b Oh, I missed one character when counting. (Missed to write it). Sorry.
– Edenia
Dec 10 '18 at 14:05
add a comment |
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
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
asked Dec 10 '18 at 14:00
Edenia
1085
1085
1
shouldba
be $27$ if you are starting from $1$ and includingaa
?
– gt6989b
Dec 10 '18 at 14:03
@gt6989b Oh, I missed one character when counting. (Missed to write it). Sorry.
– Edenia
Dec 10 '18 at 14:05
add a comment |
1
shouldba
be $27$ if you are starting from $1$ and includingaa
?
– gt6989b
Dec 10 '18 at 14:03
@gt6989b Oh, I missed one character when counting. (Missed to write it). Sorry.
– Edenia
Dec 10 '18 at 14:05
1
1
should
ba
be $27$ if you are starting from $1$ and including aa
?– gt6989b
Dec 10 '18 at 14:03
should
ba
be $27$ if you are starting from $1$ and including aa
?– gt6989b
Dec 10 '18 at 14:03
@gt6989b Oh, I missed one character when counting. (Missed to write it). Sorry.
– Edenia
Dec 10 '18 at 14:05
@gt6989b Oh, I missed one character when counting. (Missed to write it). Sorry.
– Edenia
Dec 10 '18 at 14:05
add a comment |
2 Answers
2
active
oldest
votes
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
1
Haha, I am implementing algorithms in C, bt dubs.
– Edenia
Dec 10 '18 at 14:17
@Edenia see updates
– gt6989b
Dec 10 '18 at 14:20
HmmA[pos % 26]
doesn't return the correct first character.Afirst[(pos / sz) - 1]
andAsecond[pos % sz]
works though
– Edenia
Dec 10 '18 at 14:32
sz being the 26.
– Edenia
Dec 10 '18 at 14:38
@gt6989b I think you wrote the reverse: the first letter is given by $A[lfloor k/26rfloor]$
– Shubham Johri
Dec 10 '18 at 14:46
|
show 7 more comments
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.
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.
– Edenia
Dec 10 '18 at 14:12
It's even nicer with 0-based numbering (and doesn't require the unusual interpretation of $26 bmod 26$ as 26).
– 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
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
1
Haha, I am implementing algorithms in C, bt dubs.
– Edenia
Dec 10 '18 at 14:17
@Edenia see updates
– gt6989b
Dec 10 '18 at 14:20
HmmA[pos % 26]
doesn't return the correct first character.Afirst[(pos / sz) - 1]
andAsecond[pos % sz]
works though
– Edenia
Dec 10 '18 at 14:32
sz being the 26.
– Edenia
Dec 10 '18 at 14:38
@gt6989b I think you wrote the reverse: the first letter is given by $A[lfloor k/26rfloor]$
– Shubham Johri
Dec 10 '18 at 14:46
|
show 7 more comments
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
1
Haha, I am implementing algorithms in C, bt dubs.
– Edenia
Dec 10 '18 at 14:17
@Edenia see updates
– gt6989b
Dec 10 '18 at 14:20
HmmA[pos % 26]
doesn't return the correct first character.Afirst[(pos / sz) - 1]
andAsecond[pos % sz]
works though
– Edenia
Dec 10 '18 at 14:32
sz being the 26.
– Edenia
Dec 10 '18 at 14:38
@gt6989b I think you wrote the reverse: the first letter is given by $A[lfloor k/26rfloor]$
– Shubham Johri
Dec 10 '18 at 14:46
|
show 7 more comments
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
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
gt6989b
33k22452
33k22452
1
Haha, I am implementing algorithms in C, bt dubs.
– Edenia
Dec 10 '18 at 14:17
@Edenia see updates
– gt6989b
Dec 10 '18 at 14:20
HmmA[pos % 26]
doesn't return the correct first character.Afirst[(pos / sz) - 1]
andAsecond[pos % sz]
works though
– Edenia
Dec 10 '18 at 14:32
sz being the 26.
– Edenia
Dec 10 '18 at 14:38
@gt6989b I think you wrote the reverse: the first letter is given by $A[lfloor k/26rfloor]$
– Shubham Johri
Dec 10 '18 at 14:46
|
show 7 more comments
1
Haha, I am implementing algorithms in C, bt dubs.
– Edenia
Dec 10 '18 at 14:17
@Edenia see updates
– gt6989b
Dec 10 '18 at 14:20
HmmA[pos % 26]
doesn't return the correct first character.Afirst[(pos / sz) - 1]
andAsecond[pos % sz]
works though
– Edenia
Dec 10 '18 at 14:32
sz being the 26.
– Edenia
Dec 10 '18 at 14:38
@gt6989b I think you wrote the reverse: the first letter is given by $A[lfloor k/26rfloor]$
– Shubham Johri
Dec 10 '18 at 14:46
1
1
Haha, I am implementing algorithms in C, bt dubs.
– Edenia
Dec 10 '18 at 14:17
Haha, I am implementing algorithms in C, bt dubs.
– Edenia
Dec 10 '18 at 14:17
@Edenia see updates
– gt6989b
Dec 10 '18 at 14:20
@Edenia see updates
– gt6989b
Dec 10 '18 at 14:20
Hmm
A[pos % 26]
doesn't return the correct first character. Afirst[(pos / sz) - 1]
and Asecond[pos % sz]
works though– Edenia
Dec 10 '18 at 14:32
Hmm
A[pos % 26]
doesn't return the correct first character. Afirst[(pos / sz) - 1]
and Asecond[pos % sz]
works though– Edenia
Dec 10 '18 at 14:32
sz being the 26.
– Edenia
Dec 10 '18 at 14:38
sz being the 26.
– Edenia
Dec 10 '18 at 14:38
@gt6989b I think you wrote the reverse: the first letter is given by $A[lfloor k/26rfloor]$
– Shubham Johri
Dec 10 '18 at 14:46
@gt6989b I think you wrote the reverse: the first letter is given by $A[lfloor k/26rfloor]$
– Shubham Johri
Dec 10 '18 at 14:46
|
show 7 more comments
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.
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.
– Edenia
Dec 10 '18 at 14:12
It's even nicer with 0-based numbering (and doesn't require the unusual interpretation of $26 bmod 26$ as 26).
– LSpice
Dec 10 '18 at 14:13
add a comment |
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.
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.
– Edenia
Dec 10 '18 at 14:12
It's even nicer with 0-based numbering (and doesn't require the unusual interpretation of $26 bmod 26$ as 26).
– LSpice
Dec 10 '18 at 14:13
add a comment |
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.
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 Johri
3,943716
3,943716
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.
– Edenia
Dec 10 '18 at 14:12
It's even nicer with 0-based numbering (and doesn't require the unusual interpretation of $26 bmod 26$ as 26).
– LSpice
Dec 10 '18 at 14:13
add a comment |
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.
– Edenia
Dec 10 '18 at 14:12
It's even nicer with 0-based numbering (and doesn't require the unusual interpretation of $26 bmod 26$ as 26).
– LSpice
Dec 10 '18 at 14:13
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.
– Edenia
Dec 10 '18 at 14:12
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.
– Edenia
Dec 10 '18 at 14:12
It's even nicer with 0-based numbering (and doesn't require the unusual interpretation of $26 bmod 26$ as 26).
– LSpice
Dec 10 '18 at 14:13
It's even nicer with 0-based numbering (and doesn't require the unusual interpretation of $26 bmod 26$ as 26).
– 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
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
should
ba
be $27$ if you are starting from $1$ and includingaa
?– gt6989b
Dec 10 '18 at 14:03
@gt6989b Oh, I missed one character when counting. (Missed to write it). Sorry.
– Edenia
Dec 10 '18 at 14:05