# 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?

## closed as off-topic by Daniel Lichtblau, MarcoB, zhk, happy fish, gwrApr 25 '17 at 8:47

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – Daniel Lichtblau, MarcoB, zhk, happy fish, gwr
If this question can be reworded to fit the rules in the help center, please edit the question.

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