When a Domain is Blocked by Filtering Software

Posted by Chief on Aug 10, 2010 in Reference, Scripts, Tricks and Hacks
No Comments

Can’t access your favorite websites because some stupid ISP or access point is using DNS-level filtering?

Set your DNS to use Google’s DNS:
8.8.8.8 and 8.8.4.4

Then flush the existing DNS cache and register the new DNS.

In Windows 7, you’ll need elevated privileges to changes to the DNS. Make sure you start cmd.exe as Administrator.

  1. Press the window Key
  2. Type “cmd”
  3. Right click the cmd.exe that’s found.
  4. Select “Run as administrator”
  5. Type ipconfig /flushdns
  6. Then type ipconfig /registerdns

Now you’re free to roam the internet.

Tags: , , , , , , , ,

Fast Hash Function for Strings

Posted by Chief on Jul 26, 2010 in Reference
No Comments

Pre-condition: str is null terminated.

int hash(const void * str) {
  int val = 0;
  const char *ptr;
  int tmp;
 
  while (*ptr != '\0') {
    val = (val << 4) + (*ptr);
    if (tmp = (val & 0xf0000000)) {
      val = val ^ (tmp >> 24);
      val = val ^ tmp;
    }
  }
  ptr++;
  return val;
}

Or in PHP:

function hash($str) { 
  var $val = 0;
  for($i=0,$n=strlen($str); $i<$n; $i++) {
    $val = ($val<<4) + ord($str{$i});
    if( $tmp = ($val & 0xf0000000) ) { 
      $val = $val ^ ($tmp>>24);
      $val = $val ^ $tmp;
    }
  }
  return $val;
}

Now all you have to do is mod the returned value by the size of the hash table and you have the target address for the first pass of a hash table with double-hashing.

Tags: , , , , , , , ,

Log Math, Compound Interest, and the Geometric Series

Posted by Chief on Jun 29, 2010 in Reference
No Comments

Compute the value of a $1000 deposit after 10 years, earning 1.5% APR, compounded monthly.

Naive:

function interest($initial=1000,$apr=0.015,$years=10) {
  $rate = $apr/12;
  $periods = 12*$years;
  $sum = $initial;
  for($i=0; $i<$periods;$i++) {
    $sum = $sum + $sum * $rate;
  }
  return $sum;
}

Log style:

function interest($initial=1000,$apr=0.015,$years=10) {
  $rate = $apr/12;
  $periods = 12*$years;
  return $initial * pow(1 + $rate, $periods);
}

Watch the boundary conditions. The above assumes the compounding occurs 12 times. Note that this log approach would not be appropriate if we were to add money to the fund after the compounding began. If the amount added per compounding period were consistent, we could devise a new log scheme.

Naive with additions:

function interest($initial=1000,$addition=100,$apr=0.015,$years=10) {
  $rate = $apr/12;
  $periods = 12*$years;
  $sum = $initial;
  for($i=0; $i<$periods; $i++) { 
    // Add interest earned in previous term.
    // Add $addition in preparation for next compounding period.
    $sum = $sum + $sum*$rate + $addition;
  }
  // Remove last $addition because there is no addition at the end-of-term.
  return $sum - $addition;
}

Log (via Geometric Series) with additions:

/**
 * Compute the balance on an account after some period of time.
 * The account may receive an initial deposit and/or regular deposits
 * at the start of every compounding period, after the first.
 * The APR, duration, and number of compounding periods may be specified.
 *
 * @see http://en.wikipedia.org/wiki/Geometric_series
 * @param float $inital Initial deposit amount
 * @param float $addition Amount added at start of each compounding period
 * @param float $apr Annual percentage rate (percent/100) earned on balance
 * @param float $years Number of years ($years>0) to compute interest for.
 * @param uint $ppyr Number of compounding periods per year.
 * @return float Balance after $years time accruing interest at $apr rate.
 */
function interest($initial=1000,$addition=100,$apr=0.015,$years=10,$ppyr=12) {
  // Use geometric series to calculate the cumulative rate of return
  // with recurring, inter-period deposits (periods 0..N-1)
  // $initial is initial deposit, interest accrues immediately.
  // $rate is interest earned per period (percent/100).
  // $addition is amount added at beginning of period after first period.
 
  // Over time, interest accrues on the additional deposits:
  // let R = (1 + rate)
  // v0 = 0  -- we'll deal with the initial deposit elsewhere.
  // v1 = v0 * R + additional
  // v2 = (v1) * R + additional
  // v2 = (v0 * R + add) * R + add
  // v2 = 0 + add*R + add
  // v2 = add*R + add
  // v3 = v2 * R + add
  // v3 = (add*R + add)*R + add
  // v3 = add(R^2 + R + 1)
  // v4 = v3 * R + add
  // v4 = (add*R*R + add*R + add)*R + add
  // v4 = add(R^3+R^2+R^1+R^0)
  // v5 = v4 * R + add
  // v5 = (v3 * R + add) * R + add
  // v5 = ((v2 * R + add) * R + add) * R + add
  // v5 = (((v1 * R + add) * R + add) * R + add) * R + add
  // v5 = ((((v0 * R + add) * R + add) * R + add) * R + add) * R + add
  // v5 = add * (R^4 + R^3 + R^2 + R^1 + R^0)
  // ... using geometric series ... a = add, r = (1+rate)
  // vN = a * (1 - pow(r,N)) / (1-r)
 
  $rate = $apr / $ppyr;
  $periods = $years * $ppyr;
 
  // Geometric series:
  // a + ar + ar^2 + ar^3 + ... + ar^n 
  //   = sum(k=0..n)[ar^k]
  //   = a * (1-r^(n+1)) / (1-r)
  // Where:
  //   a = $addition
  //   r = 1 + $rate
  //   n = $periods - 1
  //   k = [0..n]
  $a = $addition;
  $r = 1 + $rate;
  // Compute series from first period to last period
  $geom = $a * (1 - pow($r,$periods)) / (1-$r);
  // Remove $add from last period because next period didn't begin yet.
  $additional = $geom - $addition;
 
  // Compute interest on initial deposit + additional interest from above.
  return $initial * pow(1+$rate,$periods) + $additional;
}

Same as above, but without all those comments:

function interest($initial=1000,$addition=100,$apr=0.015,$years=10,$ppyr=12) {
  $R = 1 + $apr / $ppyr;
  $pow = pow( $R, $years * $ppyr );
  return ($initial * $pow) + ($addition * (1 - $pow) / (1-$R) - $addition);
}

Tags: , , , , , , ,

MySQL config and iptables

Posted by Chief on Jun 13, 2010 in Reference
No Comments

This post is for reference.

Of note, there is a good example of using iptables, and a description of how to bind your local mysql client to a remote mysql server.

http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html

Tags: ,

Restore Close Button to Right in Ubuntu 10.04

Posted by Chief on Jun 13, 2010 in Reference, System Administration
No Comments

gconftool-2 --set "/apps/metacity/general/button_layout" --type string "menu:minimize,maximize,close"

Ref: LaunchPad » Ubuntu » Questions » Question #108951

Tags: , , , ,

Rent vs Buy – In Regards to Housing

Posted by Chief on Jun 3, 2010 in Quotes, Reference
No Comments

The cross-over point is about 15 times annual rent, [trulia] believes. In other words, as a rough rule of thumb, homes are probably fairly valued in a city when they cost about 15 times a year’s rent. So, for example, if you’re paying $10,000 a year to rent a place, think twice about buying a home that costs more than $150,000. Dean Baker, economist at the Washington, D.C. think-tank The Center for Economic and Policy Research, came to a similar conclusion in research on the subject in recent years. Fifteen times is the historic average, he said. Brett Arends at the Wall Street Journal

Tags: , , , ,

Copyright © 2010 cat brain.log | less All rights reserved.
Shades v1.2 theme from BuyNowShop.com.