Passed
Push — main ( 12d9ec...5a557a )
by Dylan
04:52
created

bigint.ts ➔ primes   C

Complexity

Conditions 9

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 9

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 6.6666
c 0
b 0
f 0
cc 9
crap 9
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