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

కామెంట్‌లు

ఈ బ్లాగ్ నుండి ప్రసిద్ధ పోస్ట్‌లు

100% C prog to write content into a file and then read it back onto the console

Spic macay

శ్రీరస్తు.. రామలింగ 18-1-24 (సూరసాని వారి ఆహ్వానము)