Jump to content

  • Log In with Google      Sign In   
  • Create Account


Calculate number sequence

  • You cannot reply to this topic
10 replies to this topic

#1 mr_malee   Members   -  Reputation: 152

Like
0Likes
Like

Posted 12 August 2014 - 05:44 PM

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



Sponsor:

#2 Angus Hollands   Members   -  Reputation: 713

Like
0Likes
Like

Posted 12 August 2014 - 05:50 PM

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



#3 mr_malee   Members   -  Reputation: 152

Like
0Likes
Like

Posted 12 August 2014 - 06:02 PM

ah thank you.

Just gonna slap this here:

 

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


Edited by mr_malee, 12 August 2014 - 06:04 PM.


#4 Bacterius   Crossbones+   -  Reputation: 8658

Like
1Likes
Like

Posted 12 August 2014 - 06:04 PM

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, 12 August 2014 - 06:06 PM.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#5 mr_malee   Members   -  Reputation: 152

Like
0Likes
Like

Posted 12 August 2014 - 06:28 PM

awesome, thanks.



#6 mr_malee   Members   -  Reputation: 152

Like
0Likes
Like

Posted 12 August 2014 - 07:22 PM

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, 12 August 2014 - 07:33 PM.


#7 Bacterius   Crossbones+   -  Reputation: 8658

Like
0Likes
Like

Posted 12 August 2014 - 07:30 PM

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.


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#8 mr_malee   Members   -  Reputation: 152

Like
0Likes
Like

Posted 12 August 2014 - 07:39 PM

You are a wizard. Thank you.

 

Wish I understood these Math functions better. :|



#9 mr_malee   Members   -  Reputation: 152

Like
0Likes
Like

Posted 13 August 2014 - 05:29 PM

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, 13 August 2014 - 05:48 PM.


#10 Brother Bob   Moderators   -  Reputation: 8087

Like
0Likes
Like

Posted 13 August 2014 - 06:11 PM

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.


#11 mr_malee   Members   -  Reputation: 152

Like
0Likes
Like

Posted 13 August 2014 - 07:16 PM

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;
}






PARTNERS