|
| 1 | +## Python implementation |
| 2 | + |
| 3 | +```python |
| 4 | +def is_prime(n): |
| 5 | + return n > 1 and all(n % i for i in range(2, int(n**0.5) + 1)) |
| 6 | + |
| 7 | +# Example usage: |
| 8 | +print(is_prime(-5)) # False |
| 9 | +print(is_prime(17)) # True |
| 10 | +``` |
| 11 | + |
| 12 | +## TypeScript implementation |
| 13 | + |
| 14 | +```typescript |
| 15 | +function isPrime(n: number): boolean { |
| 16 | + return n > 1 && Array.from({ length: Math.floor(Math.sqrt(n)) - 1 }, (_, i) => i + 2) |
| 17 | + .every(i => n % i !== 0); |
| 18 | +} |
| 19 | + |
| 20 | +// Example usage: |
| 21 | +console.log(isPrime(17)); // true |
| 22 | +console.log(isPrime(18)); // false |
| 23 | +``` |
| 24 | + |
| 25 | +## Similarities vs Differences |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +### Similarities |
| 30 | +- They both use return `n > 1`, which helps to ensure that numbers less than or equal to 1 are not prime. |
| 31 | +- Both languages use `i` as the variable for testing divisors of `n`. |
| 32 | +- While the methods are different, they both have a way of creating a loop or a range of numbers. |
| 33 | + |
| 34 | + |
| 35 | +### Differences |
| 36 | +- The comment style in both languages is different. Python uses hashtags while TypeScript uses slashes. |
| 37 | +- Python uses `n**0.5`, while TypeScript uses `Math.sqrt(n)` to call the sqaure root. |
| 38 | +- Python has range , which produces a sequence of integers. However, while TypeScript does not have `range` built in, it does have `Array`, which you can use to generate the sequence. |
| 39 | + |
| 40 | +## Quiz Instructions |
| 41 | + |
| 42 | +### Python Quiz |
| 43 | + |
| 44 | +1. Open the project in **VS Code** |
| 45 | +2. Navigate to the [prime_python] directory. |
| 46 | +```bash |
| 47 | +cd lesson_04/trinitiejackson/prime_python |
| 48 | +``` |
| 49 | +3. Check to make sure you have Python 3 installed |
| 50 | +```bash |
| 51 | +python3 --version |
| 52 | +``` |
| 53 | +If not, do: |
| 54 | +```bash |
| 55 | +brew install python3 |
| 56 | +``` |
| 57 | +3. Open the [prime_test] file. |
| 58 | +4. Update the code to determine whether a number is prime. |
| 59 | +5. When ready to test, run the following command in the [prime_python] directory using the terminal: |
| 60 | +```bash |
| 61 | +python3 prime_test.py |
| 62 | +``` |
| 63 | +### TypeScript Quiz |
| 64 | + |
| 65 | +1. Make sure to sync your fork to pull in the latest changes. |
| 66 | +2. Open the project in **VS Code**. |
| 67 | +3. Navigate to the [prime_typescript] directory and install the required dependencies. |
| 68 | +```bash |
| 69 | +cd lesson_04/trinitiejackson/prime_typescript |
| 70 | +npm install |
| 71 | +``` |
| 72 | +4. Open the [prime_test.ts] file located in the [prime_typescript/src/] directory. |
| 73 | +5. Update the code to determine whether a number is prime. |
| 74 | +6. When ready to test, run the following command in the terminal: |
| 75 | +```bash |
| 76 | +npm test |
| 77 | +``` |
0 commit comments