Considering that Juleian has described recursion, I'll describe loops.

Let's look at a Project Euler practice problem:

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

Okay, so we could use some recursion:

(Code is in JavaScript because JavaScript is basically universally easy to read for people who have a background in almost any standard programming language. That doesn't make it good, though ).

function findSmallestNumber(currentNumber, currentFunctionCall, amountOfFactors) {
if(currentFunctionCall > amountOfFactors) {
return currentNumber;
} else if (currentNumber % currentFunctionCall === 0) {
++currentFunctionCall;
return findSmallestNumber(currentNumber, currentFunctionCall, amountOfFactors);
} else if (currentNumber % currentFunctionCall !== 0) {
++currentNumber;
currentFunctionCall = 1;
return findSmallestNumber(currentNumber, currentFunctionCall, amountOfFactors);
}
}
var amountOfFactors;
do {
amountOfFactors = prompt("How many factors are there (0 - ?)");
} while (amountOfFactors.isNaN);
console.log(findSmallestNumber(1, 1, amountOfFactors));

(What's awesome about learning recursion is that this makes sense )!

So, I tested this with 1-10 and it worked. However, try it with 1-20 (This link will bring you to a page with my code. Click the "run" button to see it in action!) You should get the error "Too much recursion". The compiler can't handle 25,000,000 function calls (I solved the code euler problem, obviously, so yeah, there's that many). So, we have to translate it into loops:

function findSmallestNumber(amountOfFactors) {
var currentNumber = 1;
var currentFactor = 1;
while(currentFactor <= amountOfFactors) {
if(currentNumber % currentFactor === 0) {
++currentFactor;
continue;
} else if(currentNumber % currentFactor !== 0) {
++currentNumber;
currentFactor = 1;
continue;
}
}
return currentNumber;
}
var amountOfFactors = 0;
do {
amountOfFactors = prompt("How many factors (1 - ?)", "");
} while (amountOfFactors.isNaN);
console.log(findSmallestNumber(amountOfFactors));

This is the same function, using loops. We see that it works the same, however it will work for an (almost) infinite amount of number.

(One more thing, don't try the script using a while() loop with the value "20". It will cause your browser to freeze because Javascript really isn't meant for this Because it's an interpreted language and was created for changing HTML / CSS with minor logic ).

Cheers !

**Edited by superman3275, 09 April 2013 - 06:56 PM.**