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

1 Ansicht (letzte 30 Tage)
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.

Antworten (1)

Image Analyst
Image Analyst am 23 Apr. 2021
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:

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by