Borrar filtros
Borrar filtros

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

2 visualizaciones (últimos 30 días)
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.

Respuestas (1)

Image Analyst
Image Analyst el 23 de Abr. de 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:

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by