Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

12, 13, 11, 14, 10, 15, 9, 16... 0, maxNum

Start with x then alternate on each side of x until you reach 0 on one side and maxNum on the other. Need to do this in some programming code but can't nail it down. How would you write out the formula for that sequence?

share|improve this question
    
What? It is very hard to figure out what you are trying to describe, especially as your "example" doesn't give specific values. I'm guessing that you are given two numbers, $x$ and $M$, $x\leq M$. Then you will write a sequence; but how? Are you going up or down? Up on one side and down in the other? First up, until you get to M, then down until zero? The other way around? What? If you want to give an example, give an explicit, complete example, not the cryptic first line you give here. –  Arturo Magidin Oct 15 '10 at 2:11
    
How is that example not giving specific values? 12, 13, 11, 14, 10, 15, 9, 16 are not specific values? x is 12. 13 is x+1. So... x, x+1, x-1, x+2, x-2, x+3, x-3, etc. –  sol Oct 15 '10 at 2:15
1  
@sol: You did not tell us what x was; you did not tell us what "MaxNum" was. You did not tell us where you started. You did not give the full list. That's why it was not "specific numbers". I had no way to figure out what "x" was, because from your description I thought you would place numbers alternating on each side of your "x". For instace, I thought you would start with, say, x=5, then write 5,6; then the next number would go on the "other side", giving 7,5,6. Then on the other side, 7,5,6,8. Etc. Your new description is much better than your previous attempt. –  Arturo Magidin Oct 15 '10 at 2:26
add comment

3 Answers

up vote 2 down vote accepted

Let n start at 0 and x be the initial value for the sequence then take $a_{n}(x)=\begin{cases} x-\frac{n}{2} & ,n\equiv0\mbox{mod}(2)\\ x+\frac{n+1}{2} & ,n\equiv1\mbox{mod}(2)\end{cases}$

You'll just need to add some kind of check statement to notice when you get 0 and some variable to store the maxNum and spit it out at the end.

share|improve this answer
    
The n=1(mod 2) line should be x+(n+1)/2. –  Jonas Kibelbek Oct 15 '10 at 2:28
    
Thanks! I corrected it now. –  WWright Oct 15 '10 at 2:29
    
thanks guys, this is helping. How would I determine the next number in the sequence if given a value? In other words, if x=12 (the initial value), and I am given the value y=14. How would I find the next value (which would be 10)? –  sol Oct 15 '10 at 2:41
    
@sol: $y_{t+1}=2x-y_t$ if $y_t > x$ and $y_{t+1}=2x-y_t+1$ if $y_t < x$ –  Jyotirmoy Bhattacharya Oct 15 '10 at 2:52
    
yes, just figured that out Jyotirmoy. Thanks all! –  sol Oct 15 '10 at 2:55
add comment

let x denote the start number, the the value of the i'th term can be written:

f_i = x + (-1)^(i+1) * floor((i+1)/2) , if (i <= 2x)
undefined, otherwise
share|improve this answer
add comment

WWright and anonymous_21321 have given nice formulas for your sequence. (Note that x is the zero-th term in those formulas-- you plug in n=0 or i=0 to get x.)

However, if you're trying to understand how to write a program for the sequence, a formula may not be the easiest way to go. What you wrote in your question is an algorithm for the sequence. To make it a program, you need to make it completely precise and clear.

In SAGE, I would write:

print x
i=1
while x-i >= 0:
     print x+i
     print x-i
     i = i+1
print x+i

This is a direct translation of your description, written precisely for SAGE. With a little insight into the sequence, you could write a better program or even a formula for the n-th term, as the other answers do. If you're just interested in maxNum, there's a simple formula-- you can skip all the other terms in the sequence once you understand what's going on.

share|improve this answer
    
Your program is a perfectly good Python program. Doesn't require SAGE. –  Jyotirmoy Bhattacharya Oct 15 '10 at 12:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.