Cody

# Problem 24. Function Iterator

Solution 331450

Submitted on 9 Oct 2013 by Nick
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
%% noOp = @(x)x; fh2 = iterate_fcn(noOp, 50); assert(isequal(fh2(pi),pi));

cop = @(varargin)varargin{3-varargin{1}}() recur = @(f,varargin)f(f,varargin{:}) ans = @(x)recur(@(f,k)cop(k==1,fh(x),@()fh(f(f,k-1))),n)

2   Pass

cop = @(varargin)varargin{3-varargin{1}}() recur = @(f,varargin)f(f,varargin{:}) ans = @(x)recur(@(f,k)cop(k==1,fh(x),@()fh(f(f,k-1))),n)

3   Pass

cop = @(varargin)varargin{3-varargin{1}}() recur = @(f,varargin)f(f,varargin{:}) ans = @(x)recur(@(f,k)cop(k==1,fh(x),@()fh(f(f,k-1))),n)

4   Pass
%% squarer = @(a) a^2; fh2 = iterate_fcn(squarer, 3); assert(isequal(fh2(3),6561));

cop = @(varargin)varargin{3-varargin{1}}() recur = @(f,varargin)f(f,varargin{:}) ans = @(x)recur(@(f,k)cop(k==1,fh(x),@()fh(f(f,k-1))),n)

5   Pass
%% fh = @(y)sqrt(y+1); fh2 = iterate_fcn(fh,30); assert(abs(fh2(1) - (1+sqrt(5))/2) < 100*eps);

cop = @(varargin)varargin{3-varargin{1}}() recur = @(f,varargin)f(f,varargin{:}) ans = @(x)recur(@(f,k)cop(k==1,fh(x),@()fh(f(f,k-1))),n)