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

조회 수: 22 (최근 30일)
Murad Alzahrani
Murad Alzahrani 2019년 5월 17일
댓글: Alexander 2023년 12월 12일
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

답변 (2개)

Jos (10584)
Jos (10584) 2019년 5월 17일
or know your math classics ...
n = 120345428372
s = n*(n+1)/2
% sum(1:n) will fail!
  댓글 수: 6
Walter Roberson
Walter Roberson 2023년 12월 11일
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 2023년 12월 12일
Understood. Thank you

댓글을 달려면 로그인하십시오.


Stephan
Stephan 2019년 5월 17일
편집: Stephan 2019년 5월 17일
n = 4;
A = sum(1:n)

카테고리

Help CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by