0
$\begingroup$

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?

  • 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.2010-10-15

3 Answers 3

1

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.

  • 0
    Your program is a perfectly good Python program. Doesn't require SAGE.2010-10-15
2

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 
2

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.

  • 0
    yes, just figured that out Jyotirmoy. Thanks all!2010-10-15