You are currently browsing the category archive for the ‘Javascript’ category.

A MathCircle is where a group of young people get together and work on some interesting Math-related topics introduced by a mentor.

The topics are sometimes problems like you find on or Math Competitions [ vis AMC Sample Questions ].  They can cover quirky topics that aren’t normally seen in the school curriculum, such as Catalan Numbers.

The idea sounds geeky, and is un-apologetically so, yet has the potential to engage students who might be bored with the traditional material.   It preps interested students for careers in science / finance / medicine / engineering and helps the school do well in math and science competitions.  Math Circle meets can also be a lot of fun.


Question : “Why do we need more math.. we already do that in school, right?”

Answer : “yeah..but you don’t learn enough Tennis in PE class to become a club player, and you don’t get enough Instrument practice in Music class to get into music school .. you really need to train, to play in an ensemble and have dedicated practice for that.”

MathCircle takes the same approach of intensive practice which you find in Basketball practice, Dance club, Swim meet or musical instrument group … except focused on math-related skills.

Practice with Duration + Intensity

One important aspect that both Math problem solving and programming share is the sustained concentration on one task – in my opinion we have gone too far in the direction of byte size chunks of learning.  Sometimes you need to chew on things for a while.

We can look at how Basketball and Music are learnt, and apply what works to math.   Training sessions often last for 2 hours, and this is accepted as a social norm – it is well understood it takes time to get into the zone.. repeat the basics, introduce a new skill, practice it, and integrate and perfect it over time thru a range of scenarios.

Its also not about the one or two elite players – the whole team improves and transfer skills around and offer peer-support thru the shared activity.  It can help develop individual qualities that are useful for success in other areas of life, namely ‘character’.

With Code

MathCircle is something most parents haven’t heard of .. but teaching young people how to program is an easy sell – it has had a lot of positive marketing over the last year or so, and its a clear pathway to a good salary.

I also think that writing small programs is a great way to introduce and discover math ideas .. its tactile, interactive, hands on, iterative, experimental.  Your also working with the real concepts – a lot of educational apps and games seem to win in terms of engaging and entertaining, but lose in terms of conveying deeper ideas.  When your making a program you are really tinkering under the hood with the engine, not just zooming around the racetrack.  So a MathCircle with an emphasis on making your own programs to investigate math topics, and using tools like Geogebra, might work really well.

Example Topics

A MathCircle which has a code-things-up emphasis, we could call a MathCodeCircle.

Here are some topics that might be covered in such a MathCodeCircle :

  • find prime number factors, use for solving lcm/gcf problems
  • pong game variant – balls bounce around and collide
  • adding waves together – beats, square waves
  • simulate jumpy stock prices, compare with compound interest
  • planets orbiting / solar system simulator
  • circle inversion using GeoGebra

Most of these would be developed in javascript, and run in the browser – using the canvas api to render 2D graphics – its real programming.


Some links if your interested :




So is in open beta, now works reasonably well on iPad and recent desktop browsers.  Android browser support coming sometime soon.

Heres a pic my 9yo created while testing things out on the iPad.. with the obligatory battle scene [ dad vs browser quirks ? ] :


Here’s a couple of sheets on how I like to present long multiplication…

Firstly a concrete rectangle where you can actually count the squares to satisfy yourself its right…


Then move to a more compact form, which still shows the rectangle grid, but not to scale..


This shows that lots of digits should not induce panic.. the same systematic approach works [ which is why computers can do multiplication so well ].

I think having the box grid is a nice way to remember where all the pieces come from [ single digit products ].  Also I think it really helps to use the blank grid spaces, rather than fill in every 0.

The lattice method is slightly more compact, but I think this box approach reminds students of whats really going on.

Thanks for all the emails and encouragement so far as I build this.

Enjoy, and let me know how your using GridMaths.

When I was a kid there was this class where each student prepared their very own ice-cream container full of counting items – marbles, colored buttons, hexagonal Meccano nuts, etc. Somehow the teacher sold us on ‘owning’ and preparing our own stash, and I was very proud of sneaking in two tiny model mini cars and a couple of cool shells I picked up from the beach.

I also remember those lengths of wood in various colors used for counting/measuring things. These “Cuisenaire Rods” are magic, you can pick up a set on eBay from $10 to $15, or improvise with flat Lego units of different lengths and colors if you have those.

It would be unconscionable of me to not include this staple of Western math diet, so I give you a peak at Cuisinaire Rods in GridMaths.  I hope you like the spicy Mexican color scheme :


Simple things, but these can be a very tactile way of developing a feel for fractions and division.   They lead into strange discoveries, like the fact that some numbers can’t be made exactly from repeats of smaller lengths… prime numbers !


A special note to those of you who have taken time to read my experiments, and egg me on with your comments and feedback – your support is so important to me and my son, we heart you !

Just added a color palette to GridMaths, so it now has rectangles, lines, ellipses in few simple colors and weights.  The idea is to help make diagrams clearer, and help with Venn diagrams and other cases where you want to group things together.

Heres a grid sheet comparing fractions : 2/3 and 4/5 … then we show the product and sum using the same visual representation.


If you look carefully, you may notice that I ‘abuse’ the grid in that each grid square is 1/5 high and 1/6 wide – so a 1 unit x 1 unit square is actually 6×5 grid squares.  I think this is a legal abuse, in that we often graph things with different x and y axis scales.

The alternative is to have a resizeable non-square grid.. which I think creates too much complexity for not much gain.  The philosophy of GridMaths is “keep it simple”, so you can do the basics quickly and easily.   I used to think more features = more power = better, but after my son showed me the Minecraft game, and all the cool things people have built with it, I changed my mind about this.  I kept thinking.. why is Minecraft so popular, when you can do all that and more in Blender ?  Blender is a superb free 3D modelling package, which like all powerful modelling packages takes a while to become proficient at.  So, it dawned on me that Minecraft is so brilliant because it brings down the barrier to entry, and makes the 80% of things you need to do to make a world, really easy and quick for everyone.

So I’m adding the most useful things to GridMaths in a way that keeps it really simple.  Its not an algebra system, it most likely wont have handwriting recognition… but it should be a really fast way for math teachers to make Math diagrams and for students to do a wide range of worked Math problems, replacing grid paper for that 80% of tasks and adding some nice features.   Its kind of like an infinite supply of grid paper, that weights nothing and can be erased and replayed, and saved for later use  And.. you can step forward and back thru your edits and change stuff, and students can step thru a worked problem.  And you have counting beans and … :-]

I did a couple of worksheets on Multiplication, to test out the GridMaths tools.

My feeling is that multiplication should be taught with Rectangles.  So once students are familiar with counting in groups, they can play around with something like this interactive demo to get a basic feel for the distributive rule.


[ The above animation is a custom interactive widget created in javascript, using Raphael and HTML5 divs for the draggable UI.  You drag the red dots and the diagram resizes and recalculates in realtime.  Other screenshots in this post are all done using MathsGrid itself ]

The distributive rule says we can divide up A and divide up B any way we like, and the product is just all the pieces of A times all the pieces of B, summed up together.

For humans with 10 fingers, its easy to break A and B up by 1s,10s,100s, which means we need to multiply each digit of A by each digit of B, then sum them up.


So long multiplication kind of falls into place, as a way of keeping the pieces straight :


Then the Lattice method can be seen as just another convenient system for keeping all the pieces organised and lining up the 1s,10s,100s etc :


The first time I saw this Lattice method on Hacker News it looked like Magic.. but after you do one you realize it really is the same thing, just a cool way of keeping track of the pieces.

Just wanted to share a small ‘teaser’ from a project I’m really excited about,

I’ve just finished adding support for the buttons keyboard which lets you make cool counting lessons for younger kids, so here’s a screenshot teaser to enjoy before we go live with the beta site in a few days :


If you haven’t guessed already from the above pic, the idea of is to have an interactive quad grid paper environment in your browser or tablet, where you can work out math problems like long division, multiplication and so on.

My 9yo son has been doing lots of KhanAcademy exercises, working his way up to multiplying decimals..[ which Im pretty chuffed with ].   You really need grid paper to do those as you have lots of adding up in columns.. so anyway we spent ages getting quad ruled grid paper in Bangkok, as this is less common, or at least not easily found in the otherwise excellent stationary shops in the main shopping malls.

Some of these problems take a fair bit of working, so its easy to make typo mistakes .. it seemed to me if you could type in a grid that would really help.  When I thought about how much time Id spent hunting down stationary and that Id spent about $50 in the last year on grid paper… something kind of clicked and I decided to make a prototype and see if it would help.  thus was born GridMaths.

Heres a worked example, of a  ‘long multiplication with decimals’  problem done on a GridMaths sheet :


My 9 year old son has discovered Chess, after watching the movie “Searching for Bobby Fischer”.

We bought a chess set so we could play with some chess puzzles and strategies.. but we were a bit dismayed at the quality of the plastic pieces.  Anyway one thing led to another, and so I spent a couple days hacking up a web based Chess app.

The basics are there :

  • the board
  • piece movements
  • castling
  • loading puzzles in FEN format
  • chess move notation / SAN


As time allows I’ll gradually add the remaining features, such as :

  • en-passant for pawns, pawn promotion
  • check-detection for kings
  • pgn game import/export
  • design & share chess board puzzle

One of the reasons I decided to bite the bullet and do this, was so I can write a few articles on developing a board game in Javascript for young programmers [ my son has been enjoying the programming environment and tutorials on ] and also Id like to be able to design my own puzzle games and share them, as a good way to help young people learn basic moves and strategy.

In other news, Ive been fighting stockfish chess AI.. and it wins every time, except on the lowest setting.   But its been fun to take up the game again, so I can play against my son and answer some of his questions/attacks.

I much enjoyed Kalid Azads Interactive Guide to Fourier Transform article on [ and was much impressed he mentioned my animated sine demo, which he extended upon in wonderful ways. ]

With recent javascript and web 2D/3D, I think we’re at the cusp of a kind of interactive learning renaissance.. because Math concepts can be made really physical, tactile and intuitive :  Show-me-how-it-works + let-me-drive beats verbose-monologue every time.

I wanted to mention another variant of the animated sine demo, where you can add a second circle and slide back and forth to see the first 2 terms of a Fourier series.


It seems like a gentle intro that would get young people thinking..’hmm, what if I add another circle… and how big should that circle be…’.

Obligatory Tolkien quote : “It’s a dangerous business, Frodo, going out of your door. You step into the Road, and if you don’t keep your feet, there is no knowing where you might be swept…”

Been ultra busy lately on two distinct web projects, both of which really need a good architecture so they can scale easily.

One nice way to do this is with services which message each other – if you have a fast, persistent reliable message queue you can have many processes grab jobs off the queue and thus scale out over many cores.  I feel this is a natural way to scale out node.js apps.

I immediately discarded SQS [ Amazon’s scalable queue service ] as it basically polls a web url to check for messages, so it really is not a message queue at all.

Another nice option is Mongo tailable cursors, which is the message queue approach that mongo uses internally to replicate between mongo instances.   This worked sort of ok, but didnt strike me as ultra high performance.  see mongoMQ npm module for a simple api wrapper.

I was very impressed with Redis Publish/Subscribe semantics… which are very fast, but dont have persistence.

In the end I decided to use a combination of Redis pub/sub for notifications, and Redis List operations rpush/lpop to store/retrieve the actual message data.  I wrapped this in a simple node.js api, which Im calling redpill.

I have an initial implementation of a typical web app which has users and info items.  This performs rather well, with web request response times under 50ms whle 1000 items/sec are being inserted as a background task.

The nice thing is that using message queues allows you to scale up by running any number of servers.

See code + comments here :

redpill : persistent messaging using Redis primitives

gorgon : demo web server architecture with messaging between server components

Lots of things to optimise, but basically a good proof of concept and initial working demo code for this approach.

Counting with coffee Beans on grid paper

When I started teaching my son maths we did a lot of counting coffee beans.  We would arrange 12 beans in groups of 3 and groups of 4… The idea was to start building up intuition and then segway into multiplication.

Don’t ask me why coffee beans.. I must be a bad parent!  The beans just happened to be handy, being dark they were easily visible when put on 5mm math grid paper and fit nicely in the squares.  You can use small buttons or raisins even.  Its pretty tactile, so works okay for young kids [ as long as they are old enough not to swallow small things ]

Abstracting to Multiplication

Later we drew the ‘beans’ in the squares and then just drawing a circle in the box instead of placing a bean.  This gradually led to  tracing out the rectangle outlines of the groups and let the squares on paper take the place of actual beans.. so it abstracts really well in a fairly natural and unforced way.

There are some nice ‘tricks’ you can do on squared paper that grow out of this approach –

  • every rectangle is a multiplication product, so you can use it to figure out any times table question
  • you can work out all the times-tables and write them in the top right square before reciting them
  • introduce distributive property (a+b)*c = a*c + b*c by showing the rectangles add up
  • ask.. are there any numbers that cant be made by a rectangle product? [ numbers]
  • show square numbers – 1,4,9,16 …
  • you can show how to add 2n+1 to n squared to get the next square number
  • you can introduce series – 1+3+5+7+ … and show how they sum to make the square numbers
  • make a stepped-triangle 1+2+3+4+5+6 and show how two of these can be put together to make a rectangle, which leads to sum 1 to n = n(n+1)/2

So this approach leads very naturally into some really nice mathematics.  Along the way it reinforces the rote learning of times tables (auditory repetition) with visual intuition.

The web app – introducing Doctor X

I thought there was probably an App or web page to do this kind of thing interactively.  I googled around and found lots of times table grids, math systems but nothing that seemed to take the grid-paper-rectangles-and-counting-beans approach into an interactive medium.

I made notes on what the app might look like, and then spent some time making a quick prototype in Javascript.  I found It needed a way to step through some basic usage notes and examples, so I added a howto box.  Then we came up with a silly name for this thing.

Anyway, here is the current version of the visual grid calculator for kids, which I’m calling :

“The Doctor X Amazing Griddable Multiplication Contraption”

The Doctor X Amazing Griddable Multiplication Contraption

When I get time Ill make some more in-depth tutorials and worksheets on some of the concepts I mentioned above.   Let me know what you’d like to see,