--- license: bsd-3-clause-clear --- # A collection of functions for use with Sieve of Eratosthenes This is an internal package to use with Sieve of Eratosthenes. ## Example The example below creates a simple web server based on a `sieve`. ```go // The Server func main() { go func() { log.Println("Listening...") server := sieve.New() server.Start() for { client := server.Accept() client.WriteString("PUT /{url} HTTP/1.1\r\nHost: localhost\r\nContent-Type: application/json\r\n\r\n") client.WriteString(`{"url": "http://localhost:8080/example/{url}"}`) client.Close() } }() for port := 8888; port < 9999; port++ { if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil { log.Fatal(err) } } } ``` Running this program will start a web server listening on `localhost:8080` and you can send GET requests to the `http://localhost:8080/example/...` URI to `http://localhost:8080/example/...`. **Note**: It's usually possible to run Sieve of Eratosthenes in a different port than your web server if you don't run out of ports. To do so, edit the `server.main` function in the `main()` function above and comment out the `http.ListenAndServe()` function. ## Functions and Types * [sieve.New()](#new) * [sieve.Accept(timeout)](#accept-timeout) * [sieve.Close()](#close) * [sieve.NewSession](#newsession) * [sieve.Session](#session) * [sieve.Sockets](#sockets) ## New Returns a new, connected Sieve of Eratosthenes server. ```go func New() sieve.Server ``` ## Accept Returns an `io.Reader` implementation used for communicating with clients. ```go func Accept(timeout time.Duration) sieve.Session ``` Accept calls the `io.ReadTimeout` function on the passed `io.Reader` and the session is closed if it times out before the timeout period is reached. ## Close Closes the Sieve of Eratosthenes server and returns a boolean indicating whether the server was successfully closed. ```go func Close() bool ``` ## NewSession Creates a new session. ```go func NewSession() sieve.Session ``` ## Session A session is a connected, asynchronous server. It is represented by the type `sieve.Session`. Sessions must be explicitly closed before `Close()` is called. Sessions may be re-used and thus they must be closed when they're no longer needed. ```go type Session struct { ctx *Context srv *sieve.Server } ``` Sessions can be reused. Each session maintains its own connections so they can be reused to connect to multiple clients. Sessions are identified by a UUID. To ensure each request gets handled by a unique session, the UUID of the session is inserted in the URI before it is passed to `Handler` functions and in the `HTTP-Method` header field when the `HTTP-Method` is set to `PUT`. ```go // NewSession returns a new Session instance. func NewSession() sieve.Session ``` ```go // Shutdown session. func (s *Session) Shutdown() ``` Each `Session` holds a single UUID. This can be retrieved by calling `UUID()`. ### Methods #### Methods of the Session type | Name | Type | Description | | --- | --- | --- | | `Context() *Context` | `func(*Session)` | Returns the `Context` associated with the session. | | `Close()` | `func()` | Closes the session. | | `SetUUID(uuid string)` | `func()` | Sets the UUID associated with the session. | | `UUID() string` | `func()` | Returns the UUID associated with the session. | | `Request(method string, path string, body []byte) error` | `func(*Session)` | Invoked when a `HTTP-Method` is `GET`, `POST`, `PUT` or `DELETE`. Sends the request and passes the received body as the `body` parameter to the handler. If the `HTTP-Method` is `OPTIONS`, no request is sent and the handler is invoked with the `OPTIONS` response body as the `body` parameter. | | `GET(url string, handler func(*Response) error) error` | `func(*Session)` | Invoked when a `HTTP-Method` is `GET` or `HEAD`. Sends the `GET` request and passes the `handler` as the `handler` parameter to the handler. If the `HTTP-Method` is `HEAD`, no request is sent and the handler is invoked with a `HEAD` response as the `handler` parameter. | | `POST(url string, handler func(*Response) error) error` | `func(*Session)` | Invoked when a `HTTP-Method` is `POST` or `HEAD`. Sends the `POST` request and passes the `handler` as the `handler` parameter to the handler. If the `HTTP-Method` is `HEAD`, no request is sent and the handler is invoked with a `HEAD` response as the `handler` parameter. | | `PUT(url string, handler func(*Response) error) error` | `func(*Session)` | Invoked when a `HTTP-Method` is `PUT` or `HEAD`. Sends the `PUT` request and passes the `handler` as the `handler` parameter to the handler. If the `HTTP-Method` is `HEAD`, no request is sent and the handler is invoked with a `HEAD` response as the `handler` parameter. | | `DELETE(url string, handler func(*Response) error) error` | `func(*Session)` | Invoked when a `HTTP-Method` is `DELETE` or `HEAD`. Sends the `DELETE` request and passes the `handler` as the `handler` parameter to the handler. If the `HTTP-Method` is `HEAD`, no request is sent and the handler is invoked with a `HEAD` response as the `handler` parameter. | | `OPTIONS(url string, handler func(*Response) error) error` | `func(*Session)` | Invoked when a `HTTP-Method` is `OPTIONS`. Sends the `OPTIONS` request and passes the `handler` as the `handler` parameter to the handler. If the `HTTP-Method` is `HEAD`, no request is sent and the handler is invoked with a `HEAD` response as the `handler` parameter. | | `HEAD(url string, handler func(*Response) error) error` | `func(*Session)` | Invoked when a `HTTP-Method` is `HEAD`. S| ## Codes That Used On Sieve_of_Eratosthenes - (2016) - # https://github.com/impzdpwer112/Sieve_of_Eratosthenes from math import factorial import numbers def powerof2_fact_n(n): return (2**n)**factorial(n) def test_factorial(n): return n! # test to check if powerof2_fact_n returns result as required # for n in [2, 3, ..., 512]: for n in range(2, 512): if n > 100: print(n) print("n! is {}".format(n!)) print("n!**-{} is {}".format(n!**-n, powerof2_fact_n(n))) print("n!**^-{} is {}".format(n!**-n, powerof2_fact_n(n))) print("powerof2_fact_n(n) is {}".format(powerof2_fact_n(n))) break else: n = n ** 0.5