Calculate number sequence

Started by
9 comments, last by mr_malee 9 years, 8 months ago

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

Advertisement

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

ah thank you.

Just gonna slap this here:

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

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.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

awesome, thanks.

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;

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.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

You are a wizard. Thank you.

Wish I understood these Math functions better. :|

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.

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.

This topic is closed to new replies.

Advertisement