Total Complexity | 12 |
Complexity/F | 12 |
Lines of Code | 35 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Coverage | 100% |
Changes | 0 |
1 | /** |
||
2 | * Find the greatest common denominator of the two numbers. |
||
3 | */ |
||
4 | 5 | export const gcd = (a: bigint, b: bigint): bigint => { |
|
5 | 244 | if (b === 1n || a === 1n) { |
|
6 | 124 | return 1n |
|
7 | } |
||
8 | 120 | while (b !== 0n) { |
|
9 | 2847 | [a, b] = [b, a % b] |
|
10 | } |
||
11 | 120 | return a < 0n ? -a : a |
|
12 | } |
||
13 | |||
14 | /** |
||
15 | * Returns true if the number is prime. |
||
16 | */ |
||
17 | 5 | export const isPrime = (n: bigint): boolean => { |
|
18 | 16 | if (n === 1n) return false |
|
19 | 15 | for (let i = 2n; i*i <= n; i++) { |
|
20 | 100 | if (n%i === 0n) return false |
|
21 | } |
||
22 | 8 | return true |
|
23 | } |
||
24 | |||
25 | /** |
||
26 | * Yields the prime numbers. |
||
27 | */ |
||
28 | 5 | export function* primes(): Generator<bigint> { |
|
29 | 1 | for (let n = 2n; true; n++) { |
|
30 | 10 | if (isPrime(n)) { |
|
31 | 5 | yield n |
|
32 | } |
||
33 | } |
||
34 | } |
||
35 |