what is the highest number that matlab can handle. (especiially for the 3x+1 problem). i am trying to do the seed 10^310 i am not getting the plot or output why?

8 次查看(过去 30 天)
% Code for 3x+1 Problem
seed = 10^309; % Starting number
x = seed; % Initialize x with the seed
trajectory = x; % Store the trajectory
tic
while x ~= 1
if mod(x, 2) == 0 % Check if x is even
x = x / 2;
else % If x is odd
x = 3 * x + 1;
end
trajectory = [trajectory, x]; % Append x to trajectory
%pause(0.01); % Slow it down artificially
end
toc
% Plot the trajectory
figure;
plot(trajectory, '-o', 'LineWidth', 1, 'MarkerSize', 5,'MarkerFaceColor', 'r');
title('Plot of the Collatz Sequence for positive Seed 63728127');
xlabel('Step');
ylabel('Value');
grid on;

回答(1 个)

John D'Errico
John D'Errico 2025-7-15
Your question should not be the highest number, but the largest INTEGER MATLAB can handle as a double.
That would be 2^53-1. Anyting larger in a double, and MATLAB no longer represents it exactly, and so knowing if the number is even or odd is impossible beyond that point.
You can go a little further using UINT64, but even there, you need to worry about overflows, since UINT64 will just max out.
Instead, you need to be using symbolic tools. But that will get slow.

类别

Help CenterFile Exchange 中查找有关 Scatter Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by