Maple a Write a procedure primesum whose input is a positive

Maple
a) Write a procedure, primesum, whose input is a positive integer n and whose output is the sum of all primes  p such that p <= n. You may use the built-in procedure isprime. Test the procedure for n= 2, 4, 10, and 1000.

primesum:=proc(n)
local SUM, k;
SUM:=0;
for k from 1 to n do
if isprime(k)=true then
SUM:=SUM + k;
end if;
end do;
end proc:
primesum(12);

I did this but no result comes out when the number input is not prime. How do I fix this? I have the same problem with the next one.

b) Write a procedure, primeset, whose input is a positive integer n and whose output is the set of all primes p such that p <= n. You may use the built-in procedure isprime. Test the procedure for n = 2, 4, 10, and 1000.b) Write a procedure, primeset, whose input is a positive integer n and whose output is the set of all primes p such that p <= n. You may use the built-in procedure isprime. Test the procedure for n = 2, 4, 10, and 1000.

primeset:=proc(n)
local SUM, S,k;
S:={};
for k from 1 to n do
if isprime(n)=true then
S:=S union {k};
end if;
end do;
end proc:
primeset(5);

Solution

Hi,

According the question you asked, the logic you wrote wont validate right unless you put the condition that the SUM < n as we have a condition that sum p <= n in the question you mentioned. So along with the condition that

If isprime(k). Let us put a condition saying SUM<n.

So the program would be put as

primesum:=proc(n)
local SUM, k;
SUM:=0;
for k from 1 to n do
if (isprime(k)=true and SUM<n) then
SUM:=SUM + k;
end if;
end do;
end proc:
primesum(12);

Which then validates the sum of all the prime numbers less than or equal to that mentioned number holds true which is required as per the question.

There is no other reason why the program wont work for non-prime numbers.

Thank you.

Maple a) Write a procedure, primesum, whose input is a positive integer n and whose output is the sum of all primes p such that p <= n. You may use the built

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site