12345678910111213141516171819202122232425262728 |
- enum State {
- Maybe = 0,
- Prime,
- NotPrime,
- }
- export function primes(limit: number): number[] {
- const all: number[] = Array(limit + 1).fill(State.Maybe);
- all[0] = State.NotPrime;
- all[1] = State.NotPrime;
- for (let i = 2; i <= limit; i++) {
- if (all[i] === State.NotPrime) {
- continue;
- }
- all[i] = State.Prime;
- for (let j = 2; j <= limit / i; j++) {
- all[i * j] = State.NotPrime;
- }
- }
- const res: number[] = [];
- let property: keyof typeof all;
- for (property in all) {
- if (all[property] === State.Prime) {
- res.push(Number(property));
- }
- }
- return res;
- }
|