Skip to content

Instantly share code, notes, and snippets.

@akanehara
Created May 6, 2014 02:12
Show Gist options
  • Save akanehara/441098a65e6edb08d640 to your computer and use it in GitHub Desktop.
Save akanehara/441098a65e6edb08d640 to your computer and use it in GitHub Desktop.
Ginqのunfoldで余再帰sieve。非効率きわまりないお遊びなので実用しないでね。
<?php
require_once("vendor/autoload.php");
$primes = Ginq::from([2])->concat(Ginq::unfold(
Ginq::range(3,null,2),
function($xs) {
$x = $xs->first();
return [
$x,
$xs->drop(1)->filter(['y'=>'y % x != 0', 'x'=>$x])
];
}
));
foreach($primes as $n) {
echo "$n\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment