I am trying to generate a plot for the 3x+1 problem for the seed 10^310 but i am unable to get the plot? Why
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
% 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
end
toc
% Plot the trajectory
figure;
plot(trajectory, '-o', 'LineWidth', 1, 'MarkerSize', 5,'MarkerFaceColor', 'r');
title('Plot of the Collatz Sequence for positive Seed 10^310');
xlabel('Step');
ylabel('Value');
grid on;
0 commentaires
Réponses (1)
Matt J
le 15 Juil 2025
Modifié(e) : Matt J
le 15 Juil 2025
It's because 1e309 is too large for double float math to handle. The largest integer that can be expressed in double float precision is 2^53. (EDIT - The largest consecutive integer).
mod(1e309, 2) %Fails
mod(1 + 2^56 , 2) %Falsely even number
It's definitely not a problem with plotting, though, as can be seen for smaller seeds below.
% Code for 3x+1 Problem
seed = 1e30; % 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
end
toc
% Plot the trajectory
figure;
plot(trajectory, '-o', 'LineWidth', 1, 'MarkerSize', 5,'MarkerFaceColor', 'r');
title("Plot of the Collatz Sequence for positive Seed " +seed );
xlabel('Step');
ylabel('Value');
grid on;
3 commentaires
Steven Lord
le 15 Juil 2025
It's because 1e309 is too large for double float math to handle.
Yes, the largest finite double precision value is:
r = realmax
How is 1e309 stored in IEEE double precision? It overflows.
q = 1e309
The largest integer that can be expressed in double float precision is 2^53.
Not correct. All exactly repressentable double precision values greater than flintmax are integer values, but not all integer values greater than flintmax are exactly representable in double precision. Let's take flintmax + 1 as an example.
y = flintmax
z = y + 1
What's the spacing from y to the next largest representable value?
eps(y)
So since 1 is less than 2, flintmax + 1 is not exactly representable as different from flintmax.
y == z
Taking a step of length 3 from flintmax gives the same value as taking a step of length 4.
(y + 3) == (y + 4)
Let's check integer value-ness. Both 2*flintmax and realmax are integer values if we test whether they are equal to their rounded value.
isintegervalue = @(x) x == round(x);
isintegervalue(2*y)
isintegervalue(r)
If you're going to use symbolic numbers, rather than starting off with a piece of text representing the number I'd start off with a value that is exactly representable, convert it into a symbolic variable, then perform symbolic computations with it.
ten = sym(10)
tenTo309 = ten ^ 309
You can even preallocate an array of all 0 values using zeros by saying you want the type to be sym.
symzeros = zeros(1, 10, 'sym') % or
symzeros2 = zeros(1, 10, 'like', ten)
whos ten tenTo309 symzeros symzeros2
Walter Roberson
le 15 Juil 2025
Modifié(e) : Walter Roberson
le 15 Juil 2025
I noticed an oddity in representation that I will be reporting to Mathworks
ten = sym(10)
tenTo309 = ten ^ 309
When Steve executed, the output representation was 1.0000e+309 . That was due to an error in initializing the symbolic engine for MATLAB Answers.
@Steven Lord please do not re-run and save your post; I need it as proof of bad output.
Voir également
Catégories
En savoir plus sur Numbers and Precision dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

