Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » XML
Language: XML
Expertise: Advanced
Oct 9, 2007

Determine Prime Numbers with XQuery

Did you know you can determine if a number is prime by calling an XQuery query? Here's the code:

<prime_numbers>
    {
    for $a in (1 to 50)
     return(
           if(not(some $counter in (2 to ($a - 1)) satisfies $a mod  
             $contor=0)) then <prime>{$a}</prime>
              else <not_prime>{$a}</not_prime>
           )
    }
</prime_numbers>
And here's a fragment of the result:

<prime_numbers>
   <prime>1</prime>
   <prime>2</prime>
   <prime>3</prime>
   <not_prime>4</not_prime>
   <prime>5</prime>
   <not_prime>6</not_prime>
   <prime>7</prime>
   <not_prime>8</not_prime>
    ...
Leonard Anghel
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date