How to calculate the palindrome of a given amount of characters?












2














A palindrome is a set of characters which equal the same thing forwards and backwards, for example; abccba



For a set of a given amount of characters (we can use 9 as an example) how would you calculate the amount of palindromes of characters mathematically?



This doesn't mean the words that can be formed but rather characters which are the same at start characters and end characters.



What formula can be used to calculate this for simple lengths like 9 with lowercase letters?










share|cite|improve this question
























  • My apologies; may i ask what I did incorrectly to receive a downvote? I'd love to improve.
    – Oliver K
    Sep 15 '17 at 3:43
















2














A palindrome is a set of characters which equal the same thing forwards and backwards, for example; abccba



For a set of a given amount of characters (we can use 9 as an example) how would you calculate the amount of palindromes of characters mathematically?



This doesn't mean the words that can be formed but rather characters which are the same at start characters and end characters.



What formula can be used to calculate this for simple lengths like 9 with lowercase letters?










share|cite|improve this question
























  • My apologies; may i ask what I did incorrectly to receive a downvote? I'd love to improve.
    – Oliver K
    Sep 15 '17 at 3:43














2












2








2


1





A palindrome is a set of characters which equal the same thing forwards and backwards, for example; abccba



For a set of a given amount of characters (we can use 9 as an example) how would you calculate the amount of palindromes of characters mathematically?



This doesn't mean the words that can be formed but rather characters which are the same at start characters and end characters.



What formula can be used to calculate this for simple lengths like 9 with lowercase letters?










share|cite|improve this question















A palindrome is a set of characters which equal the same thing forwards and backwards, for example; abccba



For a set of a given amount of characters (we can use 9 as an example) how would you calculate the amount of palindromes of characters mathematically?



This doesn't mean the words that can be formed but rather characters which are the same at start characters and end characters.



What formula can be used to calculate this for simple lengths like 9 with lowercase letters?







palindrome






share|cite|improve this question















share|cite|improve this question













share|cite|improve this question




share|cite|improve this question








edited Sep 15 '17 at 3:31







Oliver K

















asked Sep 15 '17 at 3:23









Oliver KOliver K

2151311




2151311












  • My apologies; may i ask what I did incorrectly to receive a downvote? I'd love to improve.
    – Oliver K
    Sep 15 '17 at 3:43


















  • My apologies; may i ask what I did incorrectly to receive a downvote? I'd love to improve.
    – Oliver K
    Sep 15 '17 at 3:43
















My apologies; may i ask what I did incorrectly to receive a downvote? I'd love to improve.
– Oliver K
Sep 15 '17 at 3:43




My apologies; may i ask what I did incorrectly to receive a downvote? I'd love to improve.
– Oliver K
Sep 15 '17 at 3:43










2 Answers
2






active

oldest

votes


















1














It depends (slightly) on whether you want an odd number of digits in your string, or an even number of digits in your string.



Let's say that the "alphabet" contains $N$ different "characters". (These could be the digits 1-9, or the letters A-Z, or any other set of distinguishable symbols.) You want to know how many palindromes of a given length there are.



If the string length is to be even: Say the string length is $2k$. You can choose each of the first $k$ characters to be anything you want; once you've chosen those, the rest of the string is forced on you by the required symmetry. So there are $k$ free choices, each drawn from a set of $N$ characters; that means there are $N^k$ possibilities.



If the string length is to be odd: Say the string length is $2k+1$. You can choose each of the first $k+1$ characters to be anything you want; once you've chosen those, the rest of the string is forced on you by the required symmetry. So there are $k+1$ free choices, each drawn from a set of $N$ characters; that means there are $N^{k+1}$ possibilities.



So, for example, if your alphabet consists of the 26 lowercase letters a-z, and you want a string with 9 characters, then $N=26$ and the string has length $2k+1$ with $k=4$; therefore the number of possible palindromes is $26^5=11,881,376$.






share|cite|improve this answer





















  • What a helpful explanation, thanks so much!
    – Oliver K
    Sep 15 '17 at 3:42



















-1














include



include



include



using namespace std;
int main()
{

string s,s1,s2;
cin>>s;
s1 = s.substr(0,s.length()/2+1);
int x = (s.length()%2 ==0 ) ? s.length()/2 : s.length()/2 + 1;
s2 = s.substr(s.length()/2,x);
string s3 = s2;
for(int i=0;i


for(int i=0;i<s1.length() ; i++)
{
if(s1[i]!=s3[i])
{
s3 = s3.substr(0,i)+ s1[i] +s3.substr(i,s3.length()-i);

if(s1[i+1]!= s3[i+1])
{ cout<<"NA";
return 0;
}
}

}
s2=s3;
for(int i=0;i<s3.length()+1;i++)
{
s2[i]=s3[s3.length()-i-1];
}
cout<<s1<<s2.substr(1,s2.length());
return 0;


}






share|cite|improve this answer





















    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
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2430107%2fhow-to-calculate-the-palindrome-of-a-given-amount-of-characters%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









    1














    It depends (slightly) on whether you want an odd number of digits in your string, or an even number of digits in your string.



    Let's say that the "alphabet" contains $N$ different "characters". (These could be the digits 1-9, or the letters A-Z, or any other set of distinguishable symbols.) You want to know how many palindromes of a given length there are.



    If the string length is to be even: Say the string length is $2k$. You can choose each of the first $k$ characters to be anything you want; once you've chosen those, the rest of the string is forced on you by the required symmetry. So there are $k$ free choices, each drawn from a set of $N$ characters; that means there are $N^k$ possibilities.



    If the string length is to be odd: Say the string length is $2k+1$. You can choose each of the first $k+1$ characters to be anything you want; once you've chosen those, the rest of the string is forced on you by the required symmetry. So there are $k+1$ free choices, each drawn from a set of $N$ characters; that means there are $N^{k+1}$ possibilities.



    So, for example, if your alphabet consists of the 26 lowercase letters a-z, and you want a string with 9 characters, then $N=26$ and the string has length $2k+1$ with $k=4$; therefore the number of possible palindromes is $26^5=11,881,376$.






    share|cite|improve this answer





















    • What a helpful explanation, thanks so much!
      – Oliver K
      Sep 15 '17 at 3:42
















    1














    It depends (slightly) on whether you want an odd number of digits in your string, or an even number of digits in your string.



    Let's say that the "alphabet" contains $N$ different "characters". (These could be the digits 1-9, or the letters A-Z, or any other set of distinguishable symbols.) You want to know how many palindromes of a given length there are.



    If the string length is to be even: Say the string length is $2k$. You can choose each of the first $k$ characters to be anything you want; once you've chosen those, the rest of the string is forced on you by the required symmetry. So there are $k$ free choices, each drawn from a set of $N$ characters; that means there are $N^k$ possibilities.



    If the string length is to be odd: Say the string length is $2k+1$. You can choose each of the first $k+1$ characters to be anything you want; once you've chosen those, the rest of the string is forced on you by the required symmetry. So there are $k+1$ free choices, each drawn from a set of $N$ characters; that means there are $N^{k+1}$ possibilities.



    So, for example, if your alphabet consists of the 26 lowercase letters a-z, and you want a string with 9 characters, then $N=26$ and the string has length $2k+1$ with $k=4$; therefore the number of possible palindromes is $26^5=11,881,376$.






    share|cite|improve this answer





















    • What a helpful explanation, thanks so much!
      – Oliver K
      Sep 15 '17 at 3:42














    1












    1








    1






    It depends (slightly) on whether you want an odd number of digits in your string, or an even number of digits in your string.



    Let's say that the "alphabet" contains $N$ different "characters". (These could be the digits 1-9, or the letters A-Z, or any other set of distinguishable symbols.) You want to know how many palindromes of a given length there are.



    If the string length is to be even: Say the string length is $2k$. You can choose each of the first $k$ characters to be anything you want; once you've chosen those, the rest of the string is forced on you by the required symmetry. So there are $k$ free choices, each drawn from a set of $N$ characters; that means there are $N^k$ possibilities.



    If the string length is to be odd: Say the string length is $2k+1$. You can choose each of the first $k+1$ characters to be anything you want; once you've chosen those, the rest of the string is forced on you by the required symmetry. So there are $k+1$ free choices, each drawn from a set of $N$ characters; that means there are $N^{k+1}$ possibilities.



    So, for example, if your alphabet consists of the 26 lowercase letters a-z, and you want a string with 9 characters, then $N=26$ and the string has length $2k+1$ with $k=4$; therefore the number of possible palindromes is $26^5=11,881,376$.






    share|cite|improve this answer












    It depends (slightly) on whether you want an odd number of digits in your string, or an even number of digits in your string.



    Let's say that the "alphabet" contains $N$ different "characters". (These could be the digits 1-9, or the letters A-Z, or any other set of distinguishable symbols.) You want to know how many palindromes of a given length there are.



    If the string length is to be even: Say the string length is $2k$. You can choose each of the first $k$ characters to be anything you want; once you've chosen those, the rest of the string is forced on you by the required symmetry. So there are $k$ free choices, each drawn from a set of $N$ characters; that means there are $N^k$ possibilities.



    If the string length is to be odd: Say the string length is $2k+1$. You can choose each of the first $k+1$ characters to be anything you want; once you've chosen those, the rest of the string is forced on you by the required symmetry. So there are $k+1$ free choices, each drawn from a set of $N$ characters; that means there are $N^{k+1}$ possibilities.



    So, for example, if your alphabet consists of the 26 lowercase letters a-z, and you want a string with 9 characters, then $N=26$ and the string has length $2k+1$ with $k=4$; therefore the number of possible palindromes is $26^5=11,881,376$.







    share|cite|improve this answer












    share|cite|improve this answer



    share|cite|improve this answer










    answered Sep 15 '17 at 3:30









    mweissmweiss

    17.5k23270




    17.5k23270












    • What a helpful explanation, thanks so much!
      – Oliver K
      Sep 15 '17 at 3:42


















    • What a helpful explanation, thanks so much!
      – Oliver K
      Sep 15 '17 at 3:42
















    What a helpful explanation, thanks so much!
    – Oliver K
    Sep 15 '17 at 3:42




    What a helpful explanation, thanks so much!
    – Oliver K
    Sep 15 '17 at 3:42











    -1














    include



    include



    include



    using namespace std;
    int main()
    {

    string s,s1,s2;
    cin>>s;
    s1 = s.substr(0,s.length()/2+1);
    int x = (s.length()%2 ==0 ) ? s.length()/2 : s.length()/2 + 1;
    s2 = s.substr(s.length()/2,x);
    string s3 = s2;
    for(int i=0;i


    for(int i=0;i<s1.length() ; i++)
    {
    if(s1[i]!=s3[i])
    {
    s3 = s3.substr(0,i)+ s1[i] +s3.substr(i,s3.length()-i);

    if(s1[i+1]!= s3[i+1])
    { cout<<"NA";
    return 0;
    }
    }

    }
    s2=s3;
    for(int i=0;i<s3.length()+1;i++)
    {
    s2[i]=s3[s3.length()-i-1];
    }
    cout<<s1<<s2.substr(1,s2.length());
    return 0;


    }






    share|cite|improve this answer


























      -1














      include



      include



      include



      using namespace std;
      int main()
      {

      string s,s1,s2;
      cin>>s;
      s1 = s.substr(0,s.length()/2+1);
      int x = (s.length()%2 ==0 ) ? s.length()/2 : s.length()/2 + 1;
      s2 = s.substr(s.length()/2,x);
      string s3 = s2;
      for(int i=0;i


      for(int i=0;i<s1.length() ; i++)
      {
      if(s1[i]!=s3[i])
      {
      s3 = s3.substr(0,i)+ s1[i] +s3.substr(i,s3.length()-i);

      if(s1[i+1]!= s3[i+1])
      { cout<<"NA";
      return 0;
      }
      }

      }
      s2=s3;
      for(int i=0;i<s3.length()+1;i++)
      {
      s2[i]=s3[s3.length()-i-1];
      }
      cout<<s1<<s2.substr(1,s2.length());
      return 0;


      }






      share|cite|improve this answer
























        -1












        -1








        -1






        include



        include



        include



        using namespace std;
        int main()
        {

        string s,s1,s2;
        cin>>s;
        s1 = s.substr(0,s.length()/2+1);
        int x = (s.length()%2 ==0 ) ? s.length()/2 : s.length()/2 + 1;
        s2 = s.substr(s.length()/2,x);
        string s3 = s2;
        for(int i=0;i


        for(int i=0;i<s1.length() ; i++)
        {
        if(s1[i]!=s3[i])
        {
        s3 = s3.substr(0,i)+ s1[i] +s3.substr(i,s3.length()-i);

        if(s1[i+1]!= s3[i+1])
        { cout<<"NA";
        return 0;
        }
        }

        }
        s2=s3;
        for(int i=0;i<s3.length()+1;i++)
        {
        s2[i]=s3[s3.length()-i-1];
        }
        cout<<s1<<s2.substr(1,s2.length());
        return 0;


        }






        share|cite|improve this answer












        include



        include



        include



        using namespace std;
        int main()
        {

        string s,s1,s2;
        cin>>s;
        s1 = s.substr(0,s.length()/2+1);
        int x = (s.length()%2 ==0 ) ? s.length()/2 : s.length()/2 + 1;
        s2 = s.substr(s.length()/2,x);
        string s3 = s2;
        for(int i=0;i


        for(int i=0;i<s1.length() ; i++)
        {
        if(s1[i]!=s3[i])
        {
        s3 = s3.substr(0,i)+ s1[i] +s3.substr(i,s3.length()-i);

        if(s1[i+1]!= s3[i+1])
        { cout<<"NA";
        return 0;
        }
        }

        }
        s2=s3;
        for(int i=0;i<s3.length()+1;i++)
        {
        s2[i]=s3[s3.length()-i-1];
        }
        cout<<s1<<s2.substr(1,s2.length());
        return 0;


        }







        share|cite|improve this answer












        share|cite|improve this answer



        share|cite|improve this answer










        answered Dec 5 '18 at 11:16









        sarasara

        1




        1






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2430107%2fhow-to-calculate-the-palindrome-of-a-given-amount-of-characters%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Berounka

            Sphinx de Gizeh

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...