Programming

Summation of a Partial Beatty Sequence

 \sum_{i=1}^{n}\mathcal{B}_{r\in\mathbb{Z}}^i = \sum_{i=1}^{n}\lfloor ir \rfloor

A partial Beatty sequence is the sum of all the Integer portions of positive multiples between 1 and n of an Irrational number.

Raleigh-Beatty Theorem

Rayleigh’s theorem, states that a sequence, consisting of all the positive integers that are not in the sequence, is itself a Beatty sequence generated by a different irrational number.

For any Beatty \mathcal{B}_r^n sequence where r > 1 there exists a complementary sequence \mathcal{B}_s^n where s>1 is defined as \frac{1}{r}+\frac{1}{s}=1. The union of both sequences is the set of all Integers from 1 to n.

We can find an equation for s in terms of r.

\begin{align}
\begin{gather*}
\frac{1}{s} + \frac{1}{r} = 1 \\ \downarrow \\
\frac{1}{s} = 1 - \frac{1}{r} \\ \downarrow \\
\frac{1}{s} = \frac{r}{r} - \frac{1}{r} \\ \downarrow \\
\frac{1}{s} = \frac{r-1}{r} \\ \downarrow \\
s = \frac{r}{r-1}
\end{gather*}
\end{align}

The complement consists of all integers not in the sequence. For any sequence dividing the value by the equation will give you the index of the value that is less than N = \mathcal{B}_r^n. This allows us to find the last index of the complement because the last value of the complement is lower than that of the sequence. \lfloor \frac{\mathcal{B}_r^i}{s} \rfloor.

\lfloor ir \rfloor = \mathcal{B}_r^i \rightarrow \lfloor \frac{\mathcal{B}_r^i}{r} \rfloor = i-1

\mathcal{B}_r^n is the largest term in the sequence, dividing that by ‘r’ would give you the index of the sequence .

For clarity we will express the largest value of the sequence as N = \mathcal{B}_r^i , and the upper index of the complement as m = \lfloor N/s \rfloor.

\begin{align}
\begin{gather*}
N = \mathcal{B}_r^n\\ \\
m = \lfloor N/s \rfloor\\ \\
\sum_{i=1}^{n}\lfloor ir \rfloor + 
\sum_{j=1}^m
\lfloor js \rfloor = 
\frac{N^2 + N}{2}
\end{gather*}
\end{align}

Building up to the Solution

Since the union of any Beatty sequence and its complement will cover all the positive integers up to n. This leads to the realization that any Integer between 1 and n can be expressed at either \lfloor ir \rfloor or \lfloor j(r+1) \rfloor.

The sum of the sequence in question:

\begin{align}
S(n, r) = \sum_{i=1}^{n}\mathcal{B}_r^i
\end{align}

The sum of the complementary sequence:

\begin{align}
S(m, s) = \sum_{j=1}^{m}\mathcal{B}_s^j | s = \frac{r}{r-1}
\end{align}

By way of the Raleigh-Beatty Theorem, the two sums are equivalent to the sum of the Integers from 1 to the highest Beatty sequence value (\mathcal{B}_r^n). This can be rearrange to say; the sum of a Beaty sequence is equal to the difference sum of all the encompassed Integers and the sequence’s complement.

\begin{align}
\begin{gather*}
S(n,r) + S(n,s) =
\sum_{i=1}^Ni \\
\downarrow \\
S(n,r) = 
\sum_{i=1}^Ni - 
S(n,s) 

\end{gather*}
\end{align}

At this point we want to express the sum of the complement in terms of sum the original sequence. We will do this by extracting \mathcal{B}_r^i from \mathcal{B}_i^s. So we need to define a value for s, we will use \sqrt{2}.

Expressing the Complement in Terms of the Original

Starting with r and s (1), we assign a value to r.

\begin{align}
r = \sqrt{2}, s= \frac{r}{r-1}
\end{align}

Substitute r for \sqrt{2}

\begin{align}
s= \frac{\sqrt{2}}{\sqrt{2}-1}
\end{align}

Factor out \sqrt{2}

\begin{align}
\begin{gather*}
s= 1-\frac{\sqrt{2}(\sqrt{2}+1)}{(\sqrt{2}-1)(\sqrt{2}+1)} \\
\downarrow\\
s= \frac{2+\sqrt{2}}{1} \\
\downarrow\\
 s=2+\sqrt{2}
\end{gather*}
\end{align}

Whole integers can be brought out of the floor function.

\begin{align}
\begin{gather*}
\mathcal{B}_s^i = \lfloor is\rfloor=\lfloor i(2+\sqrt{2})\rfloor \\
\downarrow \\
\lfloor 2i+i\sqrt{2}\rfloor \\
\downarrow \\
2i+\lfloor i\sqrt{2}\rfloor \\
\end{gather*}
\end{align}

Express \mathcal{B}_s^i in terms of \mathcal{B}_r^i.

\begin{align}
\begin{gather*}
\text{since } \mathcal{B}_r^i=\lfloor i\sqrt{2}\rfloor \\ 
\downarrow\\
\mathcal{B}_s^i = \mathcal{B}_r^i + 2i
\end{gather*}
\end{align}

Recursive Function

Express the complement sum in terms of the original sum.

\begin{align}
\begin{gather*}
S(n,s)=
\sum_{i=1}^n \mathcal{B}_s^i\\
\downarrow\\
\sum_{i=1}^n(\mathcal{B}_r^i+2i) \\
\downarrow\\
\sum_{i=1}^n\mathcal{B}_r^i+\sum_{i=1}^n2i \\
\downarrow\\
\sum_{i=1}^n\mathcal{B}_r^i+2\sum_{i=1}^ni \\
\downarrow\\
S(n, r) + 2(\frac{n(n+1)}{2}) \\
\downarrow\\
S(n,s)=S(n, r) + n(n+1)

\end{gather*}
\end{align}

From (5) using (11)

\begin{align}
\begin{gather*}
S(n,r) =  
\sum_{i=1}^Ni -
S(m,s) \\
\downarrow \\
S(n,r) = 
\sum_{i=1}^Ni - S(m,r) - m(m-1)
\end{gather*}
\end{align}

Expressed as a recursive function.

\mathcal{S}(n, r)= 
\begin{cases}
0 \leq n \leq 1 & 1 \\
n > 1 & 
\sum_{i=1}^Ni - S(m,r) - m(m-1)
\end{cases}

External References

https://en.wikipedia.org/wiki/Beatty_sequence

phpMyAdmin

About

The phpMyAdmin tool is an SQL database manager written in PHP. It resides in the web root on your server, making it easily accessed from a web browser.

There are a number important security considerations when using software like phpMyAdmin, since it:

  • Communicates directly with your MySQL installation.
  • Handles authentication using MySQL credentials.
  • Executes and returns results for arbitrary SQL queries.

The phpMyAdmin tool is available for a number of different operating systems. This article will focus solely on the Linux installation.

Manual Installation

While there are a number of ways to install phpMyAdmin, there is an easy quick install method. This involves downloading and extracting it directly to your web root directory. While you will need at least basic bash terminal knowledge, it is relatively trivial to set up. However, you will require sudo privileges or access to the web-user-account.

Following are the condensed steps for the quick install found in the phpMyAdmin documentation.

cd /www
sudo wget https://files.phpmyadmin.net/phpMyAdmin/5.1.1/phpMyAdmin-5.1.1-english.tar.gz -O phpMyAdmin.tar.gz
sudo tar -xvf phpMyAdmin.tar.gz
sudo chown -R wp-user:wp-user phpMyAdmin-5.1.1-english/
sudo cp config.sample.inc.php config.inc.php
  1. Change directory to your web root.
  2. Download the tar file. You can choose from a number of options here. We rename it here with the -O flag
  3. Unpack the tar, it will be placed in it’s own directory.
  4. Give ownership of the directory to the web user account.
  5. Create the configuration file by copying the sample configuration file.

Usage

When a user logs into phpMyAdmin the username and password are sent directly to the SQL database. It is just an interface to the database, and any operation it does can be done on the command line. As such, all users must be valid database users.

Securing

You should use cookie mode authentication so that your user/password pair are not kept in the configuration file. The variable may be set in the example config file as:

$cfg['Servers'][$i]['auth_type'] = 'cookie';

You will need to also add a 'blowfish secret' value to the config file.

$cfg['blowfish_secret'] = 'anyrandomtextyouwant';

Deny access to the temp, libraries, and templates subdirectories. Put the following in the server directive of your nginx enabled sites file.

location /phpmyadmin/libraries { deny all; }
location /phpmyadmin/templates { deny all; }
location /phpmyadmin/tmp { deny all; }

References

https://www.phpmyadmin.net/

https://docs.phpmyadmin.net/en/latest

NodeJS Code Coverage With C8

Code coverage allows you to determine how well unit-tests cover your codebase. The standard code coverage suite for JavaScript is the NYC interface for Istanbul. However, this package doesn’t play well with NodeJS modules. Instead we are going to use the C8 package. C8 uses functionality native to NodeJS to create output that is compatible with Istanbul reporters.

Installation

npm i --save-dev c8

Execution

You can run c8 on any source file by simply passing in the appropriate node command.

$ npx c8 node src/index.js
-----------|---------|----------|---------|---------|-------------------
File       | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files  |   88.23 |      100 |      50 |   88.23 |
 MooYou.js |   84.61 |      100 |      50 |   84.61 | 9-10
 index.js  |     100 |      100 |     100 |     100 |
-----------|---------|----------|---------|---------|-------------------

You can also easily run it with your Mocha test files.

$ npx c8 mocha
  MooYou Class Test
    #who
      ✔ returns 'you'


  1 passing (3ms)

-----------|---------|----------|---------|---------|-------------------
File       | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files  |   84.61 |      100 |      50 |   84.61 |
 MooYou.js |   84.61 |      100 |      50 |   84.61 | 9-10
-----------|---------|----------|---------|---------|-------------------

Configuration

C8 can be configured from the command line, in your package.json file, or in a configuration file in your project directory. In particular you can instruct C8 to report one files as opposed to just those in your tests (npx c8 --all mocha). You can also specify which files to include and/or ignore. See the configuration documentation for specific details.

Generating Reports

By default the coverage data will be located in the coverage/tmp directory. You can change this directory with the --temp-directory flag. Don’t forget to add this to your .gitignore file.

Run npx c8 report to regenerate reports after c8 has already been run. Use the -r flag to specify which reporter to use, and the -o flag to specify where to output the report. By default generated reports can be found in the coverage/ dirctory.

Vanilla C8 command with HTML report:

npx c8 -r html mocha

Generate C8 report after the fact:

npx c8 -r html report

Two-part C8 command with custom directories and HTML report:

npx c8 --temp-directory .c8 mocha
npx c8 report --temp-directory .c8 -o html_coverage -r html

In Code Markup

You can add comments into your source code that tells c8 to ignore certain portions of code.

Ignoring all lines until told to stop.

/* c8 ignore start */
function foo() {
  ...
}
/* c8 ignore stop */

Ignore next line.

/* c8 ignore next */

External References

https://www.npmjs.com/package/c8

https://github.com/bcoe/c8

Git: Checkout a Single File from a Previous Commit

Often we alter or delete a file that we didn’t really want to. Sometimes this goes unnoticed for a few commits. It’s fairly easy in git to retrieve a specific file from a specific commit.

git checkout <commit> <path>
git checkout f08b32 ./src/main.c
git checkout HEAD~2 ./src/main.c

How to reattach a GIT detached head.

$ git status
HEAD detached at 36bc359
nothing to commit, working tree clean

$ git branch fix-detached
$ git checkout main
$ git merge fix-detached -Xtheirs
$ git branch -d fix-detached

Updating NPM Packages

Updating NPM Packages

  1. Navigate to the root directory of your project and ensure it contains a package.json file:
    • cd /path/to/project
  2. In your project root directory, run the outdated command to view outdated packages:
    • npm outdated
  3. In your project root directory, run the update command:
    • npm update

package.json

The way the wanted version is set in the package.json file may affect how updates are decided.

Version numbers are declared as a [major, minor, patch] tuple.

Caret Ranges ^1.2.3

Allows changes that do not modify the left-most non-zero element of the version specification. So 1.3.1 will update to 1.3.2 or 1.4.0. Though, 0.3.0 will update to 0.3.1 but not update to 0.4.0.

Version will update if:

  • ^1.2.3 : 1.2.3 < version < 2.0.0
  • ^0.3.0 : 0.3.0 < version < 0.4.0

X-Ranges

Allows ‘X’, ‘x’, or ‘*’ to stand in for a number in the version specification.

Version will update if:

  • “” : any version
    • same as “*” or “x.x.x”
  • 1.x.x : 1.0.0 < version < 2.0.0
    • same as “1”
  • 1.2.x : 1.2.0 < version < 1.3.0
    • same as “1.2”