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

2 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by