Code covered by the BSD License

# Get Keith Numbers

### Sebastian (view profile)

15 Nov 2012 (Updated )

Finds and returns all Keith numbers between min and max inputs

getKeiths(min, max)
```function keiths = getKeiths(min, max)
%GETKEITHS Returns a Nx2 matrix with all N keith numbers between inputs
%
% This function will find all the Keith numbers between the inputs min and max, inclusive.
% The output matrix is a 2xN matrix, where N is the number of Keith numbers that were found between inputs. The first column is the number of digits in the found Keith number, and the second is the Keith number itself.
% For larger numbers, make sure uint64() is used on arguments passed to the function. This ensures that the input is an unsigned integer 64, which will be able to handle larger numbers.
% There is a maximum imposed by the storage capacity of unsigned integers 64. That is of less than 18446744073709551615. This can be found by running intmax('uint64').
%
% Example:
% getKeiths( uint64(10), uint64(10000) )
%
% ans =
%
%            2          14
%            2          19
%            2          28
%            2          47
%            2          61
%            2          75
%            3         197
%            3         742
%            4        1104
%            4        1537
%            4        2208
%            4        2580
%            4        3684
%            4        4788
%            4        7385
%            4        7647
%            4        7909
%
% http://mathworld.wolfram.com/KeithNumber.html
% http://en.wikipedia.org/wiki/Keith_number

if min < 1 || max < 1
error('Both inputs bust be positive integers. Please use uint64 on input. Read help for more details.');
end

if min >= max
error('max value must be greater than min. This might be because you forgot to include uint64 with large numbers. Read help for more details.');
end

if min >= intmax('uint64') || min >= intmax('uint64')
error('Both parameters must be less than 18446744073709551615.');
end

i = uint64(min);
keiths = [];

while i <= uint64(max)
digits = num2str(i);
length = numel(digits);

current = uint64(zeros(1,length));

for j = 1:length
current(j) = str2double(digits(j));
end

summing = uint64(0);

while (summing < i)
summing = sum(current);
current = circshift(current, [0 -1]);
current(length) = summing;
end

if summing == i
keiths(end + 1, 1) = length;
keiths(end, 2) = i;
end

i = i+1;
end

return
end

```