8

I've noticed that Sigma notation is a lot like a For loop in programming. What, if anything, can be used to notate a While loop mathematically?

In other words, how to you notate the sum of everything (like Sigma) while a variable is equal or unequal to a certain number or expression?

  • Check this: http://stackoverflow.com/questions/392375/turn-while-loop-into-math-equation – Hushus46 May 15 '15 at 22:05
  • You can try to describe the process like a distribution of probability or an integral. You can express conditions under a lot of functions like Kronecker delta. If you want Kronecker delta under some analytic you can try use the condition as the limit of a function. – Masacroso May 15 '15 at 22:15

2 Answers2

5

It is not uncommon to have notation like $$\sum_{i\in S} f(i)$$ where $S$ is some set of indices for $i$. Also common is $$\sum_{i\ge 100,\ i\notin S} g(i)$$ where two conditions are combined, and only indices satisfying both are used.

vadim123
  • 82,796
  • Thank you for this answer, it appears to be what I am looking for. However, I am still slightly unlearned to the complete functionality of Sigma notation. Could you give me an example of these two situations so I can see them in action? – Nick Suwyn May 15 '15 at 22:13
  • You could define $S={2k+1:k\in\mathbb{N}}$, the set of all odd indices. You could define $S={p\in\mathbb{N}:p\text{ is prime }}$. Any way you like! – vadim123 May 15 '15 at 22:45
  • @NickSuwyn Lets say you want to count all primes less than $n$. If $\mathbb P$ is the set of all primes then you can do the following: $$\sum_{p<n,,p\in\mathbb P}1$$Simmilarily if you want the sum of primes less than $n$ you can do this$$\sum_{p<n,,p\in\mathbb P}p$$ – Alice Ryhl May 16 '15 at 07:40
3

A while loop cant be directly written in mathematical notation, however every while loop can be written as a recursive function.

If we have the while loop below, which changes all the variables each iteration using a function for each variable.

a = initial value for a
b = initial value for b
...
while (expression) {
  a = na(a,b,...)
  b = nb(a,b,...)
  ...
}

This can be written as a recursive function as below:

function(a,b,...) {
  if (expression) {
    return a
  } else {
    a_new = na(a,b,...)
    b_new = nb(a,b,...)
    ...
    return function(a_new,b_new,...)
  }
}

A recursive function can easily be written in mathematical notation

$$ f(a,b,\dots)= \begin{cases} a\quad&\text{if expression}\\ f(n_a(a,b,\dots),n_b(a,b,\dots),\dots)&\text{if not expression} \end{cases} $$

Very often it is possible to write a recursive formula like this one as a sum or product, in which case it is often better to do so.


Examble based on your other question.

In your other question, you could explain the sum (up to some constant N) as the following while loop:

sum = 0
summand = x
iterations_left = N
while (iterations_left > 0) {
  summand = summand / 2
  sum = sum + summand
  iterations_left = iterations_left - 1
}

This can be written as a recursive function the following way:

sum_to(x, iterations) {
  if (iterations == 1) {
    return x/2
  } else {
    return x/2 + sum_to(x/2, iterations - 1)
  }
}

And can such be written as a recursive function

$$ f(x,i)= \begin{cases} x/2\quad&\text{if }i=1\\ x/2+f(x/2,i-1)&\text{if }i\ne1 \end{cases} $$

Alice Ryhl
  • 7,853