# Thread: I don't know what you would call this.. I guess counting. Can anyone help?

1. ## I don't know what you would call this.. I guess counting. Can anyone help?

Hey nerds, or would you guys prefer geeks?

I'm a programmer in training and have come up with a little idea which requires some math that I can't work out.

Basically, I have an array:

Code:
A = Array (
0 => 'a',
1 => 'b',
2 => 'c'
)
If you guys understand that much.. I don't know what the math terminology is for this kind of rubbish.

this is basically what it is..
A0 = a
A1 = b
A2 = c

Then here's what I'm trying to do.

I'm planning on looping through every letter in the array, then once I've ran out of letters I start with the first letter and make a double letter string. For example:

Code:
a
b
c
aa
ab
ac
ba
bb
bc
ca
cb
cc
aaa
aab
aac
aba
abb
abc
aca
acb
acc
baa
bab
bac
bba
bbb
bbc
bca
bcb
bcc
caa
cab
cac
cba
cbb
cbc
cca
ccb
ccc.. etc
Just like couting..
Code:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20... etc
K.. well what would be the way of doing this. I'm guessing some sort of power to.

Thanks for any help.

2. Hi

Are you just using A-C or A-Z?
What language are you writing this in?

3. Hi Again

Count through your array with a loop (For..Next etc)
Add value of loop to 64 (if array starts at 1) and then look-up the ASCII character (there is a function for this in most languages).
After the first set of characters you can increment another counter, look up two values and then concatenate the results.
Does this make sense or do you want me to write some pseudo code?

4. It's going to be written in Java, PHP or Perl. Haven't decided on that yet.

The whole idea is to simply make it count. And the idea is to make an array with the numbers 0-9 and letters a-z. I'm trying to get more than
Code:
3,7199332678990121746799944815084E+41
combinations. It should probably work out as around twice as many more. I don't know how to work it out as it's not really working out a combination or permutation.

So say my array is,

Code:
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
[7] => 7
[8] => 8
[9] => 9
[10] => a
[11] => b
[12] => c
[13] => d
[14] => e
[15] => f
[16] => g
[17] => h
[18] => i
[19] => j
[20] => k
[21] => l
[22] => m
[23] => n
[24] => o
[25] => p
[26] => q
[27] => r
[28] => s
[29] => t
[30] => u
[31] => v
[32] => w
[33] => x
[34] => y
[35] => z
)
I can loop through that with different types of loops, depending on the language.

My problem is I need to figure a way of counting the level, so once it's looped to the length of the array, start again with the first value and loop until the first value + all the rest of the values matches the length*the length, then start again untill the length matches the length^3 etc.. etc.. etc.. I could do this by writing an infinite number of loops by hand. But that's not the point. It's supposed to go automatic. So it would be easy if it could be done with some simple math.

Thanks for the help so far!

5. Here is one way to do it in Perl, using recursion.

Code:
$chars = 2;$array[0] = 'a';
$array[1] = 'b'; for (my$i = 1; $i <= 3;$i++) { loop($i, ""); } sub loop { my ($depth, $prefix) = @_; if ($depth == 0) { print "$prefix\n"; } else { for (my$i = 0; $i <$chars; $i++) { loop($depth - 1, $prefix .$array[\$i]);
}
}
}
The idea is the following. To print this sequence of lines of length 3:
Code:
aaa
aab
aba
abb
baa
bab
bba
bbb
one has to start with "a" and print the sequence of lines of length 2, then start with "b" and print the sequence of lines of length 2, etc.