Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function result = isKaprekarNumber(number)
result=0;
lenOfNumber = length(num2str(number));
numberArray = num2str(number);
sqOfNumber = number.^2;
sqNumberArray = num2str(sqOfNumber);
sqHead= sqNumberArray(1:lenOfNumber);
sqTail= sqNumberArray(lenOfNumber+1:lenOfNumber+lenOfNumber);
sumOfHeadAndTail = str2num(sqNumberArray(1:lenOfNumber))+str2num(sqNumberArray(lenOfNumber+1:lenOfNumber+lenOfNumber));
if isequal(number,sumOfHeadAndTail)
result=1;
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment