Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

So I have to do this problem in Matlab, but I have no idea how to do this. The f

ID: 3872488 • Letter: S

Question

So I have to do this problem in Matlab, but I have no idea how to do this.

The following is an excerpt from the Wikipedia article on 6174 (number) 6174 is known as Kaprekar's constant, after the Indian mathematician D. R. Kaprekar. This number is notable for the following property: 1. Take any four-digit number having at least two different digits a. Add leading zeros to numbers having less than 4 digits Arrange the digits in descending and then in ascending order to get two four-digit numbers, adding leading zeros if necessary. Subtract the smaller number from the bigger number to get a new 4-digit number Go back to step 2. 2. 3. 4. The above process, known as Kaprekar's routine, will always reach its fixed point, 6174, in at most 7 iterations. Once 6174 is reached, the process will continue yielding 7641-1467-6174 For example, choose 3524 5432-2345-3087 8730-0378 = 8352 8532-2358 = 6174 7641-1467 = 6174 The only four-digit numbers for which Kaprekar's routine does not reach 6174 are repdigits such as 1111, which give the result 0000 after a single iteration. All other four-digit numbers eventually reach 6174 if leading zeros are used to keep the number of digits at four

Explanation / Answer

%matlab 3 files required to be separately saved

%%%%% counter.m  

%function to calculate number of iteration required
function count=counter(x)
%settingh count to zero
count=0
%taking output of maker function into large and small
[large,small]=maker(x)
while(1)
%comparing the loop end conditions
if(large-small)==6174 or (large-small)==0000)
break
end %END OF IF
%counting iterations
count=count+1 ;
%GETTING NEW NUMBER
x=large-small;
%calling maker function to get smallest and largest number with given digits
[large,small] =maker(x);

end%end of while
end%end of function

%%%%% maker.m

%function making smallest and largest numbers with the given number digits
function [large,small]=maker(x)

%adding leading zeroes to number
number=str2num(num2str(x,%04d))
%extracting first digit
a=rem(number,1000);
%extracting second digit
b=rem(number-a*1000,100);
%extracting third digit
c=rem(number-b*100-a*1000,10);
%extracting fourth digit
d=number-a*1000-b*100-c*10;
%putting digits in a vector
r=[a b c d];
%insertion sort
for i=1:4
for i=1:4-i
if (r(i)>(i+1))
temp=r(i+1);
r(i+1)=r(i);
r(i)=r(i+1);
end%end of if
end%end of for
end%end of for
%output smallest number from given number's digits
small=r(1)*1000+ r(2)*100+ r(3)*10+ r(4)*1;
%output smallest number from given number's digits
large=r(1)*1+ r(2)*10+ r(3)*100+ r(4)*1000;
end%end of function

%%%%% main.m


prompt="enter the four digit or smaller number"
%storing the number in x
x=input(prompt)

if x<0 & x>9999:
print "invalid number"

end %END OF IF
for i=0:9999
counter(i)
end %end of for

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote