Small mathematics problem
Posted: Tue Apr 20, 2010 8:50 am
Hello all. I've never been that great at maths so sometimes I come across problems that stump me for a short while. This one has been for over 6 hours and I just don't have the time to dedicate to this problem any longer... Plus someone will probably come to an answer in about 2 seconds flat after posting
It's an array with ID values of 1-10. When first used the values 1,2,3,4 are filled in by another function and they work fine. The other values are generated in ascending order (5, 6, 7, etc) from these first 4 values. When I'm at position 5 of the array, I need a mathematical function that can return 1 and 2. If I'm at 8, I need a function that can return 5 and 6, etc etc. I've tried using loops and stuff to get these numbers but I just haven found the right way. This is going to be executed quite alot per game loop when in use (probably at least over 100-200 times) so Its best if its not like a 50-line function lol.
The challenge is that the array can be 3 to n length, as illustrated below, and this is what I'm having issues with. For example, these are valid array lengths.
Base iteration, Total array length.
2, 1
3,3
4,6
5,10
6,15
7,21
8,28
9,36
10,45
I've found out that the array length grows as the difference of previous length + 1. Iliteration 1 can be ignored as it's already taken care off, so it needs to work from 3) onwards.
Iliteration , previous value, next value, value difference
2) 0 -> 1 = 1
3) 1 -> 3 = 2
4) 3 -> 6 = 3
5) 6 -> 10 = 4
6) 10 -> 15 = 5
7) 15 -> 21 = 6
Sorry to bug people with seemingly a small mathematical issue but I'm really hitting a deadend here! Thanks in advance =]
It's an array with ID values of 1-10. When first used the values 1,2,3,4 are filled in by another function and they work fine. The other values are generated in ascending order (5, 6, 7, etc) from these first 4 values. When I'm at position 5 of the array, I need a mathematical function that can return 1 and 2. If I'm at 8, I need a function that can return 5 and 6, etc etc. I've tried using loops and stuff to get these numbers but I just haven found the right way. This is going to be executed quite alot per game loop when in use (probably at least over 100-200 times) so Its best if its not like a 50-line function lol.
The challenge is that the array can be 3 to n length, as illustrated below, and this is what I'm having issues with. For example, these are valid array lengths.
Base iteration, Total array length.
2, 1
3,3
4,6
5,10
6,15
7,21
8,28
9,36
10,45
I've found out that the array length grows as the difference of previous length + 1. Iliteration 1 can be ignored as it's already taken care off, so it needs to work from 3) onwards.
Iliteration , previous value, next value, value difference
2) 0 -> 1 = 1
3) 1 -> 3 = 2
4) 3 -> 6 = 3
5) 6 -> 10 = 4
6) 10 -> 15 = 5
7) 15 -> 21 = 6
Sorry to bug people with seemingly a small mathematical issue but I'm really hitting a deadend here! Thanks in advance =]