Question

Use MATLAB to program the following:

9. The MATLAB built-in function primes (x) finds all the prime numbers less than x. Write a M-file function addupprime that will sum up all the prime numbers of an input number x. Run and show 2 test cases to demonstrate that your function file is correct.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

This code should be saved with filename as addupprime only

Code :-

function sum_prime = addupprime(x)
    prime_num = primes(x); % finding all prime number
    sum_prime = 0; % intialization of sum_prime
    for i = prime_num     % for loop for add all prime number
      sum_prime = sum_prime + i;
    end
end

Output: -

>> addupprime(50)
ans = 328
>> addupprime(100)
ans = 1060
>>

Add a comment
Know the answer?
Add Answer to:
Use MATLAB to program the following: 9. The MATLAB built-in function primes (x) finds all the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT