DEV Community

Cover image for Why Do We Use `n` in Math and Coding? 🔢
Probal Dhali
Probal Dhali

Posted on

Why Do We Use `n` in Math and Coding? 🔢

Why n? Why not x, m, k, or any other letter?

If you've studied mathematics, algorithms, or programming, you've probably seen something like:

1, 2, 3, ..., n
Enter fullscreen mode Exit fullscreen mode

or:

for i in range(n):
    print(i)
Enter fullscreen mode Exit fullscreen mode

or:

O(n)
Enter fullscreen mode Exit fullscreen mode

or:

a₁, a₂, a₃, ..., aₙ
Enter fullscreen mode Exit fullscreen mode

At some point, you may have wondered:

Why does everyone use n?

Is n an infinity symbol?

Is n a special mathematical number?

Does n actually mean “infinite”?

Why not x?

Why not k?

Why not z?

The interesting answer is:

n does not mean infinity.

It usually represents a general positive integer, a size, a count, or an unspecified number of elements.

And the reason it became so common is a mixture of mathematical convention, historical notation, and practical computer science usage.

Let's unpack the story.


♾️ First: n Does NOT Mean Infinity

This is the most important correction.

When you see:

1, 2, 3, ..., n
Enter fullscreen mode Exit fullscreen mode

n simply means:

some final integer value.

For example, if:

n = 5
Enter fullscreen mode Exit fullscreen mode

then:

1, 2, 3, ..., n
Enter fullscreen mode Exit fullscreen mode

means:

1, 2, 3, 4, 5
Enter fullscreen mode Exit fullscreen mode

If:

n = 1,000,000
Enter fullscreen mode Exit fullscreen mode

then it means:

1, 2, 3, ..., 1,000,000
Enter fullscreen mode Exit fullscreen mode

n itself is finite unless the problem explicitly defines something else.

Infinity is normally represented by:

Enter fullscreen mode Exit fullscreen mode

not:

n
Enter fullscreen mode Exit fullscreen mode

🧠 So What Does n Actually Represent?

In mathematics, n is commonly used for a natural number or a general integer.

For example:

n ∈ ℕ
Enter fullscreen mode Exit fullscreen mode

means:

n belongs to the natural numbers.

Depending on the mathematical convention being used, natural numbers may begin at:

0, 1, 2, 3, ...
Enter fullscreen mode Exit fullscreen mode

or:

1, 2, 3, ...
Enter fullscreen mode Exit fullscreen mode

Different fields and authors use slightly different conventions.

But the important idea is:

n = an unspecified number
Enter fullscreen mode Exit fullscreen mode

🔢 Why n Specifically?

This is where things get interesting.

There isn't a single universally documented moment when someone officially declared:

“From today onward, n shall mean number.”

Mathematical notation evolved over centuries.

Different mathematicians used different letters for different purposes.

Eventually, conventions became widespread because they were:

  • convenient
  • easy to remember
  • repeatedly used in textbooks
  • adopted by later mathematicians
  • standardized through mathematical practice

So n became strongly associated with number, count, or an arbitrary integer.

One commonly cited explanation connects n with words such as:

number

and with historical notation traditions involving integer variables.

But we should be careful here:

It would be inaccurate to claim that the entire mathematical community adopted n because one specific person officially chose it for the word “number.”

Notation evolved rather than being created by one universal naming decision.


📚 A Short History of Mathematical Variables

Mathematics didn't always look like this:

f(n) = n² + 1
Enter fullscreen mode Exit fullscreen mode

Ancient mathematical writing often used words instead of compact symbolic notation.

Modern algebraic notation developed gradually.

Different civilizations contributed important ideas:

Ancient Babylonian mathematics
        ↓
Greek mathematics
        ↓
Indian mathematics
        ↓
Islamic Golden Age
        ↓
European mathematical notation
        ↓
Modern algebra
        ↓
Modern mathematics
Enter fullscreen mode Exit fullscreen mode

The notation we use today is the result of centuries of development.


🇮🇳 India's Contribution Matters Here

If you're interested in the history of mathematics, the development of zero and positional notation in India is particularly important.

Indian mathematicians developed sophisticated numerical systems and mathematical techniques.

The concept of zero became an actual number with arithmetic rules in Indian mathematics.

The work of mathematicians such as Brahmagupta was especially influential in the mathematical treatment of zero.

This eventually contributed to the numerical system that became foundational to modern mathematics.

So when we write:

0, 1, 2, 3, ...
Enter fullscreen mode Exit fullscreen mode

there is a very long history behind that simple sequence.


🌍 From Words to Symbols

As mathematics developed, using complete words became inefficient.

Imagine writing:

The number of elements in the collection is an arbitrary natural number.
Enter fullscreen mode Exit fullscreen mode

every time.

Instead:

n
Enter fullscreen mode Exit fullscreen mode

Much easier.

Mathematical notation works partly like a programming language.

It compresses complicated ideas into symbols.

For example:

Σ
Enter fullscreen mode Exit fullscreen mode

can represent summation.

Enter fullscreen mode Exit fullscreen mode

represents integration.

Enter fullscreen mode Exit fullscreen mode

represents infinity.

And:

n
Enter fullscreen mode Exit fullscreen mode

can represent an arbitrary integer or count.


💻 Then Programming Adopted the Convention

Computer science inherited a huge amount of notation from mathematics.

That's why programmers frequently write:

for i in range(n):
    ...
Enter fullscreen mode Exit fullscreen mode

Here:

n = number of iterations
Enter fullscreen mode Exit fullscreen mode

For example:

n = 5

for i in range(n):
    print(i)
Enter fullscreen mode Exit fullscreen mode

Output:

0
1
2
3
4
Enter fullscreen mode Exit fullscreen mode

Here:

n = 5
Enter fullscreen mode Exit fullscreen mode

It does not mean infinity.

It means the size or limit of the loop.


🧮 Why O(n)?

This is probably where most programmers first encounter n.

Suppose we have:

def print_items(items):
    for item in items:
        print(item)
Enter fullscreen mode Exit fullscreen mode

If the list contains:

10 items
Enter fullscreen mode Exit fullscreen mode

the loop runs approximately:

10 times
Enter fullscreen mode Exit fullscreen mode

If it contains:

1,000 items
Enter fullscreen mode Exit fullscreen mode

the loop runs approximately:

1,000 times
Enter fullscreen mode Exit fullscreen mode

If it contains:

1,000,000 items
Enter fullscreen mode Exit fullscreen mode

the loop runs approximately:

1,000,000 times
Enter fullscreen mode Exit fullscreen mode

So we describe the growth as:

O(n)
Enter fullscreen mode Exit fullscreen mode

Here:

n = input size.

It doesn't mean:

“the program runs forever.”

It means:

“the amount of work grows approximately in proportion to the size of the input.”


🔥 Example: O(n)

def find_number(numbers, target):
    for number in numbers:
        if number == target:
            return True

    return False
Enter fullscreen mode Exit fullscreen mode

If:

n = number of elements
Enter fullscreen mode Exit fullscreen mode

the algorithm may inspect:

1 element
10 elements
100 elements
1,000 elements
...
Enter fullscreen mode Exit fullscreen mode

depending on the input.

Worst-case work grows linearly with n.

Therefore:

Time Complexity = O(n)
Enter fullscreen mode Exit fullscreen mode

🧠 Why Not x?

We absolutely can use x.

For example:

x = 10
Enter fullscreen mode Exit fullscreen mode

is perfectly valid.

But x already has common mathematical roles.

For example:

y = f(x)
Enter fullscreen mode Exit fullscreen mode

Here x often represents an input variable.

Similarly:

(x, y)
Enter fullscreen mode Exit fullscreen mode

often represents coordinates.

So using n for a count helps communicate meaning.

Compare:

for i in range(x)
Enter fullscreen mode Exit fullscreen mode

with:

for i in range(n)
Enter fullscreen mode Exit fullscreen mode

The second convention immediately suggests:

n is probably some number or size.

Variable names communicate intent.


🧩 Why Not k?

Actually, k is also extremely common.

You will often see:

O(n log n)
Enter fullscreen mode Exit fullscreen mode

where:

n = input size
Enter fullscreen mode Exit fullscreen mode

and:

k = some other parameter
Enter fullscreen mode Exit fullscreen mode

For example:

n = total elements
k = number of selected elements
Enter fullscreen mode Exit fullscreen mode

Consider:

Find the largest k elements from n elements.
Enter fullscreen mode Exit fullscreen mode

Then:

n = total input size
k = requested number of results
Enter fullscreen mode Exit fullscreen mode

Using different letters prevents ambiguity.


🔠 Why Not m?

m is also frequently used.

A classic example is a matrix:

A is an m × n matrix
Enter fullscreen mode Exit fullscreen mode

Here:

m = number of rows
n = number of columns
Enter fullscreen mode Exit fullscreen mode

So:

m × n
Enter fullscreen mode Exit fullscreen mode

means:

m rows
n columns
Enter fullscreen mode Exit fullscreen mode

This convention is common enough that many programmers immediately understand it.

But again:

these are conventions, not laws of mathematics.

You could define:

A is an x × y matrix
Enter fullscreen mode Exit fullscreen mode

and mathematically it would still work.


📐 n in Sequences

You'll frequently see:

a₁, a₂, a₃, ..., aₙ
Enter fullscreen mode Exit fullscreen mode

This means:

first element
second element
third element
...
nth element
Enter fullscreen mode Exit fullscreen mode

For example:

aₙ = 2n
Enter fullscreen mode Exit fullscreen mode

Then:

a₁ = 2
a₂ = 4
a₃ = 6
a₄ = 8
Enter fullscreen mode Exit fullscreen mode

Here n is essentially an index.


n in Summation

Consider:

Σᵢ₌₁ⁿ i
Enter fullscreen mode Exit fullscreen mode

This means:

1 + 2 + 3 + ... + n
Enter fullscreen mode Exit fullscreen mode

For:

n = 5
Enter fullscreen mode Exit fullscreen mode

we get:

1 + 2 + 3 + 4 + 5 = 15
Enter fullscreen mode Exit fullscreen mode

Again:

n = upper limit.

Not infinity.


♾️ Then Where Does Infinity Enter?

Now consider:

1, 2, 3, 4, ..., n, ...
Enter fullscreen mode Exit fullscreen mode

or:

n → ∞
Enter fullscreen mode Exit fullscreen mode

Here n is still a finite integer at each stage.

The notation:

n → ∞
Enter fullscreen mode Exit fullscreen mode

means that we're considering what happens as n grows without bound.

This distinction is extremely important.

For example:

lim(n→∞) 1/n = 0
Enter fullscreen mode Exit fullscreen mode

We are not saying:

n = infinity
Enter fullscreen mode Exit fullscreen mode

We're saying:

Consider larger and larger values of n.

For example:

n = 10
n = 100
n = 1,000
n = 1,000,000
...
Enter fullscreen mode Exit fullscreen mode

As n grows without bound:

1/n → 0
Enter fullscreen mode Exit fullscreen mode

🤯 A Common Misunderstanding

People sometimes say:

“n means infinity.”

That's incorrect.

A better statement is:

n often represents an arbitrary integer or size, and in limits or asymptotic analysis it may be allowed to grow without bound.

That's much more precise.


💡 Why Does Computer Science Love n?

Because computer science constantly deals with size.

For example:

Number of users      → n
Number of records    → n
Number of elements   → n
Input length         → n
Number of vertices   → n
Number of operations → n
Enter fullscreen mode Exit fullscreen mode

Once n means:

“size of the input”

we can compare algorithms independently of a specific dataset.


⚡ Example: O(1) vs O(n)

Suppose:

numbers = [10, 20, 30, 40, 50]
Enter fullscreen mode Exit fullscreen mode

Accessing:

numbers[2]
Enter fullscreen mode Exit fullscreen mode

doesn't require scanning all elements.

So we often describe it as:

O(1)
Enter fullscreen mode Exit fullscreen mode

Now:

for x in numbers:
    print(x)
Enter fullscreen mode Exit fullscreen mode

requires processing each element.

Therefore:

O(n)
Enter fullscreen mode Exit fullscreen mode

The exact value of n isn't the point.

The growth relationship is the point.


📈 Why Big-O Uses n

Big-O notation describes asymptotic growth.

For example:

O(1)
O(log n)
O(n)
O(n log n)
O(n²)
O(2ⁿ)
Enter fullscreen mode Exit fullscreen mode

Here n normally represents input size.

For example:

n = 10
n = 100
n = 1,000
n = 1,000,000
Enter fullscreen mode Exit fullscreen mode

We care about how the algorithm's resource requirements grow as n grows.


🧠 But n Is NOT Mandatory

This is another important point.

You could write:

O(m)
Enter fullscreen mode Exit fullscreen mode

instead of:

O(n)
Enter fullscreen mode Exit fullscreen mode

and it would be mathematically valid if m represents the input size.

You could even write:

O(p)
Enter fullscreen mode Exit fullscreen mode

if you define:

p = input size
Enter fullscreen mode Exit fullscreen mode

The notation works because you define the variable.

The convention simply makes communication easier.


👨‍💻 Programming Has the Same Principle

Consider:

function processUsers(users) {
    for (let i = 0; i < users.length; i++) {
        console.log(users[i]);
    }
}
Enter fullscreen mode Exit fullscreen mode

We might analyze it as:

n = users.length
Enter fullscreen mode Exit fullscreen mode

Then:

Time Complexity = O(n)
Enter fullscreen mode Exit fullscreen mode

We could instead define:

u = number of users
Enter fullscreen mode Exit fullscreen mode

and say:

O(u)
Enter fullscreen mode Exit fullscreen mode

Nothing mathematically breaks.

But n is conventional and immediately recognizable.


🔤 What About i, j, and k?

There's another beautiful convention in mathematics and programming.

You'll often see:

for i in range(n):
    for j in range(n):
        ...
Enter fullscreen mode Exit fullscreen mode

Here:

n = size
i = current position
j = another position
Enter fullscreen mode Exit fullscreen mode

In more complex algorithms:

i
j
k
Enter fullscreen mode Exit fullscreen mode

often represent nested indices.

For example:

for i in range(n):
    for j in range(n):
        for k in range(n):
            ...
Enter fullscreen mode Exit fullscreen mode

This isn't a strict rule.

It's a convention that reduces cognitive load.


🏛️ Mathematical Notation Is a Language

Think about programming languages.

We agree that:

if
Enter fullscreen mode Exit fullscreen mode

means something.

We agree that:

for
Enter fullscreen mode Exit fullscreen mode

means something.

Mathematics works similarly.

We have conventions such as:

n → number / integer / size
i → index
x → variable
f(x) → function
Σ → summation
∞ → infinity
∈ → belongs to
∀ → for all
∃ → there exists
Enter fullscreen mode Exit fullscreen mode

These conventions make mathematical communication faster.


🧠 The Real Reason n Survived

There probably isn't one magical reason.

It's more useful to think of it as notation becoming conventional through repeated use.

A notation becomes powerful when:

Easy to use
+
Easy to remember
+
Widely published
+
Widely taught
+
Widely understood
=
Convention
Enter fullscreen mode Exit fullscreen mode

Once enough mathematicians, engineers, scientists, and programmers use a notation, changing it becomes expensive.

Imagine every textbook suddenly replaced:

n = input size
Enter fullscreen mode Exit fullscreen mode

with:

q = input size
Enter fullscreen mode Exit fullscreen mode

Nothing mathematically changes.

But everyone has to relearn the convention.

That's why established notation tends to persist.


🌍 Conventions Reduce Communication Cost

Imagine reading this algorithm:

O(n log n)
Enter fullscreen mode Exit fullscreen mode

Most programmers immediately understand:

n ≈ input size
Enter fullscreen mode Exit fullscreen mode

Now imagine every author randomly chose:

O(a)
O(x)
O(q)
O(size)
O(elements)
O(data)
Enter fullscreen mode Exit fullscreen mode

All of these could work.

But communication becomes less predictable.

Conventions are useful because they create a shared mental vocabulary.


🔥 The Same Idea Appears Everywhere

You'll find conventional variables throughout computer science.

Graph Theory

V = vertices
E = edges
Enter fullscreen mode Exit fullscreen mode

So:

G = (V, E)
Enter fullscreen mode Exit fullscreen mode

Machine Learning

n = number of samples
d = number of features
Enter fullscreen mode Exit fullscreen mode

Statistics

n = sample size
μ = population mean
σ = standard deviation
Enter fullscreen mode Exit fullscreen mode

Linear Algebra

m × n matrix
Enter fullscreen mode Exit fullscreen mode

Algorithms

n = input size
Enter fullscreen mode Exit fullscreen mode

Programming

i, j, k = indices
Enter fullscreen mode Exit fullscreen mode

Again, these are conventions—not immutable laws.


🧪 A Small Experiment

Let's see how n behaves in code.

def count_to_n(n):
    for i in range(1, n + 1):
        print(i)
Enter fullscreen mode Exit fullscreen mode

Call:

count_to_n(5)
Enter fullscreen mode Exit fullscreen mode

Output:

1
2
3
4
5
Enter fullscreen mode Exit fullscreen mode

Call:

count_to_n(100)
Enter fullscreen mode Exit fullscreen mode

Now it prints:

1
2
3
...
100
Enter fullscreen mode Exit fullscreen mode

The program doesn't care that we chose the letter n.

We could write:

def count_to_n(number):
    for i in range(1, number + 1):
        print(i)
Enter fullscreen mode Exit fullscreen mode

and the program behaves identically.

The difference is human communication.


💭 So Why Not Just Write number?

Excellent question.

We actually can.

In production code, descriptive names are often better:

def process_users(user_count):
    ...
Enter fullscreen mode Exit fullscreen mode

is usually clearer than:

def process_users(n):
    ...
Enter fullscreen mode Exit fullscreen mode

But mathematics needs extremely compact notation.

Compare:

For every natural number n greater than 1...
Enter fullscreen mode Exit fullscreen mode

with a long descriptive variable name.

Mathematical expressions would quickly become unreadable.

So mathematics favors concise notation.

Programming can often afford descriptive names.


🧑‍💻 This Gives Developers an Important Lesson

There are two different goals:

Mathematics

Optimize for:

compact symbolic communication

Production Programming

Often optimize for:

readability and maintainability

So this:

for i in range(n):
Enter fullscreen mode Exit fullscreen mode

is perfectly normal.

But this:

for x in range(a):
Enter fullscreen mode Exit fullscreen mode

may be less descriptive if a has no obvious meaning.

And in production code:

for user_index in range(user_count):
Enter fullscreen mode Exit fullscreen mode

could be clearer.


🚀 Final Answer: Why n?

So, why do mathematics and programming use n so much?

Because:

  1. n is conventionally associated with a number or integer.
  2. It is commonly used to represent a count or size.
  3. Computer science inherited this notation from mathematics.
  4. Big-O analysis commonly uses n for input size.
  5. Mathematical notation values compact symbols.
  6. Repeated historical usage turned n into a widely recognized convention.
  7. Using familiar conventions reduces communication overhead.
  8. n is not inherently special—it's replaceable if properly defined.
  9. n does not mean infinity.
  10. Infinity is represented separately, usually by .

🧠 The One Sentence to Remember

If someone asks:

“Why is n used everywhere?”

you can answer:

Because n became a widely accepted mathematical convention for representing an arbitrary number, integer, count, or input size—and computer science inherited that convention.

And if they ask:

“Does n mean infinity?”

Answer:

No. n is usually finite; it can grow without bound in contexts such as limits and asymptotic analysis, but that is different from n being infinity.


🔥 Final Thought

One of the fascinating things about mathematics is that many symbols we treat as “obvious” today were not inevitable.

Someone could have chosen another letter.

Someone could have written something completely different.

But mathematics is also a human communication system.

Over time, useful notation survives.

And n survived because millions of mathematicians, scientists, engineers, and programmers learned to look at:

n
Enter fullscreen mode Exit fullscreen mode

and immediately think:

“Some number. Some size. Some count.”

That's the real power of notation.

Not the letter itself.

The shared meaning behind it. 🧠


📚 Further Reading

For deeper exploration, look into:

  • History of mathematical notation
  • History of algebraic symbolism
  • Big-O notation and asymptotic analysis
  • History of zero and Indian mathematics
  • Mathematical conventions in computer science
  • Donald Knuth's work on mathematical and algorithmic notation

Mathematics #Programming #ComputerScience #Algorithms #BigO #SoftwareEngineering #Coding #HistoryOfMathematics #LearnToCode #Developer #DevCommunity #ProgrammingBasics

Top comments (0)