fill two-dimensional array
I have an Array with primenumbers: Prime() = 1,2,3,5,7,11,13,17,19
With this array I want to make a function FillArray(n As Integer).
This function tries to find every possible sum of primenumbers to be 7 (is Prime(4)).
BUT, every primenumber is to be followed by his next or previous primenumber in array Prime, like in following sequense. I want function FillArray to make this sequense.
1+2+3+5=11
1+2+3+2=8
1+2+1+2+3=9
1+2+1+2+1=7 is true
2+3+5=10
2+3+2=7 is true
2+1+2+3=8
2+1+2+1+2=8
3+5=8
3+2+3=8
3+2+1+2=8
5+7=12
5+3=8
7=7 is true
This seqence learns there are three sums of primenumbers to be true:
1+2+1+2+1=7
2+3+2=7
7=7
These results have to be put in a two-dimensional array like this:
1,2,1,2,1
2,3,2
7
I tried to make some code, but my VBA knowledge is too limited to make it function. So help is needed. Maybe the code helps understanding what I want, although you will find many errors in it.
visual basic code:
Function FillArray(n As Integer) 'for this example n=4
Dim primeSum, counter, row, number As Integer
Dim primeArray(row,number), Prime() As ?
primeSum=0
counter=0
row=0
number=0
DoWhile primeSum<Prime(n)
primeSum = primeSum + Prime(counter)
primeArray(row,number) = Prime(counter)
counter=counter+1
number=number+1
If primeSum=Prime(n)
primeArray(row,number)=Prime(counter)
row=row+1
'Then copy in primeArray the values of the last row in the new one
'except the last value
number=number-1 'go place back in the new row in primeArray
counter=counter-2 'now return to the DoWhile loop and go on
Else counter=counter-2 'primeSum>Prime(n) When counter<2,
'number has to be number-1?
primeSum=0 'start all over again
a=1 'start with the second prime
row=row+1 'go on with next row in primeArray
number=0 ' and then DoWhile loop starts again
|