Small mathematics problem

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Live-Dimension
Chaos Rift Junior
Chaos Rift Junior
Posts: 345
Joined: Tue Jan 12, 2010 7:23 pm
Favorite Gaming Platforms: PC - Windows 7
Programming Language of Choice: c++;haxe
Contact:

Small mathematics problem

Post by Live-Dimension »

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 :lol:

Image
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

Image
Image

Sorry to bug people with seemingly a small mathematical issue but I'm really hitting a deadend here! Thanks in advance =]
Image
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Small mathematics problem

Post by avansc »

F(n) = (n*(n+1))/2

here is code that does that for you, its not as good as it can get, but certainly does the job, (i think, i did not test it extensively, just 5 min jot down)

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int* initArray(int n)
{
	if(n < 2)
		return NULL;
	
	int *ret = (int*)malloc(sizeof(int)*((n*(n+1))/2));
	for(int a = 0; a < n;a++)
	{
		ret[a] = a+1;
	}
	return ret;
}

void processArray(int *arr, int n)
{
	//int tN = n;
	int offSet = n;
	int delta = n-1;
	//int max = (n*(n+1))/2;
	
	while(delta > 0)
	{
		for(int a = offSet;a < offSet+delta;a++)
		{
			arr[a] = arr[a-delta-1] + arr[a-delta];
		}
		offSet += delta;
		delta--;
	}
}

void printArray(int *arr, int n)
{
	for(int a = 0;a < ((n*(n+1))/2);a++)
		printf("%d\n", arr[a]);
}

int main(int argc, char * const argv[])
{
	printf("Start.\n");
	int *data = initArray(4);
	
	processArray(data, 4);
	
	printArray(data, 4);
	
	free(data);
    return 0;
}

Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Live-Dimension
Chaos Rift Junior
Chaos Rift Junior
Posts: 345
Joined: Tue Jan 12, 2010 7:23 pm
Favorite Gaming Platforms: PC - Windows 7
Programming Language of Choice: c++;haxe
Contact:

Re: Small mathematics problem

Post by Live-Dimension »

I've been talking to some people on IRC and got loads of information to go through, so for now consider this solved as it's very likely I'll be able to.
Image
Post Reply