Skip to content

Instantly share code, notes, and snippets.

@adambom
adambom / factorial.js
Created June 29, 2012 20:54
How to do factorial
function factorial(n) {
return n < 2 ? 1 : n * factorial(n - 1);
};
var fastFac = (function () {
var memo = {};
return function (n) {
if (memo[n]) {
return memo[n];
@adambom
adambom / isPrime.js
Created June 29, 2012 21:02
Is Prime Number??
function isDivisible(dividend, divisor) {
return dividend % divisor === 0;
}
function isPrime(n) {
var factor = 2;
n = Math.abs(n);
if (n <= 1) {
@adambom
adambom / isPalindrome.js
Created June 29, 2012 21:03
Is Palindrome??
function isPalindrome(n) {
var str, firstDigit, lastDigit;
str = String(n).replace(".", "").split("");
if (str.length < 2) {
return false;
}
firstDigit = str.shift();
@adambom
adambom / findLargestPrimePalindrome.js
Created June 29, 2012 21:05
Largest prime palindrome under a max
function findLargestPrimePalindrome(max) {
if (max === 0) {
return null;
}
if (isPalindrome(max) && isPrime(max)) {
return max;
}
return findLargestPrimePalindrome(max - 1);
console.log("Build started");
var now = new Date();
var _ = require("underscore");
var jsp = require("uglify-js").parser;
var pro = require("uglify-js").uglify;
var fs = require("fs");
var output = "js/compiled.js";
var src = [
{"startDate":1325404800000,"periods":[2012,2013,2014,2015,2016,2017,2018,2019,2020,2021],"baseDemandCaseId":"","currencyId":"USD","endDate":1638345600000,"endTime":2021,"demandTypes":[{"values":["Front-End","Back-End","QA","Sales"],"label":"Business Unit"},{"values":["Jamaica","Haiti","Costa Rica"],"label":"Geography"}],"id":"402882a8380b2fcb01380ffc23660618","startTime":2012,"timeStep":1,"alternativeWorkStyles":["Mobile","Traditional"],"propertyClasses":[],"clientName":"Nina's Model","dateFormat":"MM/dd/yy","reportDate":1338534000000,"unitsOfMeasure":"Sq. Feet","simulationPath":"/model/nina-example-copy","propertyTypes":[],"baseSupplyCaseId":"","geographies":["Jamaica","Haiti","Costa Rica"],"squareAreaDescription":1,"clientFiscalYearStartMonth":1,"clientId":"default","currentTime":2012,"supplyCaseComparisonOrder":[],"afterTaxDiscountRate":0.05,"ffeUpgradePctCapital":0,"reportTime":2012,"marketRentIndex":["","","","","","","","","",""],"opexEscalationRate":[0.34,"","","","","","","","",""],"propertyTaxRate":0
@adambom
adambom / gist:4261157
Created December 11, 2012 19:10
Plot data
[{"data":{"model":["Mazda RX4","Mazda RX4 Wag","Datsun 710","Hornet 4 Drive","Hornet Sportabout","Valiant","Duster 360","Merc 240D","Merc 230","Merc 280","Merc 280C","Merc 450SE","Merc 450SL","Merc 450SLC","Cadillac Fleetwood","Lincoln Continental","Chrysler Imperial","Fiat 128","Honda Civic","Toyota Corolla","Toyota Corona","Dodge Challenger","AMC Javelin","Camaro Z28","Pontiac Firebird","Fiat X1-9","Porsche 914-2","Lotus Europa","Ford Pantera L","Ferrari Dino","Maserati Bora","Volvo 142E"],"mpg":[21,21,22.8,21.4,18.7,18.1,14.3,24.4,22.8,19.2,17.8,16.4,17.3,15.2,10.4,10.4,14.7,32.4,30.4,33.9,21.5,15.5,15.2,13.3,19.2,27.3,26,30.4,15.8,19.7,15,21.4],"cyl":[6,6,4,6,8,6,8,4,4,6,6,8,8,8,8,8,8,4,4,4,4,8,8,8,8,4,4,4,8,6,8,4],"disp":[160,160,108,258,360,225,360,146.7,140.8,167.6,167.6,275.8,275.8,275.8,472,460,440,78.7,75.7,71.1,120.1,318,304,350,400,79,120.3,95.1,351,145,301,121],"hp":[110,110,93,110,175,105,245,62,95,123,123,180,180,180,205,215,230,66,52,65,97,150,150,245,175,66,91,113,264,175,335,109],"drat":[3.9
@adambom
adambom / Pseudocode for generating multidimensional json
Created March 15, 2013 23:08
Pseudocode for generating multidimensional json
# Data structure to use for household size, e.g.
{
"northeast": {
"hispanic": {
"below 35": {
"under $20,000": []
}
}
@adambom
adambom / parseIncompleteJSON.js
Last active March 5, 2022 17:30
parsing incomplete json
function parseIncompleteJSON(jsonString, stopIndex) {
try {
return {
json: JSON.parse(jsonString),
stoppedAt: stopIndex
};
} catch (e) {
return parseIncompleteJSON(jsonString.substring(0, jsonString.length - 1), jsonString.length - 1);
}
@adambom
adambom / ddd.js
Created July 15, 2013 18:15
Distributed computing api example
var s = new DistributedSession('my-identifier');
s.on('connection', function (err, conn) {
conn.Parallel(data).map(mapper).reduce(reducer).then(function (d) {
console.log(d);
});
});
// Open connection and find peers
s.open().requirePeers(data.length);