How to generate a sequence in maple based on a condition

1.9k Views Asked by At

I'm supposed to generate a sequence of even numbers from $1$ to $100$ in Maple. While this should be very straightforward, I've tried using both a conditional statement within the $\text{seq}()$ command as well as a loop but got an error in both cases. I've looked at the structure for the loop and can't quite determine where my syntax is incorrect.

2

There are 2 best solutions below

0
On

Try one of the following:

> step := 2:
  upper := 100:
  seq(step*i, i=1..upper/step);  # 2*1, 2*2, 2*3, ..., 2*(100/2).
  seq(step..upper, step);        # 2, 2 + 2, 2 + 2*2, 2 + 3*2, ..., 100.
2
On

You ought to prefer Adriano's answer.

However you seem to be learning Maple, and you did mention that you were unable to get a conditional to work within a call to the seq command. So I'll mention that can be done by using the so-called operator form of if.

seq( `if`( i::even, i, NULL ), i=1..100 );

seq( `if`( irem(i,2)=0, i, NULL ), i=1..100 );

Note that your question is about only programming in Maple rather than doing math in Maple, and so is off-topic in this forum. More appropriate would be www.stackoverflow.com or www.mapleprimes.com .