100% matlab code .. optimised to get 4,2,1 pattern
%Can be optimised as follows
p=4+ceil(rand*20) %choosing a integer between & including 5 and 24
count=0;
while(count<3) %% after few recurrences of the pattern 4,2,1 we wish to stop
if (mod(p,2)==0)
p=p/2
else
p=3*p+1
end
if(p==1)
count=count+1;
end
end
disp('lets stop now as we noticed it is giving 4,2,1 sequence as a loop')
ఔట్పుట్
Output:
p = 23
p = 70
p = 35
p = 106
p = 53
p = 160
p = 80
p = 40
p = 20
p = 10
p = 5
p = 16
p = 8
p = 4
p = 2
p = 1
p = 4
p = 2
p = 1
p = 4
p = 2
p = 1
lets stop now as we noticed it is giving 4,2,1 sequence as a loop
created just now
Octave Online
కామెంట్లు
కామెంట్ను పోస్ట్ చేయండి
దయచేసి మీ సలహాలను సూచనలను స్పష్టంగా పేర్కొనగలరు. plz see that ur comments are 'acceptable' in a value based society.