
Why are variables "i" and "j" used for counters? - J3L2404
http://stackoverflow.com/questions/4137785/why-are-variables-i-and-j-used-for-counters
======
mnemonicsloth
i, j, and k come from Hamilton's Quaternions.

Euler picked i for the imaginary unit.

Hamilton needed two more square roots of -1:

    
    
        ii = jj = kk = ijk = -1
    

Hamilton was really influential, and quaternions were the standard way to do
3D analysis before 1900. By then, mathematicians were used to thinking of
(ijk) as a matched set.

Vector calculus replaced quaternionic analysis in the 1890s because it was a
better way to write Maxwell's equations. But people tended to write vector
quantities as like this: (3i-2j+k) instead of: (3,-2,1). So (ijk) became the
standard basis vectors in R^3.

Finally, physicists started using group theory to describe symmetries in
systems of differential equations. So (ijk) started to connote "vectors that
get swapped around by permutation groups," then drifted towards "index-like
things that take on all possible values in some specified set," which is
basically what they mean in a for loop.

~~~
ced
What variable did mathematicians use for summation before physicists started
using group theory to describe symmetries in systems of differential
equations? Isn't that a fairly recent development?

My search for an old paper with a bloody summation symbol found nothing. I
did, however, find this OT gem about Newton, written in 1848:

 _From the thick darkness of the middle ages man's struggling spirit emerged
as in new birth; breaking out of the iron control of that period; growing
strong and confident in the tug and din of succeeding conflict and revolution
it bounded forwards and upwards with resistless vigour to the investigation of
physical and moral truth; ascending height after height; sweeping afar over
the earth, penetrating afar up into the heavens; increasing in endeavour,
enlarging in endowment; every where boldly, earnestly out stretching, till, in
the Author of the Principia, one arose, who, grasping the master key of the
universe and treading its celestial paths, opened up to the human intellect
the stupendous realities of the material world, and in the unrolling of its
harmonies, gave to the human heart a new song to the goodness, wisdom, and
majesty of the all creating, all sustaining, all perfect God._

~~~
robinhouston
If you want an old paper with a summation symbol, you could do worse than the
book where the symbol was first used, by Euler:
[http://www.math.dartmouth.edu/~euler/docs/originals/E212sec1...](http://www.math.dartmouth.edu/~euler/docs/originals/E212sec1ch1.pdf)

(He uses x as the index variable there.)

------
nivertech
In FORTRAN identifiers starting with I,J,K,L,M,N declared as INTEGER by
default.

~~~
aquarin
It is exactly I was taught in university when I learned FORTRAN. But it is
probably influenced from mathematical notation of indexing.

------
axod
I still use 'n' and 'f' every so often.

Reason being, on the ZX Spectrum, it had all the BASIC keywords on each key,
and pressing the key would input the keyword itself (depending on where you
were etc). The 'N' key had 'NEXT', and the 'F' key had 'FOR'.

So to type 'next n' you just pressed 'N' twice. Typing in 'for f' you pressed
'F' twice.

------
indrax
I think this:

[http://en.wikipedia.org/wiki/Summation#Capital-
sigma_notatio...](http://en.wikipedia.org/wiki/Summation#Capital-
sigma_notation)

~~~
ludwigvan
I agree, it probably comes from maths.

As a side note, the first letters are used for constants, the last letters are
for variables; and it was Descartes who popularized this usage:
<http://en.wikipedia.org/wiki/Polynomial#Notation>

I believe they used i for index, and also for the reason that it wasn't used
by other usages as above.

Similarly, the integration sign is actually a long s, standing for sum.
<http://en.wikipedia.org/wiki/Integral_symbol>

------
philwelch
Next question: what other single character variable names do people use? I've
always wanted to work up a master list somewhere.

Speaking to my own usage, I use i and j for indices, x, y, and z for Cartesian
coordinates, n for "number", k and v for "key" and "value" when iterating
through a hash, and r for a variable I'm using for a return value later on.

~~~
nailer
I use a description of what I;m iterating over. If it's a list of people, I
use 'person'. If the keys are session IDs, I use 'sessionid'. I do this
because it's easier for me and I think I'm being nicer to the next person to
come along.

~~~
philwelch
I do that if I'm doing collection.each in Ruby or something, but if I'm in C
or even if I'm doing collection.each_with_index, I use "i" for the index.

------
Groxx
I use 'i' for 'index' / 'iterator', j because it follows i, and k because it
follows j. The first time I wrote a double-deep loop, the only ones I could
recall having seen had used x and y, because they mapped to x,y coordinates.

So why don't I use x and y where it's not coordinate based? Weaker fingers.
'X' is inherently much harder to hit than 'i', 'i' serves as a mnemonic as
it's usually referring to the index being accessed, and 'y' takes a larger
movement than 'j'.

------
Kilimanjaro
Fortran is the culprit

~~~
junkbit
TODAY IS EVERYBODY TALK LIKE FORTRAN DAY

~~~
epo
You didn't begin in column 7 but wisely avoided the continuation character.

------
rwmj
When I first learned programming, the tradition was to use 'f' and 'n'.

On the ZX81, the "FOR" command was accessed by pressing the F key in a special
"command mode", so by typing the F key twice you got "FOR F". Similarly "NEXT
N".

Edit: Yes, duplicate of what axod said here:
<http://news.ycombinator.com/item?id=1971594>

------
chipsy
I rarely use i-j-k, I tend to use n-m-r for some reason(possibly because I've
been programming with iterator syntax so long, that I always think first of
grabbing "thing n" rather than incrementing "counter i.") But on reflection I
think this is probably less legible.

------
skheme
For me i has always been "iterator variable" or something similar, j being the
logical next iterator variable. Then again I haven't done this (code) for very
long.

------
mathgladiator
When I first started studying graduate math, professors would write how my
usage of i as bizarre since I would use it for my index in sums.

sqrt(-1) = 1,2,3,... ????? WTF

------
hackermom
I always used and thought of i as "index", c as "count", and n as "number".

~~~
rix0r
More specifically, I think the N stands for "Natural number".

