# Producing a particular sequence of integers recursively [closed]

I am supposed to write a function which computes the next integer in a sequence from the given one.

For example:

nextval[57]


should give mm

74

I need to make sure this function checks for positive integers, and I need use the Mathematica function IntegerDigits, which returns a list of the individual digits of any integer n.

For example:

IntegerDigits[57]


{5, 7}

The overview of this is to basically take any positive integer, say 57, square the individual digits and add them to get a new integer.

5^2 + 7^2 = 25 + 47 = 74


I know I am supposed to make function using Module, but might it also use an If statement?

nextVal[x_Integer?Positive] := Total[IntegerDigits[x]^2]


The sequence can be readily generated using NestList

seq = NestList[nextVal, 57, 27]

(*  {57, 74, 65, 61, 37, 58, 89, 145, 42, 20, 4, 16, 37, 58, 89, 145,
42, 20, 4, 16, 37, 58, 89, 145, 42, 20, 4, 16}  *)


The function to generate the sequence is expressible as a DifferenceRoot

sf = FindSequenceFunction[seq]


Verifying,

seq == sf /@ Range[Length[seq]]

(*  True  *)


is this what you want?

nextval[x_] := Total[IntegerDigits[x]^2];
nextval[57]


Also you can make this sequence which starts with 57 (lets take 20 steps)

y = 57;
steps = 20;
S = {};
nextval[x_] := Total[IntegerDigits[x]^2];
For[i = 1, i <= steps, i++, t = nextval[y]; y = t; AppendTo[S, y]]
S


{74, 65, 61, 37, 58, 89, 145, 42, 20, 4, 16, 37, 58, 89, 145, 42, 20, 4, 16, 37}

after some steps you get the repeated sequence 37,58,89, 145, 42, 20, 4, 16