[Tutor] how to write an algorithm for sequence

Jayprasad J. Hegde jjhegde@konark.ncst.ernet.in
Fri Feb 14 02:12:02 2003


On Wed, Feb 12, 2003 at 09:18:06PM -0500, 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...
I don't know what python has to do with this question. 

Anyway, here is an analysis and a possible solution to the problem: 
If you look at the problem closely, there are two sequences interleaved to 
make one seemingly complicated sequence. 
You can make use of this observation for solving the problem. 

The given series: 1,5,7,15,19,35, 43, 75...
 
series #1: 1 7 19 43...
series #2: 5 15 35 75...
The first series grows like this: x + (prevDiff * 2) 
	where prevDiff's initial value is 6
	and x is the previous term in this series

The second series grows like this: x + (prevDiff * 2)   --- (yes, the same formula again)
	Here,  prevDiff's initial value is 10
	and 'x' is the previous term in this series

I'm including the code here. Hope it helps. 

I find that the sequence you have given is less by a couple of elements. 
Sometimes these missing elements prove crucial to identifying a pattern
in a problem. 

Anyway, I am also writing the output for verification of the 
desired sequence.  
1 5 7 15 19 35 43 75 91 155 187 315 379 635 763 1275 1531 2555 3067 5115

regards
- JJH

######### the code #############

SEQUENCELENGTH = 20
prevDiff1 = 6
prevTerm1 = 1

prevDiff2 = 10
prevTerm2 = 5
print prevTerm1, prevTerm2, 
for i in range (SEQUENCELENGTH - 2):
    i = i + 1
    if (i % 2 == 0):
        currTerm2 = prevTerm2 + prevDiff2
        prevTerm2 = currTerm2
        prevDiff2 = prevDiff2 * 2
        print currTerm2, 
    else:
        currTerm1 = prevTerm1 + prevDiff1
        prevTerm1 = currTerm1
        prevDiff1 = prevDiff1 * 2
        print currTerm1,

print


-- 
BOFH excuse #362: Plasma conduit breach
(defun JJHdetails ()
  (format t "~&~A~&~A~&~A"
    "Jayprasad J Hegde, Staff Scientist, KBCS, NCST" "Gulmohar Cross
    Road 9, Juhu, Mumbai 400049." "tel: +91-22-26201606x373"))