Can someone please give me guidance on getting the primenumbers here? This is homework so I don\'t want the answer but some pointers would be greatly appreciated. It\'s real
Here's a simple "sieve" for prime numbers, which can be easily understood, and although it is a naive approach (as opposed to sophisticated efficient prime number tests such as the AKS test), it is pretty fast (10000 numbers tested in < 1 sec). It stores the found prime numbers in the array prim[]
and tests by using the modulo function (%
):
The loop tests against already found prime numbers and exits if it is no prime number, i.e. if the modulo result is 0 (regard the expression i % prim[j])===0
). Otherwise, it adds it to the list of found prime numbers.
Note that because the only even prime number is 2, the loop step is 2 rather then 1, because from 3 onwards there can't be any further even prime numbers.
var MaxNum = 10000;
var prim;
function Main() {
MaxNum = GetMaxNum();
prim = CalculatePrimes(MaxNum);
CheckSome();
}
function CalculatePrimes(pMaxNum) {
Console.WriteLine("Calculating until " + pMaxNum + "...");
var _prim = [2];
if (pMaxNum > 2) {
for (var i = 3; i < pMaxNum; i += 2) {
var is_prim = true;
if (_prim.length > 0) {
for (var j = 0; j < _prim.length; j++) {
if ((i % _prim[j]) === 0) {
is_prim = false;
break;
}
}
}
if (is_prim) {
_prim.push(i);
}
}
}
Console.WriteLine("Prime numbers:");
for (var i = 0; i < _prim.length; i++) {
Console.Write(_prim[i] + " ");
}
Console.WriteLine();
Console.WriteLine("Found " + _prim.length + " prime numbers.");
Console.WriteLine();
return _prim;
}
// test some individual pre-calculated numbers
function CheckSome() {
var num1 = prim[prim.length - 1];
var num2 = num1 - 1;
Console.WriteLine("Test: " + num1.toString() + ". Is it a prime number? " + Is_prime(num1));
Console.WriteLine("Test: " + num2.toString() + ". Is it a prime number? " + Is_prime(num2));
}
function Is_prime(n) {
if (n > MaxNum) throw "ERROR: n must be <" + MaxNum + "!";
if (prim.indexOf(n) === -1)
return false;
else
return true;
};
// ------------ HELPERS to display on screen ------------
var Console = {
Section: 1,
SectionId: "#section1",
NewSection: function() {
var $currentSection = $(this.SectionId);
this.Section++;
this.SectionId = "#section" + this.Section.toString();
$currentSection.before('');
},
Write: function(str) {
$(this.SectionId).append(str);
},
WriteLine: function(str) {
if (str !== undefined && str !== null && str !== "") this.Write(str);
this.Write("
");
}
};
var GetMaxNum = function() {
var result = $("#MaxNumSelect option:selected").val();
return result;
}
$(document).ready(function() {
$("#MaxNumSelect").change(function() {
MaxNum = GetMaxNum();
Console.NewSection();
Main();
Console.WriteLine("---------------------------------");
});
Main();
});
Select max number:
In the above example, we have tested the first 10000 natural numbers. To decide if a given number is a prime number, you simply check if it is contained in the array prim
:
function Is_prime(n) {
if (n>MaxNum) throw "ERROR: n must be <"+CalcToNum+"!";
if (prim.indexOf(n)===-1)
return false;
else
return true;
};
Of course, in order for this to work the prime numbers need to be pre-calculated.
Example: alert(Is_prime(25));
- returns false, because 25 is no prime number.
Note: The number range must be checked, because the function Is_prime
can decide only for numbers which are previously tested by the sieve above. If the array is too small for the number to check (i.e. if not enough prime numbers are pre-calculated), an error is thrown.