Maple isprime function

407 views Asked by At

I'm trying to create a program called "composites" using maple that accepts a user input of n and prints out the first n composite numbers using the isprime function although I have no idea as to how I would go about doing this as I have very little experience with maple. Here is my current code, although it is probably horribly incorrect.

composites := proc (n) 
local i, L; 
L := []; 
for i to n do if isprime(n) then L := [op(L), i] end if end do; 
L 
end proc

Any help would be appreciated

Many Thanks

1

There are 1 answers

0
acer On BEST ANSWER

You are close. You have to test isprime(i) and not isprime(n), since it is i rather than n which is changing.

Also, you only want to augment list L whenever isprime returns false. So you want not isprime(i) in the conditional.

composites := proc (n) 
local i, L; 
L := []; 
for i from 4 to n do
   if not isprime(i) then
      L := [op(L), i];
   end if;
end do; 
op(L);
end proc:

composites( 18 );

                 4, 6, 8, 9, 10, 12, 14, 15, 16, 18

You could also code it in a more functional manner, eg,

composites:=proc(n::posint)
   local i;
   seq( `if`(isprime(i), NULL, i), i=4..n );
end proc:

composites( 18 );

                 4, 6, 8, 9, 10, 12, 14, 15, 16, 18