Question: What's wrong with this function and how do you fix it?

 

we were given a function that counts the number of primes among the arguments after the
rst and returns the result in the rst argument. When calling this, you must make sure
that the rst argument is a name. 

this is it.

cp := proc (YY) local count, i ;
print("nargs=", nargs, "args=", args) ;
count := 0 ;
for i from 2 to nargs do
if isprime(args[i]) then
count := count+1 ;
end if ;
print("i=", i, "count=", count) ;
end do ;
print("count=", count) ;
YY := count ;
end proc ;

EX: cp('noprimes',2,4,5,6,7,9,19)

and this works grand, but then we were given this function with slight adjustments to it and asked to fix it. the hints we were given were to try and forve evaluation at the right places.

This is the function we have to fix...

xcp := proc (count) local i;
print("nargs=", nargs, "args=", args) ;
count := 0 ;
for i from 2 to nargs do
if isprime(args[i]) then
count := count+1 ;
end if ;
print("i=", i, "count=", count) ;
end do ;
print("count=", count) ;
end proc ;

 

any help is appreciated!

Please Wait...