π
<-

fonction isprime et crible d'ératosthène

Pour le TI-Basic sur Nspire

Re: fonction isprime et crible d'ératosthène

Unread postby Adriweb » 01 Feb 2021, 18:04

can't you just reserve a bigger list first so that it doesn't need to grow?

MyCalcs: Help the community's calculator documentations by filling out your calculators info!
MyCalcs: Aidez la communauté à documenter les calculatrices en donnant des infos sur vos calculatrices !
Inspired-Lua.org: All about TI-Nspire Lua programming (tutorials, wiki/docs...)
My calculator programs
Mes programmes pour calculatrices
User avatar
AdriwebAdmin
Niveau 16: CC2 (Commandeur des Calculatrices)
Niveau 16: CC2 (Commandeur des Calculatrices)
Level up: 79.2%
 
Posts: 14779
Images: 1123
Joined: 01 Jun 2007, 00:00
Location: France
Gender: Male
Calculator(s):
MyCalcs profile
Twitter: adriweb
GitHub: adriweb

Re: fonction isprime et crible d'ératosthène

Unread postby parisse » 01 Feb 2021, 19:58

Heneos wrote:Thanks, good answer, I remember that I tried with C and ndless, but it was a really headache (I feel more comfortable with C++ and STL), so maybe my implementation was not efficient.

C++ and STL are supported by ndless today.

For 20000, sieve runs in around 5 seconds and naive runs in 1.4 second :(

20000 is really small, trial division will need at most 34 divisions to check primality (and most of the time 5 divisions will return false). FYI, with KhiCAS, ithprime(1000000) returns 15485863 in a few seconds on a CX/CXII, it does a sieve with about 1000 times more entries than yours to find the 1000000th prime.

You can't deduce much comparing algorithms with TI Basic because it's interpreted and therefore n will be small. And trying to optimize with an interpreted language will not help much, in fact I would not recommend to do that if you want to really optimize, therefore with a compiled language, because the good pratices are sometimes opposite.
User avatar
parisseVIP++
Niveau 12: CP (Calculatrice sur Pattes)
Niveau 12: CP (Calculatrice sur Pattes)
Level up: 88.2%
 
Posts: 3700
Joined: 13 Dec 2013, 16:35
Gender: Not specified
Calculator(s):
MyCalcs profile

Re: fonction isprime et crible d'ératosthène

Unread postby Heneos » 01 Feb 2021, 21:19

C++ and STL are supported by ndless today.

Could you give me a link or tutorial about C ++ in ndless?
20000 is really small, trial division will need at most 34 divisions to check primality (and most of the time 5 divisions will return false). FYI, with KhiCAS, ithprime(1000000) returns 15485863 in a few seconds on a CX/CXII, it does a sieve with about 1000 times more entries than yours to find the 1000000th prime.

Well, this is because Sieve has a bigger complexity than primality test. But for listing primes in a range, sieve is an efficient method. You can realize this if you use:
Code: Select all
for(int i=1; i<n; i++){
if(isPrime(i)) printf("%d is prime",i);
}

the time complexity is
$mathjax$O(n\times \mathrm{isPrime()})$mathjax$
.
But in Sieve the complexity is
$mathjax$O(n\log(\log(n)))$mathjax$
.
Miller-Rabin primality test has a complexity around
$mathjax$O(\log(n)^{3})$mathjax$
.
So isPrime is more efficient than Miller-Rabin, but as you said, it is because it is interpreted.
User avatar
Heneos
Niveau 1: MD (Membre Débutant)
Niveau 1: MD (Membre Débutant)
Level up: 60%
 
Posts: 4
Joined: 18 Jul 2018, 10:17
Gender: Not specified
Calculator(s):
MyCalcs profile
Class: University
GitHub: HeNeos

Re: fonction isprime et crible d'ératosthène

Unread postby Heneos » 01 Feb 2021, 21:25

with KhiCAS, ithprime(1000000) returns 15485863 in a few seconds on a CX/CXII, it does a sieve with about 1000 times more entries than yours to find the 1000000th prime.

Meissel-Lehmer algorithm provides a more efficient way to determine the ith prime number, but it's quite different than Sieve or Primality Test.
Image
User avatar
Heneos
Niveau 1: MD (Membre Débutant)
Niveau 1: MD (Membre Débutant)
Level up: 60%
 
Posts: 4
Joined: 18 Jul 2018, 10:17
Gender: Not specified
Calculator(s):
MyCalcs profile
Class: University
GitHub: HeNeos

Re: fonction isprime et crible d'ératosthène

Unread postby parisse » 01 Feb 2021, 21:44

Heneos wrote:
C++ and STL are supported by ndless today.

Could you give me a link or tutorial about C ++ in ndless?

Do the same as for a C program, but run nspire-g++ instead of nspire-gcc.

So isPrime is more efficient than Miller-Rabin, but as you said, it is because it is interpreted.

Maybe you are calling Miller-Rabin without doing trial division first. And otherwise, trial division code is way faster compiled versus interpreted.
User avatar
parisseVIP++
Niveau 12: CP (Calculatrice sur Pattes)
Niveau 12: CP (Calculatrice sur Pattes)
Level up: 88.2%
 
Posts: 3700
Joined: 13 Dec 2013, 16:35
Gender: Not specified
Calculator(s):
MyCalcs profile

Re: fonction isprime et crible d'ératosthène

Unread postby parisse » 01 Feb 2021, 21:47

Heneos wrote:Meissel-Lehmer algorithm provides a more efficient way to determine the ith prime number, but it's quite different than Sieve or Primality Test.

Like trial division vs Miller-Rabin, sieving *is* efficient for finding the ith prime for not too large values of n (remember we are on a calculator...).
User avatar
parisseVIP++
Niveau 12: CP (Calculatrice sur Pattes)
Niveau 12: CP (Calculatrice sur Pattes)
Level up: 88.2%
 
Posts: 3700
Joined: 13 Dec 2013, 16:35
Gender: Not specified
Calculator(s):
MyCalcs profile

Previous

Return to Nspire-Basic

Who is online

Users browsing this forum: ClaudeBot [spider] and 3 guests

-
Search
-
Social TI-Planet
-
Featured topics
Grand Concours 2024-2025 - Programmation Python
Comparaisons des meilleurs prix pour acheter sa calculatrice !
"1 calculatrice pour tous", le programme solidaire de Texas Instruments. Reçois gratuitement et sans aucune obligation d'achat, 5 calculatrices couleur programmables en Python à donner aux élèves les plus nécessiteux de ton lycée. Tu peux recevoir au choix 5 TI-82 Advanced Edition Python ou bien 5 TI-83 Premium CE Edition Python.
Enseignant(e), reçois gratuitement 1 exemplaire de test de la TI-82 Advanced Edition Python. À demander d'ici le 31 décembre 2024.
Aidez la communauté à documenter les révisions matérielles en listant vos calculatrices graphiques !
12345
-
Donations / Premium
For more contests, prizes, reviews, helping us pay the server and domains...
Donate
Discover the the advantages of a donor account !
JoinRejoignez the donors and/or premium!les donateurs et/ou premium !


Partner and ad
Notre partenaire Jarrety Calculatrices à acheter chez Calcuso
-
Stats.
1059 utilisateurs:
>1021 invités
>30 membres
>8 robots
Record simultané (sur 6 mois):
6892 utilisateurs (le 07/06/2017)
-
Other interesting websites
Texas Instruments Education
Global | France
 (English / Français)
Banque de programmes TI
ticalc.org
 (English)
La communauté TI-82
tout82.free.fr
 (Français)