Skip to content

Instantly share code, notes, and snippets.

@stoimen
stoimen / eratosthenes_sieve.php
Last active March 26, 2017 09:37
Determine if a Number is Prime
<?php
function eratosthenes_sieve(&$sieve, $n) {
$i = 2;
while ($i <= $n) {
if ($sieve[$i] == 0) {
echo $i;
$j = $i;
while ($j <= $n) {
@stoimen
stoimen / linear_search.php
Last active August 29, 2015 14:10
Linear Search in Sorted Lists
<?php
/**
* Performs a sequential search using sentinel
* and changes the array after the value is found
*
* @param array $arr
* @param mixed $value
*/
function sequential_search(&$arr, $value)
{
@stoimen
stoimen / sequential_search.js
Last active August 29, 2015 14:10
Sequential Search (js)
(function() {
var sequential = function(haystack, needle) {
var i = 0,
len = haystack.length
;
for (; i < len; i++) {
if (haystack[i] === needle) {
return true;
}
@stoimen
stoimen / sequential_search_v2.php
Last active August 29, 2015 14:10
Sequential Search v2
<?php
// unordered list
$arr = array(1, 2, 3, 3.14, 5, 4, 6, 9, 8);
// searched value
$x = 3.14;
$length = count($arr);
$index = null;
for ($i = 0; $i < $length; $i++) {
@stoimen
stoimen / sequential_search_reverse.php
Last active February 6, 2016 11:10
Sequential search
<?php
// unordered list
$arr = array(1, 2, 3, 3.14, 5, 4, 6, 9, 8);
// searched value
$x = 3.14;
$index = count($arr);
while ($arr[$index--] != $x);