Skip to content

Instantly share code, notes, and snippets.

@dachev
Created June 8, 2011 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dachev/1013807 to your computer and use it in GitHub Desktop.
Save dachev/1013807 to your computer and use it in GitHub Desktop.
Greplin Solutions
// one
function palindrome(text) {
var longest = '';
for (var i = 0; i < text.length; i++) {
var charOne = text.charAt(i);
for (var j = text.length-1; j > i; j--) {
var charTwo = text.charAt(j);
if (charOne != charTwo) { continue; }
var k = i;
var l = j;
var palindrome = true;
while (k < l) {
if (text.charAt(k) == text.charAt(l)) {
k++;
l--;
continue;
}
palindrome = false;
break;
}
var length = j-i;
if (palindrome == true && length > longest.length) {
longest = text.slice(i, j+1);
}
}
}
return longest;
}
// two
function isPrime(n) {
if (n <= 1) { return false; }
if (n == 2) { return true; }
if (n%2 == 0) { return false; }
var sqrt = Math.sqrt(n);
for (var i = 3; i <= sqrt; i += 2) {
if (n % i == 0) { return false; }
}
return true;
}
function nextPrime(n) {
if (n < 1) { return 1; }
while (n++) {
if (isPrime(n) == true) { return n; }
}
}
function factorize(n) {
var factors = [];
if (n < 4) { return factors; }
var prime = nextPrime(1);
var remain = n;
while(remain > prime && isPrime(remain) == false) {
if (remain % prime == 0) { factors.push(prime); }
while(remain > prime && remain % prime == 0) {
remain = remain/prime;
}
prime = nextPrime(prime);
}
if (factors.indexOf(remain) < 0) { factors.push(remain); }
return factors;
}
var cache = {};
function fib(n) {
if (n < 4) { return 1; }
if (cache[n]) { return cache[n]; }
cache[n] = fib(n-1) + fib(n-2);
return cache[n];
}
function fast_fib(n) {
if (n < 3) { return 1; }
var lastOne = 1;
var lastTwo = 1;
var sum = lastOne + lastTwo;
for (var i = 3; i <= n; i++) {
var sum = lastOne + lastTwo;
lastTwo = lastOne;
lastOne = sum;
}
return sum;
}
function fibPrime(n) {
var i = 1;
var f = fib(i);
while (f <= n || isPrime(f) == false) {
i++;
f = fib(i);
}
return f;
}
function sum(array) {
var sum = 0;
for (var i = 0; i < array.length; i++) {
sum += array[i];
}
return sum;
}
// three
function sets(array) {
var cnt = 0;
var len = 1 << array.length;
for (var i = 0; i < len; i++) {
var pos = array.length - 1;
var bitmask = i;
var subset = [];
while(bitmask > 0) {
if((bitmask & 1) == 1) {
subset.push(array[pos]);
}
bitmask >>= 1;
pos--;
}
if (subset.length < 1) { continue; }
subset.sort(function(a,b) { return a==b ? 0 : a-b; });
var max = subset[subset.length-1];
var sum = 0;
for (var j = 0; j < subset.length-1; j++) {
sum += subset[j];
if (sum > max) { continue; }
}
if (sum == max) {
cnt++;
}
}
return cnt;
}
var text = 'FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth';
console.log(palindrome(text));
console.log(sum(factorize(fibPrime(217000)+1)));
var array = [3, 4, 9, 14, 15, 19, 28, 37, 47, 50, 54, 56, 59, 61, 70, 73, 78, 81, 92, 95, 97, 99];
console.log(sets(array));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment