フィルターのクリア

How to add up first digits to all the prime numbers within a given range ?

2 ビュー (過去 30 日間)
Syed Hafiz
Syed Hafiz 2021 年 4 月 23 日
回答済み: Image Analyst 2021 年 4 月 23 日
In MATLAB, primes(N) provides all the prime numbers less than or equal to N. E.g. primes(15) = [2 3 5 7 11 13]. The sum of the first digit of each prime is 19 (i.e. 2+3+5+7+1+1 = 19).
What is the sum of the first digit of all primes less than or equal to 77777? You may want to use the num2str() function.

回答 (1 件)

Image Analyst
Image Analyst 2021 年 4 月 23 日
Here's a start
p = primes(77777)
theSum = 0;
for k = 1 : length(p)
str = num2str(............
theSum = theSum + double(................
end
You finish it, since you're not allowed to turn in my code as your own. If you need more help, read this:

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by