Algorithms – Find closed forms for the return values


int coffee(int n) {
   int s = n * n;
   for (int q = 0; q < n; q++)
      s = s - q;
   for (int q = n; q > 0; q--)
      s = s - q;
   return s + 2;
}

int tea(int n) {
    int r = 0;
    for (int i = 1; i < n*n*n; i = i * 2)
        r++;
    return r * r;
}

int mocha(int n) {
    int r = 0;
    for (int i=0; i<=n; i = i+16)
        for (int j=0; j

I'm trying to find the return value in terms $ n $ to the coffee. tea. mochabut I'm stuck right now.

I know coffee returns 2 as the code follows:

$ s = n ^ 2 $

$ s = n ^ 2 - displaystyle sum_ {q = 0} ^ {n-1} q = n ^ 2 - dfrac {n (n-1)} {2} $

$ s = n ^ 2 - dfrac {n (n-1)} {2} - displaystyle sum_ {q = 1} ^ nq = n ^ 2 - dfrac {n (n-1)} {2} - dfrac {n (n + 1)} {2} = 0 $

Then, $ s = 0 + 2 $,

I can not figure it out, though tea. mocha, and espressobecause they do not follow +1 steps. Could someone help me how to calculate the return value in terms $ n $?

Algorithms – Determine the return value relative to $ n $


int coffee(int n) {
   int s = n * n;
   for (int q = 0; q < n; q++)
      s = s - q;
   for (int q = n; q > 0; q--)
      s = s - q;
   return s + 2;
}

int tea(int n) {
    int r = 0;
    for (int i = 1; i < n*n*n; i = i * 2)
        r++;
    return r * r;
}

int mocha(int n) {
    int r = 0;
    for (int i=0; i<=n; i = i+16)
        for (int j=0; j

I'm trying to find the return value in terms $ n $ for coffee, tea, mocha, but I'm stuck right now.

I know that coffee returns 2 as the code follows:

$ s = n ^ 2 $

$ s = n ^ 2 - displaystyle sum_ {q = 0} ^ {n-1} q = n ^ 2 - dfrac {n (n-1)} {2} $

$ s = n ^ 2 - dfrac {n (n-1)} {2} - displaystyle sum_ {q = 1} ^ nq = n ^ 2 - dfrac {n (n-1)} {2} - dfrac {n (n + 1)} {2} = 0 $

Then, $ s = 0 + 2 $

but I can not seem to find out tea, mocha and espresso as they do not follow +1 steps. Could someone help me how to calculate the return value in terms $ n $?

Web Development – Most efficient way to return results to a web page using a sorting algorithm

I recently created a search page where the user can enter their search criteria. The server side uses an evaluation and sorting algorithm to first get the & # 39; best & # 39; Return results. The current flow is as follows:

  1. The user enters his search criteria.
  2. Server-side code (PHP) creates a query to retrieve all results that meet these criteria.
  3. The server uses a function to assign a score to each result.
  4. The server goes through all the results and assigns the results from the highest to the lowest score using a "fast sort" algorithm.
  5. The array is spliced ​​to get only the 15 best results. These are then passed to the web page for display as results.

At the end of the result is the button "Load more". If you click this option, the same procedure is performed as above (via AJAX). Instead, the array is offset by a certain amount, but only the next 15 results are taken over and returned to the web page as JSON before being attached to the results via JavaScript.

After testing, this seems really inefficient. I query the database for matching results each time the page loads or clicks Load More, and then rates and sorts each one to get only 15 of those results.

Of course I have to query all matching results to get the & # 39; best & # 39; Get results for the whole table instead of just the 15.

I thought a better way would be to sort and rank the whole table with a cronjob and give each line a "positional evaluation". When the user comes to load the Web page, they can simply query the database for consistent results and sort them in the same SQL statement after the "Position Score" column and then limit the results to 15 (and use offset for the AJAX query). ,

Would this process be much faster? The only downside I can think of is that the reviews are outdated, depending on how often the cronjob sorted the results.

Is it worthwhile to switch to this new procedure, or is my efficiency problem not justified here?

PHP code to make a WHOIS request and return the received data

I've written some PHP to make WHOIS queries and return the retrieved WHOIS record.

First, there is a function for identifying the first level TLS for the domain:

//Return the first-level TLD for a domain
function getTld($domain) {
    //If domain contains dot, find first-level TLD and return it
    if(strpos($domain, ".")) {
        return(pathinfo($domain, PATHINFO_EXTENSION));
    //If domain doesn't contain a dot, just return domain as-is
    } else {
        return($domain);
    }
}

Then the TLD is prefixed to the first level .whois-servers.netand a WHOIS lookup is performed over TCP port 43:

//Return raw WHOIS data for domain
function whois($domain) {
    $whoisServer = getTld($domain) . ".whois-servers.net";
    set_error_handler("whoisWarningHandler", E_WARNING);
    $whois = fsockopen($whoisServer, 43);
    restore_error_handler();
    stream_set_timeout($whois, 3);
    stream_set_blocking($whois, false);
    fwrite($whois, $domain . "rn");
    $read = ($whois); $retries = 1; $null = null;
    while(stream_select($read, $null, $null, 5) === false) {
        if($retries++ >= 5) {
            return(0);
        }
        print("Retrying... (attempt " . $retries . ")");
        sleep(1);
    }
    sleep(1);
    return(filter_var(fread($whois, 65535), FILTER_DEFAULT, FILTER_FLAG_STRIP_HIGH));
}

Finally, there is a custom warning handler that can be used to quit the program if it can not connect to the WHOIS server:

//Custom warning handler for WHOIS fsock/stream
function whoisWarningHandler($errno, $errstr) {
    print("An error occurred while connecting to the WHOIS server.nnError output:nn" . $errstr . "n");
    exit(2);
}

The code can then be written using e.g. print(whois("example.com"));that would print the WHOIS record for example.com,

Could someone advise if the best practices for code quality, efficiency, and security were adhered to and what adjustments / improvements were recommended?

Return URL with PHP

I'm trying to print the url I specified in the address bar on the main screen. The problem is that when you type (for example) localhost: 8000 / school / public / modules -> I add "modules" when I press enter, nothing is returned to me. The idea is to print "modules" and now. In the terminal I get the following:

PHP hint: Undefined index: URL in /root/school/app/libraries/Core.php on line 21

In the Core.php file:

 class Core{
        protected $actualController = 'pages';
        protected $actualMethod = 'index'; 
        protected $parameters = ();


 public function __construct(){
            $url = $this->getUrl();
        }

        public function getUrl(){
            echo $_GET('url'); 
        }

I do not understand why it says that the index is undefined. Thank you, of course.

After approving a task in SharePoint Online, return to the Workflow Status page

Currently, I have set up a document workflow to send participants of a task an email with a specific task to review and approve a document. In the email, I've added a link to the page where you can approve / reject the document. My problem, however, is that once users click Accept or Decline on the form, they will be redirected to the to-do list for the SharePoint site that I do not want.

Instead, I'd like you to be redirected to the workflow status page for the document you approve. I was trying to set the URL in the String Builder to [% Current Task: Form_URN%] & Source = [% Workflow Context: Workflow Status URL%], since I thought this would simply direct the user back to the source URL but that did not work. I received an error related to the workflow instance id.

I'm not sure why that does not work. Is this solution possible? And if so, can it be implemented with SPD and not with code?

uml – Access attribute of the selected return value in the OCL

In OCL, is it possible to access the attribute of a collection returned by a selection operation if the collection contains only one element?

The class diagram in which I need to apply an ocl constraint is:

image

I have to suppress the fact that for Delivery of the product p It is required that the amount of this product in the distributor is greater than 0 and that it is in the distributor.

So I made this OCL restriction:

context Distributor::deliverProduct(p:Product)

pre: self.availableProd->includes(p) and (self.availableProd>select(x|x.id=p.id)).infoAvailability.amountNumber>1 

that's right?

Thanks a lot!

return type deduction decltype – return (x) returns int &

What's happening?

int x = 1;
auto f() { return x; }        // return type is int
const auto& f() { return (x); } // return type is const int&

I can not understand why they are different.
I've checked the documentation and can not determine if it's using an operator (x) or why docltype identifies it as int &.

If you do the test:

int x  = 1;
int& f()
{
    return (x);    // return type is int&, same as decltype((x))
}

int main()
{
    bool es_ref = std::is_reference::value;
    std::cout << es_ref;
}

The issue is true
Which C ++ topic is it? Do you?(x) Is it a kind of Dereference?

Thank you for bringing me to the light.

Function Declaration Documentation - Go to the section Return Type Deduction

Air travel – return flight book, but after another round trip

I am currently in the USA. I plan to return to my home country (UK) on October 18th, but I have not booked it yet.

I already booked a trip to the US with a relative in November for a week.

As we know, it is expensive to book a one way flight. Can I book a return flight to leave on the 18th and return next year?

For example:

  • October 18, 2019 US -> Home (Ticket 1)
  • November 5, 2019 Home -> USA (Ticket 2)
  • November 12, 2019 US -> Home (Ticket 2)
  • Some time in March 2020 Home -> USA (Ticket 1)

I would not cancel the ticket (2) after I flew home. It is likely that I would really use the return segment to return and then have another one-way ticket in March to return home.

Is that possible?
Many thanks