Question: Tidying up a messy set of Proc and loops. (Recurrece relations)

Hello everyone

I want to check whether a recurrence relation produces integers. What I have written is rather messy, I ideally would like some kind of Proc where I can just put in the recurrence relation, the initial conditions and the number of terms I would like to check. Then get a result out which tells me whether the terms in the sequence are all integer. 

 

I have written the following (which tells me what I want to know but in a crude way)

#this sets up the sequence and initial values

x:=proc(n)
option remember;
(x(n-1)^2+x(n-2)^2)/x(n-3);
end proc:

x(0):=2: x(1):=2: x(2):=2:
for i from 0 to 20 do A[i]:=x(i) od:

# this checks if integer and returns true or false
for j from 0 to 20 do B[j]:=type(A[j], integer) od:

#This swaps a true for a 0 and a false for a 1
Y:= proc(i)
if B[i]='true' then 0; else 1; fi; end:          
for l from 0 to 20 do P[l]:=Y(l) od:

#this sums the terms so and positive integer returned here means the is a non integer term in the sequence
add(P[n],n=0..20);

 

Many thanks Pete

Please Wait...