Effacer les filtres
Effacer les filtres

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

2 vues (au cours des 30 derniers jours)
Syed Hafiz
Syed Hafiz le 23 Avr 2021
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.

Réponses (1)

Image Analyst
Image Analyst le 23 Avr 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:

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by