var primeNumbers = {
	list: [],
	max: 0,
	max_uBound: 1000000,
	calculate: function() {
		var i, p = primeNumbers.list, uBound = primeNumbers.max, halfMax, factor, multiplier, product;
		uBound = parseInt(uBound, 10);
		uBound = isNaN(uBound) ? 5000 : Math.abs(uBound);
		uBound = (uBound > primeNumbers.max_uBound) ? primeNumbers.max_uBound : uBound;
		p = primeNumbers.list = new Array(uBound); //maybe a performance boost (dense array)?
		//p[uBound] = true;
		for (i = 0; i <= uBound; i++) {
			p[i] = true;
		}
		p[0] = p[1] = false; // by definition, 0 and 1 are not prime
		halfMax = parseInt(uBound / 2, 10) + 1;
		for (factor = 2; factor < halfMax; factor++) {
			multiplier = factor;
			product = factor * multiplier;
			while (product <= uBound) {
				p[product] = false;
				multiplier++;
				product = factor * multiplier;
			}
		}
	},
	getList: function(param) {
		var skip = false, i, max, p = [], primes = [], tmpNum, tmpStr;
		if (typeof param.delimiter != "string"){
			param.delimiter = ", ";
		}
		param.delimiter = param.delimiter.replace(/\\n/g, "\n").replace(/\\t/g, "\t");
		if (typeof param.max == "number" || typeof param.max == "string") {
			max = parseInt(param.max, 10);
			skip = isNaN(max);
			if (!skip) {
				primeNumbers.max = max;
				primeNumbers.calculate();
			}
		} else if (primeNumbers.max === 0) {
			skip = true;
		}
		if (!skip) {
			p = primeNumbers.list;
			for (i = 0, tmpNum = p.length; i < tmpNum; i++) {
				if (p[i]) {
					primes.push(i);
				}
			}
		}
		primeNumbers.list = p = [];
		tmpStr = "Number of primes between 1 and " + primeNumbers.max + ": ";
		primeNumbers.max = 0;
		if (param.delimiter.length == 0) {
			return primes.slice();
		} else if (param.delimiter == "#") {
			tmpNum = primes.length;
			tmpStr += tmpNum;
			for (i = 0; i < tmpNum; i++) {
				tmpStr += "\np[" + i + "] = " + primes[i];
			}
			return tmpStr;
		} else {
			return primes.join(param.delimiter);
		}
	}
}