Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
% Städa bort gamla variabler och grafer
clear
clf
antalTal=10000; % Variabel som anger antal tal
maxSteg=0; % Variabel för att räkna antal steg
% k räknar från 0 till antalTal
for k=1:antalTal
% Sätt startvärde för n, nollställ räknaren för steg
n=k;
steg = 0;
# Utför de steg som algoritmen föreskriver
while n != 1
steg =steg + 1;
if n / 2 == round(n / 2)
n=n/2;
else
n=3*n+1;
end
end
if steg> maxSteg
maxSteg=steg;
end
% Skriv ut resultat
disp([num2str(k)," (kräver ",num2str(steg)," steg)"])
stegArray(k)=steg;
end
%plot(1:antalTal,stegArray)
%[max maxindex]=max(stegArray)
maxSteg
Advertisements
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
AcceptLearn more