Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Generating codes  (Read 6995 times)

0 Members and 1 Guest are viewing this topic.

alex116

  • Guest
Generating codes
« on: November 22, 2006, 04:07:36 PM »
Hi,
I need to generate all combinations of three characters containing both letters and numbers (alphanumerical). Can anyone help me?


eg :
h4g
der
3t7
gh1
etc.

but also:
dd4
f33
rfr
44g
rrr
etc.

So it is not compulsory that the characters don't repeat in the same string. (they can repeat).
I would also want to know the formula for this...(it's not Cnk=n!/k!(n-k)!   thats the combinations formula, in this one, the chars do not repeat)
« Last Edit: November 22, 2006, 04:15:21 PM by alex116 »

Rob Pomeroy



    Prodigy

  • Systems Architect
  • Thanked: 124
    • Me
  • Experience: Expert
  • OS: Other
Re: Generating codes
« Reply #1 on: November 23, 2006, 02:25:17 AM »
Do you want to do this in a programming language?
Only able to visit the forums sporadically, sorry.

Geek & Dummy - honest news, reviews and howtos

Neil



    Expert
  • Fear me Track. Noone can escape my wrath.
  • Thanked: 3
    Re: Generating codes
    « Reply #2 on: November 23, 2006, 07:56:39 AM »
    You could do this using three nested for loops cycling through each charector, and storing the combinations in an array of strings.

    Wouldn't the number of combinations simply be the number of charectors available to the power of the length of the string? (Cubed in this case)
    « Last Edit: November 23, 2006, 07:57:59 AM by Neil »

    alex116

    • Guest
    Re: Generating codes
    « Reply #3 on: November 23, 2006, 11:12:34 AM »
    Quote
    Do you want to do this in a programming language?

    Any method is welcome, but I don't know a thing about programming so it would be nice of you and I would be very grateful if you told me how to do it in detail.

    BTW I found out the mathematical formula for the arrangements its : Ank=n!/(n-k)! so it's much more than n!/k!(n-k)!
    The result for n=36(26 letters and 10 #) and k=3 was : 42,840
    « Last Edit: November 23, 2006, 11:14:11 AM by alex116 »

    Neil



      Expert
    • Fear me Track. Noone can escape my wrath.
    • Thanked: 3
      Re: Generating codes
      « Reply #4 on: November 23, 2006, 11:57:59 AM »
      I write this in Javascript. I can't guarrantee it is free from errors, though.

      var combinations = [];
      for(var a = "a".charCodeAt(0); a <= "z".charCodeAt(0); a++)
         for(var b = "a".charCodeAt(0); b <= "z".charCodeAt(0); b++)
            for(var c = "a".charCodeAt(0); c <= "z".charCodeAt(0); c++)
               combinations[combinations.length] = String.fromCharCode(a) + String.fromCharCode(b) + String.fromCharCode(c);

      for(var a = 0; a <= 9; a++)
         for(var b = 0; b <= 9; b++)
            for(var c = 0; c <= 9; c++)
               combinations[combinations.length] = String(a) + String(b) + String(c);

      alert(combinations);
      alert(combinations.length);


      According to my program, there are 18576 combinations, if you include 0 as a possible number.
      « Last Edit: November 23, 2006, 12:00:27 PM by Neil »

      Rob Pomeroy



        Prodigy

      • Systems Architect
      • Thanked: 124
        • Me
      • Experience: Expert
      • OS: Other
      Re: Generating codes
      « Reply #5 on: November 24, 2006, 01:54:35 AM »
      A short PHP version that is not locale dependent:

      Code: [Select]
      <pre>
      <?php
      $validchars 
      'abcdefghijklmnopqrstuvwxyz0123456789';
      for (
      $i 0$i strlen($validchars); $i++)
        for (
      $j 0$j strlen($validchars); $j++)
          for (
      $k 0$k strlen($validchars); $k++)
            
      $combos[]=$validchars[$i].$validchars[$j].$validchars[$k];
      print_r($combos);
      ?>

      </pre>
      produces:
       Array
      (
          
      • => aaa
      • [1] => aab
            [2] => aac
            [3] => aad
            [4] => aae
            [5] => aaf
            [6] => aag
            [7] => aah
            [8] => aai
            [9] => aaj
            [10] => aak
        ...
            [46645] => 99z
            [46646] => 990
            [46647] => 991
            [46648] => 992
            [46649] => 993
            [46650] => 994
            [46651] => 995
            [46652] => 996
            [46653] => 997
            [46654] => 998
            [46655] => 999
        )[/pre]
        So I make it 46656 combinations.  Since we're talking about all possible permutations of three characters (from a subset of 36), surely the correct count is 36
      3?  (= 46656)  Or have I misunderstood the problem?
      « Last Edit: November 24, 2006, 01:57:18 AM by robpomeroy »
      Only able to visit the forums sporadically, sorry.

      Geek & Dummy - honest news, reviews and howtos

      Neil



        Expert
      • Fear me Track. Noone can escape my wrath.
      • Thanked: 3
        Re: Generating codes
        « Reply #6 on: November 24, 2006, 07:32:10 AM »
        I had a feeling that the number would be the number of possible symbols cubed. I'd like to know what's wrong with my Javascript...

        Edit: My script generates all the possible letter combinations and number combinations seperately, not letters and numbers together. DOH!
        « Last Edit: November 24, 2006, 07:36:23 AM by Neil »

        Rob Pomeroy



          Prodigy

        • Systems Architect
        • Thanked: 124
          • Me
        • Experience: Expert
        • OS: Other
        Re: Generating codes
        « Reply #7 on: November 24, 2006, 10:04:00 AM »
        And my PHP is only six lines long.   :P  In fact if you do it like this, it's only three lines long (I did it the other way for readability):

        Code: [Select]
        $validchars = 'abcdefghijklmnopqrstuvwxyz0123456789';
        for ($i = 0; $i < strlen($validchars); $i++)  for ($j = 0; $j < strlen($validchars); $j++) for ($k = 0; $k < strlen($validchars); $k++) $combos[]=$validchars[$i].$validchars[$j].$validchars[$k];
        print_r($combos);

        ;D ;D
        « Last Edit: November 24, 2006, 10:04:29 AM by robpomeroy »
        Only able to visit the forums sporadically, sorry.

        Geek & Dummy - honest news, reviews and howtos

        Neil



          Expert
        • Fear me Track. Noone can escape my wrath.
        • Thanked: 3
          Re: Generating codes
          « Reply #8 on: November 24, 2006, 11:45:43 AM »
          Nice method Rob ;)

          var validchars = "abcdefghijklmnopqrstuvwxyz0123456789";
          var combinations = [];
          for(var a = 0; a < validchars.length; a++)
             for(var b = 0; b < validchars.length; b++)
                for(var c = 0; c < validchars.length; c++)
                   combinations[combinations.length] = validchars.charAt(a) + validchars.charAt(b) + validchars.charAt(c);

          document.write(combinations.length);
          document.write(combinations);

          This gives the correct value 46656.
          « Last Edit: November 24, 2006, 11:46:47 AM by Neil »

          Rob Pomeroy



            Prodigy

          • Systems Architect
          • Thanked: 124
            • Me
          • Experience: Expert
          • OS: Other
          Re: Generating codes
          « Reply #9 on: November 24, 2006, 02:55:22 PM »
          Quote
          Nice method Rob ;)
          Thanks, buddy!  I still have a feeling in my bones that there's an even more efficient way of doing this...

          I'd be very puzzled to know why the OP thought the correct count was n!/(n-k)!, if he returns...
          Only able to visit the forums sporadically, sorry.

          Geek & Dummy - honest news, reviews and howtos

          Neil



            Expert
          • Fear me Track. Noone can escape my wrath.
          • Thanked: 3
            Re: Generating codes
            « Reply #10 on: November 24, 2006, 06:58:21 PM »
            I think that's if you can only use each letter once per combination. But what do I know - I barely passed Statistics Module Level 1 ;)

            alex116

            • Guest
            Re: Generating codes
            « Reply #11 on: November 26, 2006, 03:06:49 AM »
            Quote
            Quote
            Nice method Rob ;)
            Thanks, buddy!  I still have a feeling in my bones that there's an even more efficient way of doing this...

            I'd be very puzzled to know why the OP thought the correct count was n!/(n-k)!, if he returns...


            As far as I know C336=36!/(3!(36-3)!)= 14280 and thats for the case when the chars don't repeat in the same string. For the case I need, the formula is :
            A336=36!/(36-3)!=42840 (Please correct me if I'm wrong).
            I can't see how you've found that 46....
            Anyway, I'll try both methods and post whether the results were satisfying.

            Thanks a lot.

            alex116

            • Guest
            Re: Generating codes
            « Reply #12 on: November 26, 2006, 03:16:57 AM »
            Quote
            Nice method Rob ;)

            var validchars = "abcdefghijklmnopqrstuvwxyz0123456789";
            var combinations = [];
            for(var a = 0; a < validchars.length; a++)
               for(var b = 0; b < validchars.length; b++)
                  for(var c = 0; c < validchars.length; c++)
                     combinations[combinations.length] = validchars.charAt(a) + validchars.charAt(b) + validchars.charAt(c);

            document.write(combinations.length);
            document.write(combinations);

            This gives the correct value 46656.


            I tried your method and it only works until a10, then it stops for some reason. It also displays the strings separated by comma and I need then one under the other (in a column) and of coursem, I need them all.

            I would very much appreciate if you do it in Pascal or C++ because I'm more familiar with those...I know nothing about php.

            alex116

            • Guest
            Re: Generating codes
            « Reply #13 on: November 26, 2006, 03:25:55 AM »
            Quote
            And my PHP is only six lines long.   :P  In fact if you do it like this, it's only three lines long (I did it the other way for readability):

            Code: [Select]
            $validchars = 'abcdefghijklmnopqrstuvwxyz0123456789';
            for ($i = 0; $i < strlen($validchars); $i++)  for ($j = 0; $j < strlen($validchars); $j++) for ($k = 0; $k < strlen($validchars); $k++) $combos[]=$validchars[$i].$validchars[$j].$validchars[$k];
            print_r($combos);

            ;D ;D

            OK, your method works, just that I want ONLY the string diplayed in the results, not the number of the string and I want them displayed one under the other as in a column. Thank you very much for your time...

            Also, I want .net at the end of each string. Sorry I didn't say all these things from the beginning but I thought I could do them myself.
            I want them displayed like this :
            aaa.net
            aab.net
            aac.net
            aad.net
            .....


            Thanks again!
            « Last Edit: November 26, 2006, 03:29:40 AM by alex116 »

            Rob Pomeroy



              Prodigy

            • Systems Architect
            • Thanked: 124
              • Me
            • Experience: Expert
            • OS: Other
            Re: Generating codes
            « Reply #14 on: November 26, 2006, 06:35:11 AM »
            Quote
            OK, your method works, just that I want ONLY the string diplayed in the results, not the number of the string and I want them displayed one under the other as in a column. Thank you very much for your time...

            Also, I want .net at the end of each string. Sorry I didn't say all these things from the beginning but I thought I could do them myself.
            Even easier then:

            Code: [Select]
            $validchars = 'abcdefghijklmnopqrstuvwxyz0123456789';
            for ($i = 0; $i < strlen($validchars); $i++)  for ($j = 0; $j < strlen($validchars); $j++) for ($k = 0; $k < strlen($validchars); $k++) echo $validchars[$i].$validchars[$j].$validchars[$k].".net\n";
            « Last Edit: November 26, 2006, 06:35:37 AM by robpomeroy »
            Only able to visit the forums sporadically, sorry.

            Geek & Dummy - honest news, reviews and howtos