|
| 1 | +# Challenge Description and Solution |
| 2 | + |
| 3 | +## English Version |
| 4 | + |
| 5 | +### Challenge Description |
| 6 | +Implement a function that determines if a number is prime. Pay attention to edge cases such as negative values and the number 1. |
| 7 | + |
| 8 | +### Code Explanation |
| 9 | +The `isPrime` function checks if a number `n` is prime. It first excludes numbers less than or equal to 1, which are not prime. Then, it considers 2 and 3 as prime numbers. Next, it eliminates multiples of 2 and 3. Finally, it uses a loop that checks divisors starting from 5, skipping multiples of 6, to optimize the search. If a divisor is found, it returns `false`; otherwise, it returns `true`. |
| 10 | + |
| 11 | +### Relevant Code Snippet |
| 12 | + |
| 13 | +```javascript |
| 14 | +function isPrime(n) { |
| 15 | + if (n <= 1) { |
| 16 | + return false; |
| 17 | + } |
| 18 | + if (n <= 3) { |
| 19 | + return true; |
| 20 | + } |
| 21 | + if (n % 2 === 0 || n % 3 === 0) { |
| 22 | + return false; |
| 23 | + } |
| 24 | + let i = 5; |
| 25 | + while (i * i <= n) { |
| 26 | + if (n % i === 0 || n % (i + 2) === 0) { |
| 27 | + return false; |
| 28 | + } |
| 29 | + i += 6; |
| 30 | + } |
| 31 | + return true; |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +### Example Usage |
| 36 | + |
| 37 | +```javascript |
| 38 | +const testNumbers = [-1, 0, 1, 2, 3, 4, 5, 29, 35]; |
| 39 | +testNumbers.forEach(num => { |
| 40 | + console.log(`${num} is prime: ${isPrime(num)}`); |
| 41 | +}); |
| 42 | +``` |
| 43 | + |
| 44 | +--- |
| 45 | + |
| 46 | +## Versión en Español |
| 47 | + |
| 48 | +### Descripción del Reto |
| 49 | +Implementa una función que determine si un número es primo. Presta atención a casos especiales como valores negativos y el número 1. |
| 50 | + |
| 51 | +### Explicación del Código |
| 52 | +La función `isPrime` verifica si un número `n` es primo. Primero descarta números menores o iguales a 1, que no son primos. Luego, considera los números 2 y 3 como primos. Después, elimina múltiplos de 2 y 3. Finalmente, utiliza un bucle que verifica divisores desde 5 en adelante, saltando múltiplos de 6, para optimizar la búsqueda. Si encuentra un divisor, retorna `false`; si no, retorna `true`. |
| 53 | + |
| 54 | +### Fragmento de Código Relevante |
| 55 | + |
| 56 | +```javascript |
| 57 | +function isPrime(n) { |
| 58 | + if (n <= 1) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + if (n <= 3) { |
| 62 | + return true; |
| 63 | + } |
| 64 | + if (n % 2 === 0 || n % 3 === 0) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + let i = 5; |
| 68 | + while (i * i <= n) { |
| 69 | + if (n % i === 0 || n % (i + 2) === 0) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + i += 6; |
| 73 | + } |
| 74 | + return true; |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +### Ejemplo de Uso |
| 79 | + |
| 80 | +```javascript |
| 81 | +const testNumbers = [-1, 0, 1, 2, 3, 4, 5, 29, 35]; |
| 82 | +testNumbers.forEach(num => { |
| 83 | + console.log(`${num} es primo: ${isPrime(num)}`); |
| 84 | +}); |
0 commit comments