[Tutor] how to write an algorithm for sequence

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Tue Feb 11 22:13:02 2003


On Wed, 12 Feb 2003, reavey wrote:

> I'm stumped. I made up this sequence and I can't figure out a method.
> the sequence is 1,5,7,15,19,35, 43, 75...

Hi Reavey,


This isn't quite Python either, but I'll give it a shot.  *grin*

Hmmm... I don't see anything immediate from the sequence above either.


But have you tried the method of differences?  Here's how it works: take
each pair of numbers, subtract them, and see if a pattern emerges.

###
>>> def differences(seq):
...     for i in range(0, len(seq)-1):
...         print seq[i+1] - seq[i],
...
>>> differences([1, 5, 7, 15, 19, 35, 43, 75])
4 2 8 4 16 8 32
###


Let's arrange it so that it's easier to see what's going on:


    1   5   7   15   19   35   43   75
      4   2   8    4   16    8    32



Interesting.  All of the individual "differences" are even, first of all.
Wait: Reavey, I do see some sort of pattern here.  Do you see it too now?



Good luck to you!