# Collatz Conjecture using for loop

66 ビュー (過去 30 日間)
B 2021 年 9 月 17 日
コメント済み: B 2021 年 9 月 17 日
Consider the following algorithm
• if the number is even, divide by 2 if not multiply it by 3 and add one
• repeat the process until the number 1 is obatined
We ae required to create a function that takes two inputs 'n'=positive integer and 'max_steps' and returns the number of steps rquired to reach 1. The function is meant to be such that if the number of steps reaches the value 'max_steps'(without the algorithm reaching 1) it returns Nan. The code has to use a for loop and at least 1 if statement
N='Nan';
steps = 0;
while n~=1;
if mod(n,2) == 0;
n = n/2;
else
n = 3*n + 1;
end
steps=steps+1;
end
if steps>max_steps
disp=(N);
end
Most of the code is correct expect i cant seem to get 'Nan' when the number of steps reaches the value 'max_steps' and dont know i can implement a for loop
Any help will appreciated

サインインしてコメントする。

### 採用された回答

John D'Errico 2021 年 9 月 17 日
Your code is close to a working algorithm.
There are some issues truly good code would worry about. Could you ever have an overflow issue? I'm not talking about overflowing the realmax value for a double, but instead, the largest possible integer a double can represent exactly and unambiguously. This is flintmax in MATLAB.
flintmax
ans = 9.0072e+15
And flintmax might not be that terribly a huge number in this context. flintmax is 2^53-1. So good code would worry about that. Perhaps you can gain a little headroom, by using uint64 to represent your integers.
First, this must be a function. Displaying a result in NOT the same thing as returning a value from a function. So your code should be wrapped in a function, like this:
function steps = collatz(n,max_steps)
end
Now I'll put your while loop in there.
function steps = collatz(n,max_steps)
steps = 0;
while n~=1
if mod(n,2) == 0;
n = n/2;
else
n = 3*n + 1;
end
steps=steps+1;
end
end
But this never worries about how to stop that loop when there is a problem. So we need to do more. One idea is to use the break command, when max_steps is exceeded. Another idea is to change the value of N to a NaN, and then to put that into the test for the while loop.
function steps = collatz(n,max_steps)
steps = 0;
while n~=1 && ~isnan(n)
% the collatz iteration
if mod(n,2) == 0;
n = n/2;
else
n = 3*n + 1;
end
steps=steps+1; % increment the steps counter.
if (steps >= max_steps) && (n~=1) % have we gone too far?
n = NaN;
end
end
end
As you can see, this version will overwrite the value of n is we have hit the step counter limit. And then the while loop will fail.
The alternative is to use break.
function steps = collatz(n,max_steps)
steps = 0;
while n~=1
% the collatz iteration
if mod(n,2) == 0;
n = n/2;
else
n = 3*n + 1;
end
steps=steps+1; % increment the steps counter.
if (steps >= max_steps) && (n~=1) % have we gone too far?
n = NaN;
break
end
end
end
And of course, there are surely other ways you might write it.
##### 1 件のコメント表示非表示 なし
B 2021 年 9 月 17 日
thanks alot

サインインしてコメントする。

### カテゴリ

Find more on Matrix Indexing in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by