CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    Question How many words are possible with the english alphabet.

    Hi I wanted to write a program to do this just out of boredom and i started to realize that the prog might take a very long time to run.

    So we have the alphabet to start all capitals A-Z.
    I want to write every possible combination of the letters to file. Up to 10 characters in length.

    So the output file would look something like.
    Code:
    A
    B                              // 1 char in length
    C
    ...
    ...
    AA
    AB                           //2 char in length
    AC
    ......
    AAA
    AAB                         //3 char in length and so on up to 
    AAC
    .........
    AAAAAAAAAA
    ABAAAAAAAA
    ACAAAAAAAA
    .....
    ZZZZZZZZZX
    ZZZZZZZZZY                //10 char in length.
    ZZZZZZZZZZ
    I dont know much concerning the math involved to caluculate the total possible combinations and i tried to study up on factorials but im not understanding them very well. So I was wondering if anyone could crunch some numbers and tell me the total # of possible combinations.

    I was thinking it might be 26^1! + 26^2! + 26^3! ...... +26^10!
    But i checked some of the math and 26^4! comes out to
    9.107 x 10^33
    =9107000000000000000000000000000000

    I am thinking my reasoning above is wrong so if someone knows the correct way to calculate the total possibilities i would appreciate some help. Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: How many words are possible with the english alphabet.

    I do not think that factorial is involved here. Consider a similiar problem using numbers: how many three digit integers are there? We could consider this the same as asking the size of the range [0,1000), hence the answer is 1000. In your case it is slightly different, but the count should just be: 26 + 26^2 + 26^3 + ... + 26^9 + 26^10.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: How many words are possible with the english alphabet.

    The factorials don't look correct to me.

    Single letters = 26
    Two letters = 26 for each single letter = 26^2
    Three letters = 26 for each two letter combination = 26^3
    etc
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  4. #4
    Join Date
    Jan 2009
    Posts
    1,689

    Re: How many words are possible with the english alphabet.

    Create a program that can do addition in base 26.

    Code:
    //in base 26
    word++;
    //write word to file

  5. #5
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: How many words are possible with the english alphabet.

    I once read a sci-fi short story where a group of monks, who's destiny it was to write out all the names of God (Every combination of 10 letters or something like that). When the job was completed then the purpose of the universe would be over and it would be dismantled. The monks, being a modern lot, thought that a computer would help. As the last name is printed out, the stars start to wink out...

    So maybe you shouldn't attempt this project
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: How many words are possible with the english alphabet.

    Quote Originally Posted by JohnW@Wessex
    I once read a sci-fi short story where a group of monks, who's destiny it was to write out all the names of God (Every combination of 10 letters or something like that). When the job was completed then the purpose of the universe would be over and it would be dismantled. The monks, being a modern lot, thought that a computer would help. As the last name is printed out, the stars start to wink out...
    That sounds like a rehashing of Lucas' tower of Hanoi story
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    Re: How many words are possible with the english alphabet.

    Okay thanks. I had the factorial idea stuck in my head and could not get away from it.

    26^1 + 26^2 .... + 26^10
    =
    1.468 x 10^14 possible combinations.
    Ill write the prog which i dont think will be a problem and then count the lines in the output file. I still have a feeling it will take a while to run .

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: How many words are possible with the english alphabet.

    Quote Originally Posted by g.eckert
    Ill write the prog which i dont think will be a problem and then count the lines in the output file.
    Note that the integer that you expect to get is not guaranteed to fit into an unsigned long.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: How many words are possible with the english alphabet.

    Assuming each string takes 1 microsecond to create, then your program run should take about 4 years 34 weeks and 1 day
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  10. #10
    Join Date
    May 2002
    Posts
    1,435

    Re: How many words are possible with the english alphabet.

    Do you have 1.6 million gigabytes of free disk space for the file? That's what I have calculated will be required assuming an 8-bit character text file with a new line character for each unique word. And as suggested, it will probably take a while to write the entire file.

  11. #11
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    Re: How many words are possible with the english alphabet.

    haha yea. I realized that. My solution, i havnt tried, was to count how many times an integer maxed out at 2,000,000,000 reset the integer, then increment another variable indicating it rolled over. So it might max out say 650000 times at 2 billion. and have a remainder of 1,234,567. I can just do some multiplication and print the value not really store it. XD

    Reply to #8
    Last edited by g.eckert; March 12th, 2009 at 10:06 AM.

  12. #12
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    Re: How many words are possible with the english alphabet.

    4 years hmm.....
    1.6 million gigs hmm......

    I dont want to try this anymore.

  13. #13
    Join Date
    Mar 2009
    Location
    Bulgaria
    Posts
    63

    Re: How many words are possible with the english alphabet.

    Definitely a "Non Visual C++ Issue" :-D

  14. #14
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    Re: How many words are possible with the english alphabet.

    Code:
    /*
        Words
        By: Gerald Eckert
    
        Prints all possible
        combinations of the alphabet up 5 charcters in length.
    */
    
    
    #include <iostream>
    using namespace std;
    
    //The alphabet
    const char alphabet[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    
    int main()
    {
        //Iterate up to 5 character words.
        for(int i=0;i<5;i++)
        {
            switch(i)
            {
                //For 1 character words.
                case 0: for(int a=0;a<25;a++)
                            cout<<alphabet[a]<<endl;
                        break;
    
                //For 2 character words.
                case 1: for(int a=0; a<26 ;a++)
                            for(int b=0;b<26;b++)
                                cout<<alphabet[a]<<alphabet[b]<<endl;
                        break;
    
                //For 3 character words.
                case 2: for(int a=0;a<26;a++)
                            for(int b=0;b<26;b++)
                                for(int c=0;c<26;c++)
                                    cout<<alphabet[a]<<alphabet[b]<<alphabet[c]<<endl;
                        break;
    
                //For 4 character words.
                case 3: for(int a=0;a<26;a++)
                            for(int b=0;b<26;b++)
                                for(int c=0;c<26;c++)
                                    for(int d=0;d<26;d++)
                                        cout<<alphabet[a]<<alphabet[b]<<alphabet[c]<<alphabet[d]<<endl;
    
                //For 5 character words
                case 4:break;
            }
        }
        return(0);
    }
    I ran the above which implements up to 4 character words. It took about 94 seconds. Im not sure its the best way to do what i want but it makes sense to me. Any comments on making it faster? My algorithm seems to hit every combo i think. The DOS window doesnt hold all the lines so i cant search back to the begining only like 200 lines or something.

  15. #15
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: How many words are possible with the english alphabet.

    If you only want to count, you do not need to actually generate the strings. So, it is just a matter of whether you want to implement a formula, or write a program that goes through the motion of iterating over all possibilities while incrementing a count.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured