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

8 ビュー (過去 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)
  1 件のコメント
M Ahasanul
M Ahasanul 2023 年 12 月 11 日
10

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by