Published
Edited
Mar 19, 2019
Importers
Insert cell
Insert cell
Insert cell
function* A000002(n=1) {
let x = -1
let y = x
// TODO fast forward to index position n...
const out = [2, 1]
do {
yield out[x&1]
const f = y &~ (y+1)
x ^= f
y = (y+1) | (f & (x>>1))
} while (++n < Number.MAX_SAFE_INTEGER)
}
Insert cell
Insert cell
assert(A000002_expected, take(A000002_expected.length, A000002()))
Insert cell
Insert cell
function* A000004(n=0) {
do {
yield 0
} while (++n <= Number.MAX_SAFE_INTEGER)
}
Insert cell
Insert cell
assert(A000004_expected, take(A000004_expected.length, A000004()))
Insert cell
Insert cell
function* A000005(n=1) {
// TODO quite a bit of room to optimize
do {
if (n==1) {
yield 1
continue
}
const pp = primePowers(n)
let v = 1
for (let [p, e] of pp) {
v *= (e + 1)
}
yield v
} while (++n <= Number.MAX_SAFE_INTEGER)
}
Insert cell
Insert cell
assert(A000005_expected, take(A000005_expected.length, A000005()))
Insert cell
Insert cell
function* A000006(n=1) {
// TODO seek to index position n
for (let p of primes()) {
yield Math.floor(Math.sqrt(p))
}
}
Insert cell
Insert cell
assert(A000006_expected, take(A000006_expected.length, A000006()))
Insert cell
Insert cell
function* A000007(n=0) {
do {
yield (n ? 0 : 1)
} while (++n <= Number.MAX_SAFE_INTEGER)
}
Insert cell
Insert cell
assert(A000007_expected, take(A000007_expected.length, A000007()))
Insert cell
Insert cell
function* A000010(n=1) {
do {
if (n==1) {
yield 1
continue
}
let v = n
// loop over distinct factors
for (let p of new Set([...factors(n)])) {
v *= p-1
v /= p
}
yield v
} while (++n <= Number.MAX_SAFE_INTEGER)
}
Insert cell
Insert cell
assert(A000010_expected, take(A000010_expected.length, A000010()))
Insert cell
Insert cell
function* A000012(n=0) {
// TODO why does OEIS have these indices running backward?
do {
yield 1
} while (++n <= Number.MAX_SAFE_INTEGER)
}
Insert cell
Insert cell
function* A000027(n=1) {
do {
yield n
} while (++n <= Number.MAX_SAFE_INTEGER)
}
Insert cell
Insert cell
assert(A000027_expected, take(A000027_expected.length, A000027()))
Insert cell
Insert cell
function* A000037(n=1) {
// while (n <= Number.MAX_SAFE_INTEGER) ?
do {
yield n + Math.floor(1/2 + Math.sqrt(n))
} while (++n <= Number.MAX_SAFE_INTEGER)
}
Insert cell
Insert cell
assert(A000037_expected, take(A000037_expected.length, A000037()))
Insert cell
assert(take(1,A000037(1234))[0], 1269) // value at 1,234th index position of sequence
Insert cell
assert(A000037(1234).next().value, 1269)
Insert cell
Insert cell
A000040 = primes // TODO take an n
Insert cell
Insert cell
assert(A000040_expected, [...itertools.takewhile(A000040(), (p) => p <= 271)])
Insert cell
Insert cell
function* A000042(n=1, b='') {
// obviously this falls down fast without bigints...
// could force strings here instead, though prob want a custom numeric type anyway
// opting into custom type accomplished with second "base" arg
do {
if (typeof b === 'string') {
yield Array.from({ length: n }).fill('1').join('')
} else if (typeof b === 'number') {
// base "b" repdigit
// TODO BigInt polyfill?
yield (b**n - 1) / (b - 1)
}
} while (++n <= Number.MAX_SAFE_INTEGER)
}
Insert cell
Insert cell
assert(A000042_expected, take(A000042_expected.length, A000042()))
Insert cell
assert(A000042(5,10).next().value, 11111)
Insert cell
assert(A000042(5,2).next().value, parseInt('11111',2))
Insert cell
Insert cell
function* A000217(n=0) {
// TODO support characters over an alphabet?
do {
yield choose(n+1,2)
} while (++n <= Number.MAX_SAFE_INTEGER)
}
Insert cell
Insert cell
Insert cell
Insert cell
function* A000290(n=0) {
// TODO support characters over an alphabet?
do {
yield n*n
} while (++n <= Number.MAX_SAFE_INTEGER)
}
Insert cell
Insert cell
assert(A000290_expected, take(A000290_expected.length, A000290()))
Insert cell
Insert cell
function* A006881(n=1) {
// seek to first value... TODO should we `yield null` for each place?
if (n < 6) n = 6
do {
// TODO base generator to emit pairs of factors, can be composed back together
if (n % 2 == 0) {
// for singly even n, divide out by 2 and run a prime test
if (n % 4 == 2 && isPrime(n/2)) yield n
} else {
// seek out first prime factor, test for primality of residue
// TODO we can be smarter than this by doubly iterating primes
const f1 = factors(n).next().value
const f2 = n / f1
if (f1 != f2 && isPrime(f2)) yield n
}
} while (++n <= Number.MAX_SAFE_INTEGER)
}
Insert cell
Insert cell
Insert cell
Insert cell
take = (n, iterable) => [...itertools.itake(n, itertools.icompact(iterable))]
Insert cell
function* factors(n) {
let x = Math.max(1, Math.floor(n)) || 1
let i = 0
let q
for (const p of primes()) {
if (!(p * p <= x)) break;
while ((q = x / p) > 1 && q === Math.floor(q)) {
x = q
yield p
}
if (++i % 1000 === 0) {
yield
}
}
yield x
}
Insert cell
primePowers = (n) => countValues(itertools.groupby([...itertools.icompact(factors(n))]))
Insert cell
countValues = (grouped) => [...itertools.imap(grouped, ([k, v]) => [k, [...v].length])]
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more