Why are variables "i" and "j" used for counters?
stackoverflow.com
stackoverflow.com
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.
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.
(He uses x as the index variable there.)
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.
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
Remember that modern electronic computing was invented by mathematicians and physicists.
---
[1] HN isn't about TeX, so read that as "M sub-i sub-j".
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.
For example, an F# pairing heap:
type 't heap = Empty | Heap of 't * 't heap list
let findmin (Heap(x,_)) = x
let merge h1 h2 =
match (h1,h2) with
| Empty,h | h,Empty -> h
| Heap(x,h1s),Heap(y,h2s) -> if x < y then Heap(x,h2::h1s) else Heap(y,h1::h2s)
let deletemin (Heap(_,hs)) = List.fold merge Empty hs
let insert h x = merge h (Heap(x,[]))
First line: t for type. Second line: x for something of generic type t. Third line: h1 and h2 for heap. Fifth line: h1s and h2s for lists of heaps corresponding to h1 and h2. This saves time coming up with names, and it ends up being more readable if you're used to it. Of course these short names will only be used for variables that are used internally in a function, not for function names and such.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'.
FORTRAN taught me that languages should understand some spacing.
Oh those long gone years...
Of the explanations on the SO page, the fact that i is the first letter of index seems like the most likely. j is popular because it follows i.
Although I do find the answer to the latter question interesting as well, just not as interesting as the answer to the first.
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
sqrt(-1) = 1,2,3,... ????? WTF