Filter löschen
Filter löschen

Need help on a Question producing a sequence

3 Ansichten (letzte 30 Tage)
Calvin
Calvin am 3 Mär. 2014
Bearbeitet: Rick Rosson am 3 Mär. 2014
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.

Antworten (1)

Rick Rosson
Rick Rosson am 3 Mär. 2014
Bearbeitet: Rick Rosson am 3 Mär. 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);

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by