Need help on a Question producing a sequence
Afficher commentaires plus anciens
I'm stucked on a question which produces a sequence. Can anybody help? Here's the question:
Let x(0) be any integer. Suppose the following rule is used to define a sequence of numbers based on x(0). x(k+1)=x(2)/k if x(k) is even or 3*x(k)+1 if x(k) is odd.
If the sequence stop generating value when x(k)=1 or k>500. Write a script file to show whether the sequence diverge to infinity or converge to 1.(Hint: What is needed is a 'while loop' that stops when x(k)=1 or k>500, and an 'if-else-end/ construction to implement the foregoing rule.)
I have no idea on how to implement this.
Réponses (1)
Rick Rosson
le 3 Mar 2014
Modifié(e) : Rick Rosson
le 3 Mar 2014
Here's a start:
N = 500;
x = nan(N+1,1);
x(1) = ...
k = 1;
while ...
if ...
x(k+1) = ...
else
x(k+1) = ...
end
k = k + 1;
end
x = x(~isnan(x));
N = size(x,1);
figure;
stem(0:N-1,x);
Catégories
En savoir plus sur Desktop dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!