Question: How to compute a recursive sequence

How is it possible in Maple to keep hold of pre determined results for comparison with subsequent results so that a recursive decision can be made to either modify the list of “kept” data or to continue to the next calculation, etc... ?

Example: a simple “subtract or double” sequence. If subtracting (say 1) from the current number would result in a term we already have,  then double it instead, and start over again with subtraction.

Formally: a(0)=0, a(1)=1, and for n>=1,

a(n+1) = a(n)-1 if that number has not been found already, else a(n+1)=2*a(n).

0,1,2,4,3,6,5,10,9,8,7,14,13,12,11,22.....

The arithmetic operations are facile but how to organise the keeping and comparison process??

David.

 

Please Wait...