Need help on a Question producing a sequence
조회 수: 3 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (1개)
Rick Rosson
2014년 3월 3일
편집: Rick Rosson
2014년 3월 3일
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);
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!