This site is supported by donations to The OEIS Foundation.
User:M. F. Hasler/dynamic content/Client-side
From OeisWiki
JavaScript
This is a basic proof-of-concept implementation. It generates up to 250 terms of the sequence, outputting them directly in the sequence. If you save the script as "foo.user.js" you should be able to load it in Greasemonkey in Firefox by dragging the file into your browser window. It may also work in Opera and Chrome.
// ==UserScript==
// @name OEIS linear recurrences
// @namespace http://userscripts.org/crg4
// @description Proof-of-concept: generate sequence data for linear recurrences
// @include http://oeis.org/*
// ==/UserScript==
//alert('Processing ' + document.getElementsByTagName('a').length + ' links');
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
var lnk = links[i];
// Should be "http://oeis.org/Sindx_Rea.html#recLCC"
if (/#recLCC$/.test(lnk.href)) {
linRec(lnk.parentNode);
}
}
function firstAncestorOfType(y, typ) {
typ = typ.toUpperCase();
if (!y)
return false;
for (var i = 0; i < 15; i++) {
y = y.parentNode;
if (!y)
return false;
if (y.nodeName.toUpperCase() == typ)
return y;
}
return false;
};
function linRec(x) {
var txt = x.innerHTML.replace(/<\/?b>/g, ''); // Search results are marked with <b>...</b>; strip this out
if (/linear\s+recurrences\s+with\s+constant\s+coefficients<\/a>,\s*signature\s*\(([^()]*)\)/.test(txt)) {
var sig = RegExp.$1;
//alert('Found recurrence with signature ' + sig);
var y = firstAncestorOfType(firstAncestorOfType(x, 'tbody'), 'tbody'); // Grab table containing the entire sequence
if (!y)
return;
var seqDataElement = y.children[3].firstChild.children[0].children[0].children[0].children[1].children[0];
generateContent(x, sig.split(','), seqDataElement.innerHTML.split(','));
//x.innerHTML = x.innerHTML.replace('(' + sig + ')', '<span style="color: red">(' + sig + ')</span>');
}
}
function generateContent (DOMLocation, signature, sequence) {
var el = document.createElement('tt');
var txt = document.createTextNode(extendSeq(sequence, signature).join(', '));
el.appendChild(txt);
DOMLocation.appendChild(document.createElement('br'));
DOMLocation.appendChild(el);
el.style.color = 'red';
}
function extendSeq(seq, signature) {
if (signature.length > seq.length)
return seq;
var s = new Array(250);
for (var i = 0; i < seq.length; i++) {
s[i] = seq[i];
}
for (var i = seq.length; i < s.length; i++) {
var t = 0;
for (var j = 0; j < signature.length; j++) {
t += signature[j] * s[i - 1 - j];
}
if (t > (1 << 53))
return s.slice(0, i - 1);
s[i] = t;
}
return s;
}