Programming exercise for beginners II

• 09-21-2011, 09:23 AM
vijayan
Programming exercise for beginners II
Requires knowledge of: functions, arrays (or basics of std::vector<>)

Define the integer function F(x) as the sum of the squares of the digits of x.
Example: F(3) = 3*3 = 9 and F(230) = 2*2 + 3*3 + 0*0 = 13.

Define the set S(x) to be the set of (unique) numbers that are produced by repeatedly applying F to x.
ie: F(x), F( F(x) ), F( F( F(x) ) ), etc...

Example: Repeatedly applying F to 37, we get:
F(37) = 3*3 + 7*7 = 58.
F(58) = 5*5 + 8*8 = 89.
F(89) = 145.
F(145) = 42.
F(42) = 20.
F(20) = 4.
F(4) = 16.
F(16) = 37.

This sequence will now repeat; so we can stop and:
S(37) = { 58, 89, 145, 42, 20, 4, 16, 37 }.
Note that S(x) need not necessarily contain x.

Implement a function
Code:

`int smallest_member( int x ) ;`
taking int x as the input, and returning the smallest integer in S(x) as the result. Assume that x is in the range 0 <= x <= 99

Example: smallest_member(37) should return 4.