π
<-
Chat plein-écran
[^]

fonction isprime et crible d'ératosthène

Pour le TI-Basic sur Nspire

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

Message non lude Adriweb » 01 Fév 2021, 18:04

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

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...)
Avatar de l’utilisateur
AdriwebAdmin
Niveau 16: CC2 (Commandeur des Calculatrices)
Niveau 16: CC2 (Commandeur des Calculatrices)
Prochain niv.: 80.3%
 
Messages: 14617
Images: 1218
Inscription: 01 Juin 2007, 00:00
Localisation: France
Genre: Homme
Calculatrice(s):
MyCalcs profile
Twitter/X: adriweb
GitHub: adriweb

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

Message non lude parisse » 01 Fév 2021, 19:58

Heneos a écrit: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.
Avatar de l’utilisateur
parisseVIP++
Niveau 12: CP (Calculatrice sur Pattes)
Niveau 12: CP (Calculatrice sur Pattes)
Prochain niv.: 78%
 
Messages: 3511
Inscription: 13 Déc 2013, 16:35
Genre: Non spécifié
Calculatrice(s):
MyCalcs profile

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

Message non lude Heneos » 01 Fév 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: Tout sélectionner
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.
Avatar de l’utilisateur
Heneos
Niveau 1: MD (Membre Débutant)
Niveau 1: MD (Membre Débutant)
Prochain niv.: 60%
 
Messages: 4
Inscription: 18 Juil 2018, 10:17
Genre: Non spécifié
Calculatrice(s):
MyCalcs profile
Classe: University
GitHub: HeNeos

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

Message non lude Heneos » 01 Fév 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
Avatar de l’utilisateur
Heneos
Niveau 1: MD (Membre Débutant)
Niveau 1: MD (Membre Débutant)
Prochain niv.: 60%
 
Messages: 4
Inscription: 18 Juil 2018, 10:17
Genre: Non spécifié
Calculatrice(s):
MyCalcs profile
Classe: University
GitHub: HeNeos

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

Message non lude parisse » 01 Fév 2021, 21:44

Heneos a écrit:
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.
Avatar de l’utilisateur
parisseVIP++
Niveau 12: CP (Calculatrice sur Pattes)
Niveau 12: CP (Calculatrice sur Pattes)
Prochain niv.: 78%
 
Messages: 3511
Inscription: 13 Déc 2013, 16:35
Genre: Non spécifié
Calculatrice(s):
MyCalcs profile

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

Message non lude parisse » 01 Fév 2021, 21:47

Heneos a écrit: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...).
Avatar de l’utilisateur
parisseVIP++
Niveau 12: CP (Calculatrice sur Pattes)
Niveau 12: CP (Calculatrice sur Pattes)
Prochain niv.: 78%
 
Messages: 3511
Inscription: 13 Déc 2013, 16:35
Genre: Non spécifié
Calculatrice(s):
MyCalcs profile

Précédente

Retourner vers Nspire-Basic

Qui est en ligne

Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 14 invités

-
Rechercher
-
Social TI-Planet
-
Sujets à la une
Comparaisons des meilleurs prix pour acheter sa calculatrice !
Aidez la communauté à documenter les révisions matérielles en listant vos calculatrices graphiques !
Phi NumWorks jailbreak
123
-
Faire un don / Premium
Pour plus de concours, de lots, de tests, nous aider à payer le serveur et les domaines...
Faire un don
Découvrez les avantages d'un compte donateur !
JoinRejoignez the donors and/or premium!les donateurs et/ou premium !


Partenaires et pub
Notre partenaire Jarrety Calculatrices à acheter chez Calcuso
-
Stats.
1280 utilisateurs:
>1264 invités
>11 membres
>5 robots
Record simultané (sur 6 mois):
6892 utilisateurs (le 07/06/2017)
-
Autres sites intéressants
Texas Instruments Education
Global | France
 (English / Français)
Banque de programmes TI
ticalc.org
 (English)
La communauté TI-82
tout82.free.fr
 (Français)