Published
Edited
May 17, 2018
1 fork
9 stars
Insert cell
Insert cell
Insert cell
{
let sum = 0;
for (let i = 0; i < 1000; i++) {
if (i % 3 === 0 || i % 5 === 0) {
sum += i;
}
}
return sum;
}
Insert cell
Insert cell
Insert cell
// Generate fibonacci numbers
function* fibonacci() {
let a = 1, b = 2;
let sum = 2;
yield sum;
while (true) {
let c = a + b;
a = b;
b = c;
if (c > 4000000) break;
yield c;
}
}
Insert cell
// Gut check that calculation by looking at the first 10 values
{
let array = [];
for (let term of fibonacci()) {
array.push(term);
if (array.length > 9) return array;
}
}
Insert cell
Insert cell
Insert cell
{
let sum = 0;
for (let term of fibonacci()) {
if (term % 2 === 0) sum += term;
}
return sum;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
numberInWords = n => {
let str = n.toString();
if (str.length === 4) return 'one thousand';
let singleDigit = +str[str.length - 1];
let tensDigit = +str[str.length - 2];
let hundredsDigit = +str[str.length - 3];
let single = singleDigit ? LESS_THAN_TWENTY[singleDigit] : '';
let tens = tensDigit > 1 ? TENTHS[tensDigit] : '';
let hundreds = hundredsDigit ? (single || tens)
?
LESS_THAN_TWENTY[hundredsDigit] + ' hundred and' :
LESS_THAN_TWENTY[hundredsDigit] + ' hundred'
: '';
return hundreds + ' ' + tens + single;
}
Insert cell
{
let numbers = [];
for (let i = 1; i <= 1000; i++) {
numbers.push(numberInWords(i));
}
return numbers;
}
Insert cell
Insert cell
Insert cell
function fact(x) {
let acc = 1;
while (x > 1) {
acc *= --x;
}
return acc;
}
Insert cell
Insert cell
function sumDigits(x) {
let sum = 0;
while (x !== 0) {
sum += x % 10;
x = Math.floor(x / 10);
}
return sum;
}
Insert cell
sumDigits(fact(10))
Insert cell
Insert cell
sumDigits(fact(100))
Insert cell
{
let res = [];
for (let i = 1; i <= 100; i++) {
res.push(`${i} = ${fact(i)} | ${sumDigits(fact(i))}`);
}
return res;
}
Insert cell
Insert cell
BigNumber = {
let bn = await require('bignumber.js');
// Print out lots and lots of digits
bn.config({ EXPONENTIAL_AT: 1e+9 });
return bn;
}
Insert cell
Insert cell
function fact2(x) {
let acc = new BigNumber(1);
while (x > 1) {
acc = acc.times(--x);
}
return acc;
}
Insert cell
function sumDigits2(x) {
let str = x.toString();
let sum = 0;
for (let char of str) {
sum += +char;
}
return sum;
}
Insert cell
sumDigits2(fact2(100))
Insert cell
Insert cell
Insert cell
Insert cell
sum = {
let sum = new BigNumber(0);
let index = 1;
for (let name of names) {
let nameScore = 0;
for (let char of name) {
nameScore += char.charCodeAt(0) - 64;
}
sum = sum.plus(nameScore * index);
index++;
}
return sum.toString();
}
Insert cell
Insert cell
{
let sum = new BigNumber(0);
for (let i = 1; i <= 1000; i++) {
sum = sum.plus((new BigNumber(i)).pow(i));
}
return sum.toString().slice(-10);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
let digits = new BigNumber(2).pow(1000).toString();
let sum = new BigNumber(0);
for (let d of digits) {
sum = sum.plus(parseInt(d));
}
return sum.toString();
}
Insert cell
Insert cell
function findPrimeFactors (num) {

let primeFactors = [];
while (num % 2 === 0) {
primeFactors.push(2);
num = num / 2;
}
var sqrtNum = Math.sqrt(num);
for (let i = 3; i <= sqrtNum; i++) {
while (num % i === 0) {
primeFactors.push(i);
num = num / i;
}
}
if (num > 2) {
primeFactors.push(num);
}
return primeFactors;
}
Insert cell
Insert cell
Insert cell
function isPalindromeLoop(a) {
let str = a.toString();
if (str.length == 1) return true;
for (let i = 0; i < Math.ceil(str.length / 2); i++) {
if (str[i] !== str[str.length - i - 1]) return false;
}
return true;
}
Insert cell
Insert cell
Insert cell
{
for (let i = 2520;; i += 20) {
if (i % 11 == 0 &&
i % 12 == 0 &&
i % 13 == 0 &&
i % 14 == 0 &&
i % 15 == 0 &&
i % 16 == 0 &&
i % 17 == 0 &&
i % 18 == 0 &&
i % 19 == 0 &&
i % 20 == 0) {
return i;
}
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
' '.charCodeAt(0)
Insert cell
Insert cell
Insert cell
Insert cell
start = 'a'.charCodeAt(0)
Insert cell
end = 'z'.charCodeAt(0)
Insert cell
decoded = {
for (let i = start; i < end; i++) {
for (let j = start; j < end; j++) {
for (let k = start; k < end; k++) {
let password = [i, j, k];
let out = "";
for (let c = 0; c < characters.length; c++) {
out += String.fromCharCode(characters[c] ^ password[c % 3]);
}
if (out.indexOf(' the ') !== -1) {
return out;
}
}
}
}
}
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