Bill Allombert on Sun, 14 Oct 2018 19:31:49 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: How to do Cartesian products in GP


On Sun, Oct 14, 2018 at 03:56:21PM +0000, Jacques Gélinas wrote:
> Here is an example of the use of the outer product for sieving a list of prime numbers,
> from the APL program (~R∊R∘.×R)/R←1↓ιR    or   (~R<-Ro.xR)/R<-1 drop iota R
> which is interpeted after reading from right to left
> https://en.wikipedia.org/wiki/APL_(programming_language)#Prime_numbers
> 
> R = 10;
> R = [2..R];                 \\ R<-1 drop iota R
> RR = [ a*b | a<-R; b<-R ];  \\ Ro.xR
> setminus(R,Set(RR))         \\ Set(RR) needed here instead of RR

You can actually replace the two last lines by

setminus(R, setbinop((x,y)->x*y,R))

Also note this is not a sieve!
The whole point of a sieve is to replace multiplications/divisions by
additions/substaction.
(instead of 
x=a*1; x=a*2; x=a*3; ...
one should do
x=a; x+=a; x+=a
)

Cheers,
Bill.