2

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 Answers2

2

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.
Adriano
  • 41,576
1

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 .

acer
  • 5,293
  • Wow, i didn't know you could add a criterion to seq( ). That's very useful. Can you explain why there is a NULL inside the if. – john Sep 29 '22 at 06:32
  • The first argument to the functional if call is the condition. The second argument is the return value (of the if call) if the condition is true. The third argument is the return value (of the if call) if the condition is not true. Sometimes it's handy to be able to specifiy that third argument as NULL, ie. nothing is contributed by the failing cases. Of course the above is a toy example; in practice one could generate the even numbers in better ways, eg. seq with a step of 2. – acer Oct 11 '22 at 23:25