Sign in to follow this  
Followers 0
mr_malee

Calculate number sequence

10 posts in this topic

Hi, was wondering if there's a more mathematical approach to calculating a sequence of numbers where each number multiplies itself by 2. so starting with the number 5, and running 6 levels you would end up with

 

5, 10, 20, 40, 80, 160

 

I've got this and it works, but wondering if there's a more efficient way of doing it?

int n = 5;
int level = 6;

for (int i = 1; i < level; i++) {

	n *= 2;
}

return n;

thanks

0

Share this post


Link to post
Share on other sites

You're performing a multiple of two, n times, which is the same as one multiplication of (2 ^ (n-1))

 

I.E

 

5 * 2 ^ (6 - 1)  is 5 * 2 ^ 5

0

Share this post


Link to post
Share on other sites

ah thank you.

Just gonna slap this here:

 

5 * Mathf.Pow(2, level - 1);

Edited by mr_malee
0

Share this post


Link to post
Share on other sites

In code, you can do this using the bitshift operator as well instead of using pow or something, courtesy of binary:

n = 5 << (level - 1);

Watch out for overflow though. Of course, if later on you want to have partial levels, e.g. level = 2.5 somewhere between level 2 and level 3, then just using pow is the better solution, since you're using floats anyway.

Edited by Bacterius
1

Share this post


Link to post
Share on other sites

Say I wanted to get the inverse of that function. Given these numbers:

 

5, 10, 20, 40, 80, 160

 

how could I produce:

 

1, 2, 3, 4, 5, 6

 

again, I only know how to do this using loops:

int n = 1;
int value = 80;

while (value > 5) {

	value -= value / 2;
	n++;
}

return n;
Edited by mr_malee
0

Share this post


Link to post
Share on other sites

Say I wanted to get the inverse of that function. Given these numbers:

 

5, 10, 20, 40, 80, 160

 

how could I produce:

 

1, 2, 3, 4, 5, 6

 

It would be:

 

level = log2(n / 5) + 1

 

Where log2 is the base-two logarithm. (again, if using bitshifts and integers, log2 can be implemented as the "highest bit set to one" instruction, which has no built-in keyword but may have an intrinsic available on your compiler - but at this point, using your math library's log2 is probably easier).

 

So e.g. for n = 80, we take n / 5 = 16, and log2(16) = 4 which gives us level = 5, as expected.

0

Share this post


Link to post
Share on other sites

hello again, I've got another interesting sequence problem, more of a difficult pattern this time (imo)

 

given this sequence of numbers: 1, 5, 10, 30, 60, 300, 600, 1800, 3600, 18000, 36000

 

can you find a way (without arrays) to return one of these numbers based on an index (1, 2, 3, 4, 5, 6, 7...)?

 

ignoring 1. The sequence is separated into pairs of double size numbers:

 

(5, 10)

(30, 60)

(300, 600)

(1800, 3600)

(18000, 36000)

 

pairs are separated by a multiplication of the preceding number:

 

1 * 5 = 5

10 * 3 = 30

60 * 5 = 300

600 * 3 = 1800

3600 * 5 = 18000

 

I'm almost positive there needs to be some tricky modulus going on, but can't seem to crack it.

 

The very difficult problem is given only an index "7", and the first number real number "5". How do you get 1800. 

Edited by mr_malee
0

Share this post


Link to post
Share on other sites

Your sequence increases by multiples of the repeating sequence [5, 2, 3, 2], so for each multiple of 4 indices, the sequence increases by a factor 60. The remainder of a multiple by 4 can easily be applied afterwards.

 

Two hints:

  1. (index-1)/4 gives you the number of times to multiply by 60.
  2. (index-1)%4 gives you the number of factors to multiply as a fraction of 60; for no remainder you multiply by 1 since all factors are covered by a whole number of 60's, for one remainder you multiply by 1*5=5, for two remainders you multiply by 1*5*2=10, and so on for the last factor which I leave up to you to understand.
0

Share this post


Link to post
Share on other sites

ah Thanks. Here's what I ended up with

if (index == 0) {
			
	return 1;
}

int v = (index - 1) / 4;
int n = (index - 1) % 4;
int m = (int)Mathf.Pow(60, v);

if (n == 0) {

	return 5 * m;
}
else if (n == 1) {

	return 10 * m;
}
else if (n == 2) {

	return 30 * m;
}
else if (n == 3) {

	return 60 * m;
}
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0