Categories
Coding

Memory Usage Statistics via Bash

I was just experimenting with a couple of useful commands (one is basically from the ps man page), and I want to record them here before I forget them.

Firstly,

ps -o pid,pmem,user --sort pmem --user [myuser]

Will show the currently running processes for [myuser], and for each process, the percentage of RSS (resident set size) of that process with respect to the total amount of physical memory on the machine. Very useful. If you want to see the entire process virtual size, add the vsize field to the field list above.

Secondly:

echo `cat /proc/meminfo | /bin/awk '/MemTotal/ {tot=$2} /MemFree/ {print $2/tot}'`

I’m sure there is a much nicer way to do this in awk, but I am an awk newbie. This returns the amount of free memory expressed as a percentage of total physical memory.

Awk doesn’t seem to have a round() function, so a version with more readable output would be:

echo `cat /proc/meminfo | /bin/awk '/MemTotal/ {tot=$2} /MemFree/ {print int($2/tot*10
0) "% memory free"}'`

Categories
Coding Project Euler R

Project Euler Problem #1 (R)

Here is a solution for Project Euler’s problem #1 in R. The problem is expressed as:

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

As usual with Project Euler questions, there is an obvious way, and a less obvious, but much more efficient way. In this case, the obvious way is:

[code lang=”R”]
x < – seq(1,999)
sum(x[x %% 3 ==0 | x %% 5 == 0])
[/code]

Which very concisely returns the correct answer, 233168. However, if we use the following intuition:

\[S_N = S_3 + S_5 – (S_{3,5})\]

i.e. the sum of all numbers divisible by 3 or 5 is the sum of all numbers divisible by 3, plus the sum of all numbers divisible by 5, minus the sum of all numbers divisible by 3 and 5 (as we have double counted them), then we get the correct answer.

Since \[S_n = \frac{n(a_1 + a_n)}{2}\], where \(n = \lfloor \frac{N}{n} \rfloor\), and \(a_n = a_1n\), the last piece of the puzzle is what to use for \(S_{3,5}\). This is straightforward, we just use the lowest common multiple of 3 and 5, which in this case is 15. Hence, the R representation of this is:

[code lang=”R”]sum(333*((3+333*3)/2),199*((5+199*5)/2)-66*((15+66*15)/2))[/code]

Categories
Coding

MathTran

MathTran is a very popular (and useful) web service which performs a similar function to Google Charts – hit it with a valid TeX formula and it will return an image representation of the formula. Jonathan Fine has written a JavaScript wrapper which enables you to mark up img tags with alt values like “tex:[formula]” and these are automagically converted to images.

For example, the tag

<img alt="tex:\frac{1}{\sqrt{2\pi}\sigma}\int_{-\infty}^{x}{e^{-\frac{(x-\mu)^2}{2\sigma^2}}}dx">

will be converted to:

tex:\frac{1}{\sqrt{2\pi}\sigma}\int_{-\infty}^{x}{e^{-\frac{(x-\mu)^2}{2\sigma^2}}}dx

I have taken the MathTran source script and modified it slightly – MathTran allows you to specify a size parameter, between 1 and 9. This is not available via the default script, but I added the ability to have the img tag take a “size” parameter. This can be used like so:

<alt="tex:\sum_{0}^{\infty}x_i = \frac{1}{(1-x)}" size="2">

An example below:

tex:\sum_{i=0}^{\infty}x^i = \frac{1}{(1-x)}
tex:\sum_{i=0}^{\infty}x^i = \frac{1}{(1-x)}
tex:\sum_{i=0}^{\infty}x^i = \frac{1}{(1-x)}
tex:\sum_{i=0}^{\infty}x^i = \frac{1}{(1-x)}
tex:\sum_{i=0}^{\infty}x^i = \frac{1}{(1-x)}
tex:\sum_{i=0}^{\infty}x^i = \frac{1}{(1-x)}
tex:\sum_{i=0}^{\infty}x^i = \frac{1}{(1-x)}
tex:\sum_{i=0}^{\infty}x^i = \frac{1}{(1-x)}
tex:\sum_{i=0}^{\infty}x^i = \frac{1}{(1-x)}

UPDATE: I made a slight modification to add the raw TeX source to the title attribute of the img tag, so if you hover on a generated MathTran image, you should see the original formula.

UPDATE (2012) – this doesnt work anymore – now using the lovely MathJAX!

If you want to get the (modified) MathTran script, it is here: http://www.theresearchkitchen.com/blog/wp-includes/js/mathtran.js