How to divide by 9 really, really fast (and why it works)

How to divide by 9 really, really fast (and why it works)

An interesting post on Hacker News shows tricks for dividing by 9. The fastest trick goes like this. Suppose you want to do 53876 / 9 in your head. You do the following:

1. The first digit is going to be the same as in the left hand side, so it's 5.

2. The second digit is the first digit plus the second digit: 5 + 3 = 8

3. The third digit is the previous answer plus the third digit: 8 + 8 = 16. Because that's bigger than 10 carry it to the digit above (which is now 9) and just take the rest. So it's 6.

4. The fourth digit is previous answer plus the fourth digit: 16 + 7 = 23. Again carry the tens to the preceding digit (which now becomes 8) and you're left with 3.

5. The last digit is the sum of all the digits: 23 + 6 = 29. For this final digit it's necessary to work out how many times 29 can be divided by 9 (it's 3). That gets added to the previous digit which becomes 6. The remainder (29 - 9 x 3 = 2) is the remainder of the whole calculation.

So, reading off the digits 53876 / 9 = 5986 remainder 2.

Why does this work? To show why I'll do a smaller example of a three digit number (with digits a, b, c) being divided by 9. The calculation abc/9 can be written as:

(a x 100 + b x 10 + c) / 9

=  a x 100 + b x 10 + c
   -------   ------   -
      9         9     9

=  a x (11 + 1/9) + b x (1 + 1/9) + c/9

=  a x 11 + b x 1 + (a + b + c)
                    -----------
                         9

=  a x 10 + a x 1 + b x 1 + (a + b + c)
                            -----------
                                 9

=  a x 10 + (a + b) + (a + b + c)
                      -----------
                           9
So, the first digit depends on a and any carry from (a + b), the second digit depends on (a + b) and any carry from the final term, and the only division left is the division of (a + b + c) by 9 and so that's where the remainder comes from.

Exactly the same pattern appears for longer numbers. Just be sure to carry; for very long numbers there could be a lot of carrying.

(Also fun, Squaring two digit numbers in your head.)

Labels:

If you enjoyed this blog post, you might enjoy my travel book for people interested in science and technology: The Geek Atlas. Signed copies of The Geek Atlas are available. Looking for a new job? Try UseTheSource.

posted by John Graham-Cumming at 16:06 Permalink

R Google Maps

From Evernote:

R Google Maps

Clipped from: https://www.google.com/reader/view/?hl=en&tab=cy

Visualizing GIS data with R and Open Street Map

from R-bloggers by gerhi

 

(This article was first published on Sustainable Research » Renglish, and kindly contributed to R-bloggers)

 

In this post I way to share with you some code to use Openstreetmap – maps as a backdrop for a data visualization. We will use the RgoogleMaps-package for R. In the following I will show you how to make this graph.



1. Download the map

I wanted to take a closer look at an area around my former neighborhood, which is in Bochum, Germany.

lat_c<-51.47393
lon_c<-7.22667
bb<-qbbox(lat = c(lat_c[1]+0.01, lat_c[1]-0.01), lon = c(lon_c[1]+0.03, lon_c[1]-0.03))

Once this is done, you can download the corresponding Openstreetmap tile with the following line.

OSM.map<-GetMap.OSM(lonR=bb$lonR, latR=bb$latR, scale = 20000, destfile=”bochum.png”)

2. Add some points to the graphic

Now your second step will most likely be adding points to the map. I choose the following two.

lat <- c(51.47393, 51.479021)
lon <- c(7.22667, 7.222526)
val <- c(10, 100)

As the R-package was mainly build for google-maps, the coordinates need to be adjusted by hand. I made the following functions, that take the min and max value from the downloaded map.

lat_adj<-function(lat, map){(map$BBOX$ll[1]-lat)/(map$BBOX$ll[1]-map$BBOX$ur[1])}
lon_adj<-function(lon, map){(map$BBOX$ll[2]-lon)/(map$BBOX$ll[2]-map$BBOX$ur[2])

Now you can add some points to the map. If you want them to mean anything it may be handy to specify an alpha-level and change some aspects of the points, e.g. size, color, alpha corresponding to some variable of interest.

PlotOnStaticMap(OSM.map, lat = lat_adj(lat, OSM.map), lon = lon_adj(lon, OSM.map), col=rgb(200,val,0,85,maxColorValue=255),pch=16,cex=4)

Here is the full code:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
require(RgoogleMaps)   #define the part of the world you want to plot. Here the area around my former home. lat_c<-51.47393 lon_c<-7.22667 bb<-qbbox(lat = c(lat_c[1]+0.01, lat_c[1]-0.01), lon = c(lon_c[1]+0.03, lon_c[1]-0.03))   # download the tile from OSM OSM.map<-GetMap.OSM(lonR=bb$lonR, latR=bb$latR, scale = 20000, destfile="bochum.png") image(OSM.map) #Add some coordinates lat<- c(51.47393, 51.479021) lon<- c(7.22667, 7.222526) val <- c(0, 255)   #function to adjust the coordinates lat_adj<-

JQuery Lessons

From Evernote:

JQuery Lessons

<script>

(function() {
$('article').append('Hello from the Javascript'); //also prepend
})();

//note that this last bit () calls the anonymous function

</script>


<script>

(function() {
$('h1').after('Hello from the Javascript');
})();

</script>


<script>

(function() {
$('p').first().before('Hello from the Javascript');
})();

</script>

Create an H2 tag:

<script>

(function() {
$('<h2></h2>', {
text: 'Hello from the Javascript',
class: 'myClass',
id: 'myCrap'
}).insertAfter('h1');    //also appendTo, prependTo etc

})();

</script>


<script>

(function() {
$('<h2></h2>', {
text: 'Hello from the Javascript',
class: 'myClass',
id: 'myCrap'
}).insertBefore('p:nth-child(3)'');    

})();

</script>

Move content:

<script>

(function() {

$('h1').appendTo('article'); //this will move an existing h1 to where you told it to

})();

//be specific to avoid duplications

</script>

<script>

(function() {

$('p').eq(0).after(function() {
return $(this).prev();
});

</script>

<script>

(function() {

var co =  $('span.co").each(function() {
     $('blockquote></blockquote>', {
     class: 'co',
     test: $this.closest('p') );
     $(this). closest('p').prepend (

}

});

</script>