|
1 | | -There are many algorithms for this task. |
| 1 | +Yra daug algoritmų šiai užduočiai. |
2 | 2 |
|
3 | | -Let's use a nested loop: |
| 3 | +Panaudokime matrioškinį ciklą: |
4 | 4 |
|
5 | 5 | ```js |
6 | | -For each i in the interval { |
7 | | - check if i has a divisor from 1..i |
8 | | - if yes => the value is not a prime |
9 | | - if no => the value is a prime, show it |
| 6 | +Kiekvienam i intervale { |
| 7 | + patikrinkite ar i turi daliklį iš 1..i |
| 8 | + jeigu taip => vertė nėra pirminius skaičius |
| 9 | + jeigu ne => vertė yra pirminis skaičius, parodykite ją |
10 | 10 | } |
11 | 11 | ``` |
12 | 12 |
|
13 | | -The code using a label: |
| 13 | +Kodas naudojant etiketę: |
14 | 14 |
|
15 | 15 | ```js run |
16 | 16 | let n = 10; |
17 | 17 |
|
18 | 18 | nextPrime: |
19 | | -for (let i = 2; i <= n; i++) { // for each i... |
| 19 | +for (let i = 2; i <= n; i++) { // kiekvienam i... |
20 | 20 |
|
21 | | - for (let j = 2; j < i; j++) { // look for a divisor.. |
22 | | - if (i % j == 0) continue nextPrime; // not a prime, go next i |
| 21 | + for (let j = 2; j < i; j++) { // ieškoti daliklio.. |
| 22 | + if (i % j == 0) tęsti nextPrime; // ne pirminis skaičius, eiti prie sekančio i |
23 | 23 | } |
24 | 24 |
|
25 | | - alert( i ); // a prime |
| 25 | + alert( i ); // pirminis |
26 | 26 | } |
27 | 27 | ``` |
28 | 28 |
|
29 | | -There's a lot of space to optimize it. For instance, we could look for the divisors from `2` to square root of `i`. But anyway, if we want to be really efficient for large intervals, we need to change the approach and rely on advanced maths and complex algorithms like [Quadratic sieve](https://en.wikipedia.org/wiki/Quadratic_sieve), [General number field sieve](https://en.wikipedia.org/wiki/General_number_field_sieve) etc. |
| 29 | +Yra daug vietos, kur galima jį optimizuoti. Pavyzdžiui, galėtume ieškoti daliklių nuo `2` iki `i` šaknies. Bet kokiu atveju, jeigu norime būti efektyvūs dideliems intervalams, reikia pakeisti priėjimo būdus ir pasitikėti pažengusia matematika ir sudėtingais algoritmais kaip [Quadratic sieve](https://en.wikipedia.org/wiki/Quadratic_sieve), [General number field sieve](https://en.wikipedia.org/wiki/General_number_field_sieve) etc. |
0 commit comments