Karim Belabas on Tue, 06 Feb 2024 20:05:31 +0100
|
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
- To: pari-users@pari.math.u-bordeaux.fr
- Subject: Re: nonprime(n)
- From: Karim Belabas <Karim.Belabas@math.u-bordeaux.fr>
- Date: Tue, 6 Feb 2024 20:05:26 +0100
- Arc-authentication-results: i=1; smail; arc=none
- Arc-message-signature: i=1; a=rsa-sha256; d=math.u-bordeaux.fr; s=openarc; t=1707246327; c=relaxed/relaxed; bh=sAJlb55FvZGIuguz3/VToEqRWXhPc5OZVgCEzWk1sFQ=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:Mail-Followup-To: References:MIME-Version:Content-Type:Content-Disposition: Content-Transfer-Encoding:In-Reply-To; b=JJS1R6Kow8b8jB40Zj8TdI5Vj9vEmz5AN8jx5OUc9/wX9iusj2JRd1PC+XKlekVnaKWiMkpQMUPkdrmsPEMoJkHN/yiqnQ/hsY1jnnVq6OUXh86PKoWE9VOSSR7JVevANQrjl+4+yZJBTOvLDG2yiUzyj5oGxrgYgv0LPlQAHiGAvo+SB+C9PxhvM+hazQ14IOfrm3Ktcw6vZw+7Fw5wWNEOb9Rl0A4RtMAEEFxztl4yEkgl0i3tspL2U2kV/SNtQlo7pYJ3JY5QLHGp2dZj3UVhg9Tn3GG5pXecqelmKLvhdvCvvEqxsy+6/k97gFkVLtjfeSj6NNrF+N5IB7/WjCjTzr/Yz5DIpTcFISU04egeAydD1XVWTiR0xFr1hk0icFsexVnaj1O0QlwrKWfvU0YAtwFFtRLzvfsKrQPGR8WP/luygaLUsi5fj7XHhpuKFu8cJ3MDU9LxUB6ztbCUgwWxYVyGBAhiyByBP8MXPzQgXNO8SxRBWWx0aRV14Rvwv6jWihc3RneWqmLvX1Iem8OiSlh2oALt19U4AQzhXOUpgJkKCjDZMsuqhgX0pwhSBjSYLrdkrztmwK1tI+hnTHxn1bjfXllqGX2EZs4C7GLNLDeSF4ueoOB3BHpcfhaG9e/dISIGHFfduhXYkrb36xeYIBXxaPFkiSBaFtpMfd8=
- Arc-seal: i=1; a=rsa-sha256; d=math.u-bordeaux.fr; s=openarc; t=1707246327; cv=none; b=qtIL82e+LAWqNoNmDTyLrLp2N85S2wrYxM6O4TVpXAx64g39xumP7pFMDihEYI/AZz/xd+LqTHj+vzTVzmPdM96+zyfFREGUodcAGWcq4y9WRl7T0PmoP39/G45UYrVLs3VdaOMVEnNIKiWOqVAW4H/EctssRKyYrXdkONaC6siQk2xVdL3VwehohCp19lK8BnSxmwnadP3670pLp9lVBmiaG9AZSSBLxFV2ijDlCRrtVTinA/eJXnG6VSUIQ2fwXtngiF2lO9qxb4r+knHUvHxVzIlQeX4KknVX6IgmQ9IoioewqbZB3LY05zFoZ3OcCcgxukc2Dhjc2gBBZdexYibgwt80S6h74p/xVXRBWr9ko8hjVlqA93HHTytMW+/J3pWphUVlO3FUg2T+dPv3MLwWIE7FmXskAE//Y/inEdtDfpJKagPyKmgp0U8GsCLvgUmIl5tLzJ5Ecllh6YqZRgBDl0pEIMGu9Igm2SiDTLYMcJrMufNQugqEoZj0hscImWmBb7Pj3pB3nofmxHL83koQy5iSgcIrZGezYJWFBBUy3iWCT/ObXq1TgoM9h+cilNXlUSA183s+ieL+oB+tGTJvYkMz5zijp5oZGDwx22Hr04tm/sBJk4I8w23dX5mu5j3Qecf2IdBWSI+wWB4f5NU8Y3a/0Q0wnVGS7eYQZtc=
- Authentication-results: smail; dmarc=none header.from=math.u-bordeaux.fr
- Authentication-results: smail; arc=none
- Delivery-date: Tue, 06 Feb 2024 20:05:31 +0100
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=math.u-bordeaux.fr; s=2022; t=1707246327; bh=sAJlb55FvZGIuguz3/VToEqRWXhPc5OZVgCEzWk1sFQ=; h=Date:From:To:Subject:References:In-Reply-To:From; b=NnsLFxNVsp5EXOjGP5K2OJRocbs6YwMCLuqRyMfZrCxdFzTqTvoLK2MefpBV2Ixd5 2Q4hd3Uo5sz2ARCaoicHmN2u3jDFCaNvDlSKoQ4M17fTS+bmWshECiz56BK2wZJ/hb tl+4waATNS9dDYkma4KIbcWLBjqIyERJTi235nGSXatruRW2rObjZoBjtdMukMbuBc NiLaqCDw3YtTdb4HIESKyy4wIqt9x4NsNvXtKAS42qq1SEDqm8i62Uu1X6fKxOcM9y Bq0u/6LjeACoOfHTl7NzwWdrx0wG8sUBQ1qyiCTyrG9TSb6DLNmFXU+xzo62Y3OvfH i3rKbvJv3GW8G5MvsLrN/6uH1NClyCQ50GuK2WhCGzwcadzl17sQ5eEP6GT9xDYvYP BXHCds3X5U1U5nd+JolCjmDf0fZl0fqF7oyytdmd0/j5AP8kqulTVfSAxWI1Opfxeu u6lolSR/eJRjRL+ty1+tYP1uEiw3BgEpOUW80cFNkLbQsWGNrtdK9jlF5OvXss4zai DgyHwqDBSDhaLEfJuhtwvPKul9eLw6GIJjzYfe5Qi52ICPFUepxd2PW487tsLczWMV mFcm2tAoPfBrM1Uii7BHaoYdiouGpNtokTq62+W5aNbSew9yd2HgE4wrCk+rJS9Pdt ewhUtuB+qHZ9/qJAb3MjJcEM=
- In-reply-to: <ZcJ1iDLeNjf7Hxtn@seventeen>
- Mail-followup-to: pari-users@pari.math.u-bordeaux.fr
- References: <cf9ba63c-87cc-4d0a-aded-8ac6ddb01c03@isolution.nl> <ca71cdf7-c294-42b0-985c-6044b9ff57c5@filmsons.com> <ZcEfRGHu02X3r4mS@seventeen> <6f7c35b5-4668-42e3-8943-55e67004a036@filmsons.com> <1afa6722-6270-4a8e-9585-a67e234c473f@normalesup.org> <881a66d9f2f99ff39a04c441e2d2204c@stamm-wilbrandt.de> <ZcJ1iDLeNjf7Hxtn@seventeen>
* Bill Allombert [2024-02-06 19:08]:
> On Tue, Feb 06, 2024 at 06:14:47PM +0100, hermann@stamm-wilbrandt.de wrote:
> > On 2024-02-06 08:40, Aurel Page wrote:
> > > 257M is overkill.
> > >
> > > $ gp -p4M
> > > ? for(n=1,2^18, prime(n));
> > > time = 1,315 ms.
> > >
> > > We have all the necessary primes:
> > > ? prime(2^18)
> > > % = 3681131
> > >
> > > Cheers,
> > > Aurel
> > >
> > And why is forprime() so fast without -p...?
> >
> > hermann@7950x:~$ gp -q
> > ? c=0;forprime(p=2,oo,c+=1;if(c==2^18,print(p);break()));
> > 3681131
> > ? ##
> > *** last result: cpu time 60 ms, real time 60 ms.
>
> Compared to
> ? prime(2^18)
> %1 = 3681131
> *** last result computed in 5 ms.
> this is not so fast.
>
> the issue is that "for(n=1,2^18, prime(n))" does
>
> for(n=1,2^18, c=0;forprime(p=2,oo,c+=1;if(c==n,break())))
>
> which is a very inefficient way to do that computation.
I agree with the conclusion, but the algorithm is a little less silly.
We start from a quick search in a small table of [p_i, \pi(p_i)],
for some quickly increasing sequence of primes p_i. We then start the
forprime loop at the largest p_i less than our target: this gains a
constant factor. A larger table would mean a larger gain (and more
memory used).
Note that the initial part of forprime iterates over a bounded
number of differences of primes. We could switch to storing actual
primes (the table would only be 8 times larger), but lots of functions
would be immensely sped up as long as we remain in the table. For
instance prime(n) would become O(1) time and primepi(x) would use O(log x)
time. Etc.
Cheers,
K.B.
--
Pr. Karim Belabas, U. Bordeaux, Vice-président en charge du Numérique
Institut de Mathématiques de Bordeaux UMR 5251 - (+33) 05 40 00 29 77
http://www.math.u-bordeaux.fr/~kbelabas/