Created
March 3, 2011 03:06
-
-
Save amcjen/852252 to your computer and use it in GitHub Desktop.
Challenge 1 for Greplin Challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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! | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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! | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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