Skip to content

Commit

Permalink
Make primeRange start parameter optional
Browse files Browse the repository at this point in the history
Fix broken documentation.
  • Loading branch information
frostburn committed Apr 30, 2024
1 parent 0cfd1ee commit df0d293
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/__tests__/primes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ describe('Prime range generator', () => {
expect(primeRange(0, 4)).toEqual([2, 3, 5, 7]);
});

it('produces the first 4 primes (start is optional)', () => {
expect(primeRange(4)).toEqual([2, 3, 5, 7]);
});

it('produces 5 primes starting from the 999th odd prime', () => {
expect(primeRange(999, 999 + 5)).toEqual([7919, 7927, 7933, 7937, 7949]);
});
Expand Down
8 changes: 6 additions & 2 deletions src/primes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,16 @@ export function primes(start: number, end?: number) {
}

/**
* Obtain a range of odd primes starting at ordinal the given ordinal.
* Obtain a range of primes starting at the given ordinal. Prime 2 has ordinal 0.
* @param start 1-based ordinal of the nth odd prime to start from, or zero to include prime two.
* @param end Range end. `end - start` elements are returned.
* @returns The primes in the range.
*/
export function primeRange(start: number, end: number) {
export function primeRange(start: number, end?: number) {
if (end === undefined) {
end = start;
start = 0;
}
start = Math.round(Math.max(0, start));
end = Math.round(end);
if (end <= start) {
Expand Down

0 comments on commit df0d293

Please sign in to comment.