| Karim Belabas on Sun, 16 Sep 2012 09:12:22 +0200 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
| Re: forprime |
> On Sat, Sep 15, 2012 at 3:36 AM, <michel.marcus@free.fr> wrote:
> > forprime loops over prime numbers.
> >
> > is there a function that would loop over composite numbers ?
* Charles Greathouse [2012-09-15 09:41]:
> I typically write
>
> p=3; forprime(q=5, lim, for(n=p+1, q-1, /* your code here */); p=q)
It's not easy to do this properly in GP and the result is not that readable
[ N.B. the above loops through composites only up to precprime(lim) ]
I just committed a function forcomposite() to 'master', following the (new)
forprime() model:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
(09:05) gp > ??forcomposite
forcomposite(n = a,{b},seq):
Evaluates seq, where the formal variable n ranges over the composite numbers
between the non-negative real numbers a to b, including a and b if they are
composite. Nothing is done if a > b.
? forcomposite(p = 4, 10, print(p))
4
6
8
9
10
Omitting b means we will run through all composites >= a, starting an
infinite loop; it is expected that the user will break out of the loop himself
at some point, using break or return.
Note that the value of n cannot be modified within seq:
? forcomposite(n = 2, 10, n = [])
*** at top-level: forcomposite(n=2,10,n=[])
*** ^---
*** index read-only: was changed to [].
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Cheers,
K.B.
--
Karim Belabas, IMB (UMR 5251) Tel: (+33) (0)5 40 00 26 17
Universite Bordeaux 1 Fax: (+33) (0)5 40 00 69 50
351, cours de la Liberation http://www.math.u-bordeaux1.fr/~belabas/
F-33405 Talence (France) http://pari.math.u-bordeaux1.fr/ [PARI/GP]
`