Skip to content

Instantly share code, notes, and snippets.

@amcjen
Created March 3, 2011 03:06
Show Gist options
  • Save amcjen/852252 to your computer and use it in GitHub Desktop.
Save amcjen/852252 to your computer and use it in GitHub Desktop.
Challenge 1 for Greplin Challenge
<?php
$text = strtolower('FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth');
$textLength = strlen($text);
$maxPalindromeLength = 0;
$currentWinner = '';
for ($i=0; $i<$textLength; $i++) {
for ($j=$textLength; $j>0; $j--) {
$subString = substr($text, $i, $j);
if ($subString == strrev($subString)) {
if (strlen($subString) > $maxPalindromeLength) {
$maxPalindromeLength = strlen($subString);
$currentWinner = $subString;
}
}
}
}
echo 'Password is ' . $currentWinner . ' with a length of ' . $maxPalindromeLength . ". Onward! \n\n";
// 19 minutes!
?>
<?php
$x = 1;
$xPrev = 0;
$fibb = 0;
$floor = 217000;
function isPrime($num) {
for ($i = 2; $i < $num; $i++) {
if ($num % $i == 0 ) {
return false;
}
}
return true;
}
while ($fibb < $floor) {
$fibb = $x + $xPrev;
$xPrev = $x;
$x = $fibb;
}
while (!isPrime($fibb)) {
$fibb = $x + $xPrev;
$xPrev = $x;
$x = $fibb;
}
echo 'The first prime fibonacci number larger than ' . $floor . ' is ' . $fibb . ".\n";
$z = $fibb + 1;
$sumOfPrimeDivisors = 0;
for ($i = 2; $i < $z; $i++) {
if ($z % $i == 0 && isPrime($i)) {
$sumOfPrimeDivisors += $i;
}
}
echo 'The sum of prime divisors for X + 1 is ' . $sumOfPrimeDivisors . ". Onward!\n";
// 35 minutes!
?>
<?php
//$set = array(1,2,3,4,6);
$set = array(3, 4, 9, 14, 15, 19, 28, 37, 47, 50, 54, 56, 59, 61, 70, 73, 78, 81, 92, 95, 97, 99);
$top = pow(2,count($set));
$arraySub = array();
for ($i=0; $i<$top; $i++) {
$mask = str_split(sprintf('%0' . count($set) . 's', decbin($i)));
$sum = 0;
$subsetArray = array();
$keyString = '';
foreach ($mask as $key => $index) {
if ($index == 1) {
$sum += $set[$key];
$subsetArray[$key] = $set[$key];
$keyString .= $set[$key] . ':';
}
}
if (in_array($sum, $set) && count($subsetArray) > 1) {
$arraySub[$keyString] = $sum;
}
}
echo 'The number of subsets is ' . count($arraySub). ". Score!\n";
// 60 minutes! Needed a mad-rush tutorial on power sets!
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment