Server Control-exposure property: primegenerator control example-Note (2)

Source: Internet
Author: User

Create a solution and two more projects

One is the control project. Modify the compilation result path in the property to the bin directory of the second project.

The first project is the Control Project:

Using system;
Using system. Web. UI;

Namespace mspress. servercontrols
{
Public class primegenerator: Control
{
Public int number {Get; set ;}
Protected override void render (htmltextwriter writer)
{
Int [] primes = sieve. getprives (number );

Writer. Write ("primes less than or equal :");
Writer. Write (number );
Writer. Write ("<br> ");
For (INT I = 0; I <primes. length; I ++)
{
Writer. Write (primes [I]);
Writer. Write (",");
}
Writer. Write ("<br> ");
}
}
}

 

____

Class for calculating prime numbers:

Using system;
Using system. collections;

Namespace mspress. servercontrols
{
Internal sealed class sieve
{
Private sieve (){}
Public static int [] getprives (int n)
{
Bitarray flags = new bitarray (n + 1, true );
For (INT I = 2; I <= (INT) math. SQRT (n); I ++)
{
If (flags [I])
{
For (Int J = I; j * I <= N; j ++)
{
Flags [I * j] = false;
}
}
}
Int COUNT = 0;
For (INT I = 2; I <= N; I ++)
{
If (flags [I]) Count ++;
}

Int [] primes = new int [count];

For (INT I = 2, j = 0; j <count; I ++)
{
If (flags [I]) primes [J ++] = I;
}
Return primes;
}
}
}

 

___________

The second project is to use the server control class:

Add:

<% @ Register tagprefix = "prime" namespace = "mspress. servercontrols" assembly = "mspress. servercontrols" %>

And

<Prime: primegenerator id = "simple1" runat = "server" number = "19"/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.