diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..53253a8 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,3 @@ +# These are supported funding model platforms +github: [mirahman] +custom: ["paypal.me/mirahman1221", mizanurrahman.com] diff --git a/.sonarcloud.properties b/.sonarcloud.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/.sonarcloud.properties @@ -0,0 +1 @@ + diff --git a/Algorithms/Graph/Dijkstra.php b/Algorithms/Graph/Dijkstra.php index 13ca987..864205e 100644 --- a/Algorithms/Graph/Dijkstra.php +++ b/Algorithms/Graph/Dijkstra.php @@ -1,71 +1,73 @@ - - * - */ - -function Dijkstra(array $graph, string $source, string $target): array { - $dist = []; - $pred = []; - $Queue = new SplPriorityQueue(); - - foreach ($graph as $v => $adj) { - $dist[$v] = PHP_INT_MAX; - $pred[$v] = null; - $Queue->insert($v, min($adj)); - } - - $dist[$source] = 0; - - while (!$Queue->isEmpty()) { - $u = $Queue->extract(); - if (!empty($graph[$u])) { - foreach ($graph[$u] as $v => $cost) { - if ($dist[$u] + $cost < $dist[$v]) { - $dist[$v] = $dist[$u] + $cost; - $pred[$v] = $u; - } - } - } - } - - $S = new SplStack(); - $u = $target; - $distance = 0; - - while (isset($pred[$u]) && $pred[$u]) { - $S->push($u); - $distance += $graph[$u][$pred[$u]]; - $u = $pred[$u]; - } - - if ($S->isEmpty()) { - return ["distance" => 0, "path" => $S]; - } else { - $S->push($source); - return ["distance" => $distance, "path" => $S]; - } -} - -$graph = [ - 'A' => ['B' => 3, 'C' => 5, 'D' => 9], - 'B' => ['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7], - 'C' => ['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3], - 'D' => ['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2], - 'E' => ['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5], - 'F' => ['C' => 3, 'D' => 2, 'E' => 5], -]; - -$source = "A"; -$target = "F"; - -$result = Dijkstra($graph, $source, $target); -extract($result); - -echo "Distance from $source to $target is $distance \n"; -echo "Path to follow : "; - -while (!$path->isEmpty()) { - echo $path->pop() . "\t"; + + * + */ + +function dijkstra(array $graph, string $source, string $target): array { + $dist = []; + $pred = []; + $Queue = new SplPriorityQueue(); + + foreach ($graph as $v => $adj) { + $dist[$v] = PHP_INT_MAX; + $pred[$v] = null; + foreach ($adj as $w => $cost) { + $Queue->insert($w, $cost); + } + } + + $dist[$source] = 0; + + while (!$Queue->isEmpty()) { + $u = $Queue->extract(); + if (!empty($graph[$u])) { + foreach ($graph[$u] as $v => $cost) { + if ($dist[$u] + $cost < $dist[$v]) { + $dist[$v] = $dist[$u] + $cost; + $pred[$v] = $u; + } + } + } + } + + $S = new SplStack(); + $u = $target; + $distance = 0; + + while (isset($pred[$u]) && $pred[$u]) { + $S->push($u); + $distance += $graph[$u][$pred[$u]]; + $u = $pred[$u]; + } + + if ($S->isEmpty()) { + return ["distance" => 0, "path" => $S]; + } else { + $S->push($source); + return ["distance" => $distance, "path" => $S]; + } +} + +$graph = [ + 'A' => ['B' => 3, 'C' => 5, 'D' => 9], + 'B' => ['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7], + 'C' => ['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3], + 'D' => ['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2], + 'E' => ['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5], + 'F' => ['C' => 3, 'D' => 2, 'E' => 5], +]; + +$source = "E"; +$target = "F"; + +$result = dijkstra($graph, $source, $target); +extract($result); + +echo "Distance from $source to $target is $distance \n"; +echo "Path to follow : "; + +while (!$path->isEmpty()) { + echo $path->pop() . "\t"; } \ No newline at end of file diff --git a/Algorithms/Graph/Kruskal.php b/Algorithms/Graph/Kruskal.php index 5c45187..335bc9a 100644 --- a/Algorithms/Graph/Kruskal.php +++ b/Algorithms/Graph/Kruskal.php @@ -7,7 +7,7 @@ * */ -function Kruskal(array $graph): array { +function kruskal(array $graph): array { $len = count($graph); $tree = []; @@ -67,7 +67,7 @@ function unionSet(array &$set, int $i, int $j) { [0, 0, 4, 2, 6, 0] ]; -$mst = Kruskal($graph); +$mst = kruskal($graph); $minimumCost = 0; diff --git a/Algorithms/Numbers-Maths/BigNumber.php b/Algorithms/Numbers-Maths/BigNumber.php index 48e7027..6129067 100644 --- a/Algorithms/Numbers-Maths/BigNumber.php +++ b/Algorithms/Numbers-Maths/BigNumber.php @@ -7,7 +7,6 @@ */ - class BigInteger { @@ -44,7 +43,8 @@ public function __construct(string $number) if ($number == "0") $this->lastDigit = 0; } - public static function print(BigInteger &$n) { + public static function print(BigInteger &$n) + { if ($n->signBit == BigInteger::MINUS) echo "- "; for ($i = $n->lastDigit; $i >= 0; $i--) echo $n->digits[$i]; @@ -242,12 +242,13 @@ public static function divide(BigInteger &$a, BigInteger &$b, BigInteger &$c) $b->signBit = $bsign; } - public static function factorial (int $n, BigInteger &$c) { + public static function factorial(int $n, BigInteger &$c) + { BigInteger::toBigInteger("1", $c); - if($n > 0) { - for($i = 1;$i<=$n;$i++) { + if ($n > 0) { + for ($i = 1; $i <= $n; $i++) { $tmp = clone $c; $number = new BigInteger($i); $result = new BigInteger("0"); @@ -258,8 +259,23 @@ public static function factorial (int $n, BigInteger &$c) { BigInteger::zeroJustify($c); } -} + public static function power(BigInteger &$a, int $n, BigInteger &$c) + { + BigInteger::toBigInteger("1", $c); + + if ($n > 0) { + for ($i = 0; $i < $n; $i++) { + $tmp = clone $c; + $result = new BigInteger("0"); + BigInteger::multiply($a, $tmp, $result); + $c = $result; + } + } + BigInteger::zeroJustify($c); + } + +} $first = new BigInteger("1234567812345678123456781234567812345678"); @@ -281,6 +297,11 @@ public static function factorial (int $n, BigInteger &$c) { BigInteger::divide($first, $second, $divide); BigInteger::print($divide); +$number = new BigInteger("55"); +$power = new BigInteger("0"); +BigInteger::power($number, 5, $power); +BigInteger::print($power); + $factorial = new BigInteger("0"); BigInteger::factorial(100, $factorial); BigInteger::print($factorial); \ No newline at end of file diff --git a/Algorithms/Numbers-Maths/Sieve.php b/Algorithms/Numbers-Maths/Sieve.php new file mode 100644 index 0000000..5324392 --- /dev/null +++ b/Algorithms/Numbers-Maths/Sieve.php @@ -0,0 +1,29 @@ + + * + */ + +function sieveOfEratosthenes(int $n) +{ + + $prime = array_fill(0, $n + 1, true); + + for ($p = 2; $p * $p <= $n; $p++) { + // If prime[p] is not changed, then it is a prime + if (isset($prime[$p]) && $prime[$p] == true) { + // Update all multiples of p + for ($i = $p * 2; $i <= $n; $i += $p) + $prime[$i] = false; + } + } + + // Print all prime numbers + for ($p = 2; $p <= $n; $p++) + if ($prime[$p]) + echo $p . " "; +} + + +sieveOfEratosthenes(5000000); \ No newline at end of file diff --git a/Algorithms/Recursion-DP-Others/BacktrackingSudoku.php b/Algorithms/Recursion-DP-Others/BacktrackingSudoku.php index cece398..a86dd9c 100644 --- a/Algorithms/Recursion-DP-Others/BacktrackingSudoku.php +++ b/Algorithms/Recursion-DP-Others/BacktrackingSudoku.php @@ -9,17 +9,17 @@ define("N", 9); define("UNASSIGNED", 0); -function SolveSudoku(array &$grid): bool { +function solveSudoku(array &$grid): bool { $row = $col = 0; - if (!FindUnassignedLocation($grid, $row, $col)) + if (!findUnassignedLocation($grid, $row, $col)) return true; // success! no empty space for ($num = 1; $num <= N; $num++) { if (isSafe($grid, $row, $col, $num)) { $grid[$row][$col] = $num; // make tentative assignment - if (SolveSudoku($grid)) + if (solveSudoku($grid)) return true; // return, if success// return, if success $grid[$row][$col] = UNASSIGNED; // failure, unmake & try again @@ -28,7 +28,7 @@ function SolveSudoku(array &$grid): bool { return false; // triggers backtracking } -function FindUnassignedLocation(array &$grid, int &$row, int &$col): bool { +function findUnassignedLocation(array &$grid, int &$row, int &$col): bool { for ($row = 0; $row < N; $row++) for ($col = 0; $col < N; $col++) if ($grid[$row][$col] == UNASSIGNED) @@ -36,15 +36,15 @@ function FindUnassignedLocation(array &$grid, int &$row, int &$col): bool { return false; } -function UsedInRow(array &$grid, int $row, int $num): bool { +function usedInRow(array &$grid, int $row, int $num): bool { return in_array($num, $grid[$row]); } -function UsedInColumn(array &$grid, int $col, int $num): bool { +function usedInColumn(array &$grid, int $col, int $num): bool { return in_array($num, array_column($grid, $col)); } -function UsedInBox(array &$grid, int $boxStartRow, int $boxStartCol, int $num):bool { +function usedInBox(array &$grid, int $boxStartRow, int $boxStartCol, int $num):bool { for ($row = 0; $row < 3; $row++) for ($col = 0; $col < 3; $col++) if ($grid[$row + $boxStartRow][$col + $boxStartCol] == $num) @@ -54,9 +54,9 @@ function UsedInBox(array &$grid, int $boxStartRow, int $boxStartCol, int $num):b function isSafe(array $grid, int $row, int $col, int $num): bool { - return !UsedInRow($grid, $row, $num) && - !UsedInColumn($grid, $col, $num) && - !UsedInBox($grid, $row - $row % 3, $col - $col % 3, $num); + return !usedInRow($grid, $row, $num) && + !usedInColumn($grid, $col, $num) && + !usedInBox($grid, $row - $row % 3, $col - $col % 3, $num); } /* A utility function to print grid */ @@ -80,7 +80,7 @@ function printGrid(array $grid) { [0, 0, 2, 0, 9, 0, 5, 0, 0] ]; -if (SolveSudoku($grid) == true) +if (solveSudoku($grid) == true) printGrid($grid); else echo "No solution exists"; \ No newline at end of file diff --git a/Algorithms/Recursion-DP-Others/DNASequencing.php b/Algorithms/Recursion-DP-Others/DNASequencing.php index b6f77eb..1d9b776 100644 --- a/Algorithms/Recursion-DP-Others/DNASequencing.php +++ b/Algorithms/Recursion-DP-Others/DNASequencing.php @@ -11,7 +11,7 @@ define("GP", -1); define("MS", -1); -function NWSquencing(string $s1, string $s2) { +function nwSquencing(string $s1, string $s2) { $grid = []; $M = strlen($s1); $N = strlen($s2); @@ -89,4 +89,4 @@ function printSequence($grid, $s1, $s2, $j, $i) { $X = "GAATTCAGTTA"; $Y = "GGATCGA"; -NWSquencing($X, $Y); +nwSquencing($X, $Y); diff --git a/Algorithms/Recursion-DP-Others/KMPMatching.php b/Algorithms/Recursion-DP-Others/KMPMatching.php index 09b1113..71cabd0 100644 --- a/Algorithms/Recursion-DP-Others/KMPMatching.php +++ b/Algorithms/Recursion-DP-Others/KMPMatching.php @@ -5,14 +5,14 @@ * */ -function KMPStringMatching(string $str, string $pattern): array { +function kmpStringMatching(string $str, string $pattern): array { $matches = []; $M = strlen($pattern); $N = strlen($str); $i = $j = 0; $lps = []; - ComputeLPS($pattern, $lps); + computeLPS($pattern, $lps); while ($i < $N) { if ($pattern[$j] == $str[$i]) { @@ -33,7 +33,7 @@ function KMPStringMatching(string $str, string $pattern): array { return $matches; } -function ComputeLPS(string $pattern, array &$lps) { +function computeLPS(string $pattern, array &$lps) { $len = 0; $i = 1; $M = strlen($pattern); @@ -58,7 +58,7 @@ function ComputeLPS(string $pattern, array &$lps) { $txt = "AABAACAADAABABBBAABAA"; $pattern = "AABA"; -$matches = KMPStringMatching($txt, $pattern); +$matches = kmpStringMatching($txt, $pattern); if ($matches) { foreach ($matches as $pos) { diff --git a/DS/Heap/Classes/MinHeap.php b/DS/Heap/Classes/MinHeap.php index 3c62795..26d82bd 100644 --- a/DS/Heap/Classes/MinHeap.php +++ b/DS/Heap/Classes/MinHeap.php @@ -7,77 +7,86 @@ namespace DS\Heap\Classes; -class MinHeap { +class MinHeap +{ public $heap; public $count; - public function __construct(int $size) { - $this->heap = array_fill(0, $size + 1, 0); - $this->count = 0; + public function __construct(int $size) + { + $this->heap = array_fill(0, $size + 1, 0); + $this->count = 0; } - public function create(array $arr = []) { - if ($arr) { - foreach ($arr as $val) { - $this->insert($val); - } - } + public function create(array $arr = []) + { + if ($arr) { + foreach ($arr as $val) { + $this->insert($val); + } + } } - public function display() { - echo implode("\t", array_slice($this->heap, 1)) . "\n"; + public function display() + { + echo implode("\t", array_slice($this->heap, 1)) . "\n"; } - public function insert(int $i) { - if ($this->count == 0) { - $this->heap[1] = $i; - $this->count = 2; - } else { - $this->heap[$this->count++] = $i; - $this->siftUp(); - } + public function insert(int $i) + { + if ($this->count == 0) { + $this->heap[1] = $i; + $this->count = 2; + } else { + $this->heap[$this->count++] = $i; + $this->siftUp(); + } } - public function siftUp() { - $tmpPos = $this->count - 1; - $tmp = intval($tmpPos / 2); + public function siftUp() + { + $tmpPos = $this->count - 1; + $tmp = intval($tmpPos / 2); - while ($tmpPos > 0 && $this->heap[$tmp] > $this->heap[$tmpPos]) { - $this->swap($tmpPos, $tmp); - $tmpPos = intval($tmpPos / 2); - $tmp = intval($tmpPos / 2); - } + while ($tmpPos > 0 && $this->heap[$tmp] > $this->heap[$tmpPos]) { + $this->swap($tmpPos, $tmp); + $tmpPos = intval($tmpPos / 2); + $tmp = intval($tmpPos / 2); + } } - public function swap(int $a, int $b) { - $tmp = $this->heap[$a]; - $this->heap[$a] = $this->heap[$b]; - $this->heap[$b] = $tmp; + public function swap(int $a, int $b) + { + $tmp = $this->heap[$a]; + $this->heap[$a] = $this->heap[$b]; + $this->heap[$b] = $tmp; } - public function extractMin() { - $min = $this->heap[1]; - $this->heap[1] = $this->heap[$this->count - 1]; - $this->heap[--$this->count] = 0; - $this->siftDown(1); - return $min; + public function extractMin() + { + $min = $this->heap[1]; + $this->heap[1] = $this->heap[$this->count - 1]; + $this->heap[--$this->count] = 0; + $this->siftDown(1); + return $min; } - public function siftDown(int $k) { - $smallest = $k; - $left = 2 * $k; - $right = 2 * $k + 1; - if ($left < $this->count && $this->heap[$smallest] > $this->heap[$left]) { - $smallest = $left; - } - if ($right < $this->count && $this->heap[$smallest] > $this->heap[$right]) { - $smallest = $right; - } - if ($smallest != $k) { - $this->swap($k, $smallest); - $this->siftDown($smallest); - } + public function siftDown(int $k) + { + $smallest = $k; + $left = 2 * $k; + $right = 2 * $k + 1; + if ($left < $this->count && $this->heap[$smallest] > $this->heap[$left]) { + $smallest = $left; + } + if ($right < $this->count && $this->heap[$smallest] > $this->heap[$right]) { + $smallest = $right; + } + if ($smallest != $k) { + $this->swap($k, $smallest); + $this->siftDown($smallest); + } } } \ No newline at end of file diff --git a/DS/LinkedList/Classes/LinkedList.php b/DS/LinkedList/Classes/LinkedList.php index 1d6bfa3..40dd272 100644 --- a/DS/LinkedList/Classes/LinkedList.php +++ b/DS/LinkedList/Classes/LinkedList.php @@ -69,7 +69,11 @@ public function insertBefore(string $data = NULL, string $query = NULL) { while ($currentNode !== NULL) { if ($currentNode->data === $query) { $newNode->next = $currentNode; - $previous->next = $newNode; + if ($previous === NULL) { + $this->_firstNode = &$newNode; + } else { + $previous->next = $newNode; + } $this->_totalNode++; break; } diff --git a/DS/Tree/.DS_Store b/DS/Tree/.DS_Store new file mode 100644 index 0000000..1ad25cb Binary files /dev/null and b/DS/Tree/.DS_Store differ diff --git a/DS/Tree/Classes/Trie.php b/DS/Tree/Classes/Trie.php new file mode 100644 index 0000000..2fe2292 --- /dev/null +++ b/DS/Tree/Classes/Trie.php @@ -0,0 +1,49 @@ + + * + */ + +namespace DS\Tree\Classes; + +use DS\Tree\Classes\TrieNode; + +class Trie +{ + public $root; + public function __construct() + { + $this->root = new TrieNode; + } + + public function insert(string $key) + { + $key = strtolower($key); + $length = strlen($key); + $pCrawl = $this->root; + for ($level = 0; $level < $length; $level++) { + $index = ord($key[$level]) - ord('a'); + if (is_null($pCrawl->children[$index])) + $pCrawl->children[$index] = new TrieNode(); + + $pCrawl = $pCrawl->children[$index]; + } + $pCrawl->isEndOfWord = true; + } + + public function search(string $key) + { + $key = strtolower($key); + $length = strlen($key); + $pCrawl = $this->root; + for ($level = 0; $level < $length; $level++) { + $index = ord($key[$level]) - ord('a'); + if (is_null($pCrawl->children[$index])) + return false; + + $pCrawl = $pCrawl->children[$index]; + } + return (!is_null($pCrawl) && $pCrawl->isEndOfWord); + } +} diff --git a/DS/Tree/Classes/TrieNode.php b/DS/Tree/Classes/TrieNode.php new file mode 100644 index 0000000..ee49527 --- /dev/null +++ b/DS/Tree/Classes/TrieNode.php @@ -0,0 +1,21 @@ + + * + */ + +namespace DS\Tree\Classes; + +class TrieNode +{ + public $children; + public $isEndOfWord; + const ALPHABETSIZE = 26; + + public function __construct() + { + $this->isEndOfWord = false; + $this->children = array_fill(0,self::ALPHABETSIZE, null); + } +} \ No newline at end of file diff --git a/DS/Tree/Examples/trie.php b/DS/Tree/Examples/trie.php new file mode 100644 index 0000000..23e9ab0 --- /dev/null +++ b/DS/Tree/Examples/trie.php @@ -0,0 +1,30 @@ + + * + */ + +include __DIR__ . '/../../../Vendor/Autoload.php'; + +use \DS\Tree\Classes\Trie; + +try { + $keys = ["dhaka", "bangladesh", "there", "answer", "any", "by", "bye", "their"]; + + $trie = new Trie(); + // Construct trie + for ($i = 0; $i < count($keys); $i++) + $trie->insert($keys[$i]); + + $searches = ["dhaka", "three", "these", "there", "the", "any", "DHAKA", "anyone", "desh"]; + foreach ($searches as $search) { + if ($trie->search($search)) { + echo "$search - is present in the trie \n"; + } else { + echo "$search - is not present in the trie \n"; + } + } +} catch (Exception $e) { + echo $e->getMessage(); +} diff --git a/HackerRank/Euler/12.php b/HackerRank/Euler/12.php new file mode 100644 index 0000000..df70f4a --- /dev/null +++ b/HackerRank/Euler/12.php @@ -0,0 +1,63 @@ + + * + * Project Euler #12: Highly divisible triangular number + */ + +$triangleNumbers = []; + +function numberOfDivisors($triangle) { + + global $triangleNumbers; + + if (isset($triangleNumbers[$triangle])) { + return $triangleNumbers[$triangle]; + } else { + $tempTriangle = $triangle; + $primePowerCount = 1; + $count = 0; + while ($triangle % 2 == 0) { + $triangle =floor($triangle/ 2); + $count += 1; + } + $primePowerCount *= ($count + 1); + for ($i = 3; $i <= intval(sqrt($triangle)); $i++) { + $count = 0; + while ($triangle % $i == 0) { + $count += 1; + $triangle = floor($triangle/$i); + } + $primePowerCount *= ($count + 1); + } + if ($triangle > 2) { + $primePowerCount *= 2; + } + $triangleNumbers[$tempTriangle] = $primePowerCount; + return $triangleNumbers[$tempTriangle]; + } +} + +function getTriangle($number) { + return floor($number * ($number + 1)/ 2); +} + + +$handle = fopen ("php://stdin","r"); +$t = fgets($handle); +for($i=0; $i<$t; $i++) { + + $n = trim(fgets($handle)); + $number = 1; + $triangle = getTriangle($number); + $divisorCount = numberOfDivisors($triangle); + $triangleNumbers[$number] = $divisorCount; + while ($divisorCount <= $n) { + $number += 1; + $triangle = getTriangle($number); + $divisorCount = numberOfDivisors($triangle); + $triangleNumbers[$triangle] = $divisorCount; + + } + echo $triangle."\n"; +} \ No newline at end of file diff --git a/HackerRank/Euler/2.php b/HackerRank/Euler/2.php new file mode 100644 index 0000000..edf012b --- /dev/null +++ b/HackerRank/Euler/2.php @@ -0,0 +1,46 @@ + + * + * Project Euler #2: Even Fibonacci numbers + */ + +$fibs = fibo(80); + + +$handle = fopen ("php://stdin","r"); +$t = fgets($handle); + +for($i=0; $i<$t; $i++) { + $num = trim(fgets($handle)); + + $sum = 0; + foreach($fibs as $fib) { + if($fib<=$num) { + $sum += $fib; + } + } + + print($sum)."\n"; +} +fclose($handle); + + +function fibo($n) { + $prev = 1; + $next = 2; + + $fibs = array(2); + + for($i = 1;$i<$n;$i++) { + $tmp = $next; + $next = $next+$prev; + $prev = $tmp; + + if(($next+$prev)%2 == 0) + $fibs[] = $next+$prev; + } + + return $fibs; +} \ No newline at end of file diff --git a/HackerRank/Euler/21.php b/HackerRank/Euler/21.php new file mode 100644 index 0000000..559cddd --- /dev/null +++ b/HackerRank/Euler/21.php @@ -0,0 +1,64 @@ + + * + * Project Euler #21: Amicable numbers + */ + +$amicableNumbers = []; + +function numberOfDivisors($amicable) +{ + global $amicableNumbers; + + $tempAmicable = $amicable; + $factors = [1]; + $sum = 1; + for ($i = 2; $i <= intval(sqrt($tempAmicable)); $i++) { + $amicable = $tempAmicable; + $count = 0; + while ($amicable % $i == 0) { + if (!in_array($i, $factors)) { + $factors[] = $i; + $sum += $i; + } + + $amicable = floor($amicable / $i); + if (!in_array($amicable, $factors)) { + $factors[] = $amicable; + $sum += $amicable; + } + } + } + + $amicableNumbers[$tempAmicable] = $sum; +} + +for ($i = 1; $i <= 100000; $i++) { + numberOfDivisors($i); +} + +$validAmicableNumbers = []; + +foreach ($amicableNumbers as $i => $val) { + if (isset($amicableNumbers[$i]) && isset($amicableNumbers[$val]) && $amicableNumbers[$val] == $i && $i != $val) { + $validAmicableNumbers[$i] = $i; + } +} + + +$handle = fopen("php://stdin", "r"); +$t = fgets($handle); +for ($i = 0; $i < $t; $i++) { + $n = trim(fgets($handle)); + $sum = 0; + + foreach ($validAmicableNumbers as $number) { + if ($number > $n) { + break; + } + $sum += $number; + } + + echo $sum . "\n"; +} \ No newline at end of file diff --git a/HackerRank/Euler/23.php b/HackerRank/Euler/23.php new file mode 100644 index 0000000..b85a82f --- /dev/null +++ b/HackerRank/Euler/23.php @@ -0,0 +1,68 @@ + + * + * Project Euler #23: Non-abundant sums + */ + +$abundantNumbers = []; + +function numberOfDivisors($abundant) +{ + global $abundantNumbers; + + $tempAbundant = $abundant; + $factors = [1]; + $sum = 1; + for ($i = 2; $i <= intval(sqrt($tempAbundant)); $i++) { + $abundant = $tempAbundant; + $count = 0; + while ($abundant % $i == 0) { + if (!in_array($i, $factors)) { + $factors[] = $i; + $sum += $i; + } + + $abundant = floor($abundant / $i); + if (!in_array($abundant, $factors)) { + $factors[] = $abundant; + $sum += $abundant; + } + } + } + + $abundantNumbers[$tempAbundant] = $sum; +} + +for ($i = 1; $i <= 100000; $i++) { + numberOfDivisors($i); +} + +$validAbundantNumbers = []; + +foreach ($abundantNumbers as $i => $val) { + if ($i < $val) { + $validAbundantNumbers[$i] = $val; + } +} + +$handle = fopen("php://stdin", "r"); +$t = fgets($handle); +for ($i = 0; $i < $t; $i++) { + $n = trim(fgets($handle)); + $sum = 0; + $possible = false; + + foreach ($validAbundantNumbers as $number => $val) { + if ($number > $n) { + break; + } + + if(isset($validAbundantNumbers[$n-$number])) { + $possible = true; + break; + } + } + + echo $possible ? 'YES'."\n" : 'NO' . "\n"; +} \ No newline at end of file diff --git a/HackerRank/Euler/34.php b/HackerRank/Euler/34.php new file mode 100644 index 0000000..d509d00 --- /dev/null +++ b/HackerRank/Euler/34.php @@ -0,0 +1,33 @@ + + * + * Project Euler #34: Digit factorials + */ + + +$factorial = [1,1,2,6,24,120,720,5040,40320,362880]; + +$factorialSum = []; + + + +$handle = fopen("php://stdin", "r"); +$number = fgets($handle); +$total = 0; + +for($i = 10;$i<=$number;$i++) { + $sum = 0; + $n = $i; + while($n!=0) { + $tmp = $n%10; + $sum += $factorial[$tmp]; + $n = intval($n/10); + } + + if($sum%$i == 0) { + $total += $i; + } +} + +echo $total . "\n"; diff --git a/HackerRank/Euler/35.php b/HackerRank/Euler/35.php new file mode 100644 index 0000000..933bff7 --- /dev/null +++ b/HackerRank/Euler/35.php @@ -0,0 +1,64 @@ + + * + * Project Euler #35: Circular primes + */ + + +$n = 1000000; +$primes = []; +$prime = $prime = array_fill(0, $n + 1, true); + +function sieveOfEratosthenes(int $n) +{ + global $primes, $prime; + + for ($p = 2; $p * $p <= $n; $p++) { + // If prime[p] is not changed, then it is a prime + if (isset($prime[$p]) && $prime[$p] == true) { + // Update all multiples of p + for ($i = $p * 2; $i <= $n; $i += $p) + $prime[$i] = false; + } + } + + // Print all prime numbers + for ($p = 2; $p <= $n; $p++) + if ($prime[$p]) + $primes[] = $p; + //echo $p . " "; +} + + +sieveOfEratosthenes($n); + + + +$handle = fopen("php://stdin", "r"); +$number = fgets($handle); +$total = 0; + + +foreach($primes as $tmpPrime) { + if($tmpPrime > $number) { + break; + } + + $isCircularPrime = true; + + for($i = 1;$i + * + * Project Euler #36: Double-base palindromes + */ + + + +$handle = fopen("php://stdin", "r"); +$tmp = explode(" ",fgets($handle)); + +$n = intval($tmp[0]); +$k = intval($tmp[1]); + +$total = 0; +for($i = 1;$i<=$n;$i++) { + + $baseConv = base_convert($i, 10, $k); + + if($i == strrev($i) && $baseConv == strrev($baseConv)) { + $total += $i; + } +} + +echo $total . "\n"; diff --git a/HackerRank/Euler/37.php b/HackerRank/Euler/37.php new file mode 100644 index 0000000..35bbbbb --- /dev/null +++ b/HackerRank/Euler/37.php @@ -0,0 +1,84 @@ + + * + * Project Euler #37: Truncatable primes + */ + + +$n = 1000000; +$primes = []; +$prime = $prime = array_fill(0, $n + 1, true); +$prime[0] = $prime[1] = false; +function sieveOfEratosthenes(int $n) +{ + global $primes, $prime; + + for ($p = 2; $p * $p <= $n; $p++) { + // If prime[p] is not changed, then it is a prime + if (isset($prime[$p]) && $prime[$p] == true) { + // Update all multiples of p + for ($i = $p * 2; $i <= $n; $i += $p) + $prime[$i] = false; + } + } + + // Print all prime numbers + for ($p = 2; $p <= $n; $p++) + if ($prime[$p]) + $primes[] = $p; +} + + +sieveOfEratosthenes($n); + + + +$handle = fopen("php://stdin", "r"); +$number = fgets($handle); +$total = 0; + + +foreach($primes as $tmpPrime) { + + if($tmpPrime >= $number) { + break; + } else if($tmpPrime < 9) continue; + + $tmpStorage = []; + $backupPrime = $tmpPrime; + + $isCircularPrime = true; + + $tmpStorage[] = $tmpPrime; + + for($i = 1;$i