Write a function ShowPrimes(n) that will find all prime numbers between 1 and n and return them as a vector. Do this using a for loop. Hint: use isprime(x) to check whether x is a prime number. As an example, >> primes=ShowPrimes(20) primes =
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Caitlin Schmidt
le 23 Juin 2019
Réponse apportée : infinity
le 23 Juin 2019
I'm having some issues with this problem. So far, I have written:
function vec=ShowPrimes(n)
%Creates a vector of all of the prime numbers in a range of 1 to n
%Format:ShowPrimes(n)
for i=1:n
vec(i)=isprime(i);
end
end
This gives me a logical vector, but I need a vector of all the prime numbers. How should I go about fixing my function?
0 commentaires
Réponse acceptée
infinity
le 23 Juin 2019
Hello,
The idea is that you use isprime to check is this prime or not.
Then, if it is the prime, you store the value of "i" (not isprime(i)) to the output.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Discrete Math 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!