Collatz Conjecture using for loop
조회 수: 38 (최근 30일)
이전 댓글 표시
Consider the following algorithm
- start with positive integer
- 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
댓글 수: 0
채택된 답변
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
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!