Skip to content

Instantly share code, notes, and snippets.

@xquery
Created September 1, 2011 07:46
Show Gist options
  • Select an option

  • Save xquery/1185659 to your computer and use it in GitHub Desktop.

Select an option

Save xquery/1185659 to your computer and use it in GitHub Desktop.
XQuery prime number generation
(: simple - http://e-blog-java.blogspot.com/2010/03/determine-prime-numbers-with-xquery.html :)
<primes>
{
for $a in (2 to 1000)
return(
if(not(some $counter in (2 to ($a - 1)) satisfies $a mod
$counter=0)) then {$a}
else {$a}
)
}
</primes>
(: Chris Wallace - http://kitwallace.posterous.com/the-prime-sieve-in-xquery :)
declare function local:sieve($primes,$nums) {
if (fn:exists($nums))
then
let $prime := $nums[1]
return
local:sieve(($primes,$prime), $nums[. mod $prime != 0])
else $primes
};
<primes>
{
local:sieve((),2 to 1000)
}</primes>
(: Jirka Kosek ' slow'n'short:' - http://twitter.com/jirkakosek :)
<primes>
{
for $i in (2 to 1000) return if (every $j in (2 to $i - 1) satisfies $i mod $j ne 0) then $i else ()
}
</primes>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment