How writing code sum 1+2+3+4+...+n

10 visualizaciones (últimos 30 días)
Murad Alzahrani
Murad Alzahrani el 17 de Mayo de 2019
Comentada: Alexander el 12 de Dic. de 2023
Hi. How I can writing code in MATLAB sum 1+2+3+4+..+n
and I can change ( n ) to any numbers and MATLAB can sum for me.
Thank you

Respuestas (2)

Jos (10584)
Jos (10584) el 17 de Mayo de 2019
or know your math classics ...
n = 120345428372
s = n*(n+1)/2
% sum(1:n) will fail!
  6 comentarios
Walter Roberson
Walter Roberson el 11 de Dic. de 2023
v = sym('7241511065080263999378')
v = 
7241511065080263999378
fprintf('%.999g\n', double(v))
7241511065080263868416
So 7241511065080263868416 is the closest double precision representation of 7241511065080263999378
eps(double(v))
ans = 1048576
which is large enough adjacent representable double precision numbers in that range are 1048576 apart.
Alexander
Alexander el 12 de Dic. de 2023
Understood. Thank you

Iniciar sesión para comentar.


Stephan
Stephan el 17 de Mayo de 2019
Editada: Stephan el 17 de Mayo de 2019
n = 4;
A = sum(1:n)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by