Question #54761

A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be divided by 1 and 5. The number 6 however, is not prime because it can be divided evenly by 1, 2, 3, and 6.
Write a Boolean function named isPrime which takes an integer as an argument and returns true if the argument is a prime number of false otherwise. Generate 100 random numbers and display the results of checking each one for primality.
Save this in a script called primes.js.
1

Expert's answer

2015-09-19T00:00:43-0400

Answer on Question #54761 - Programming - AJAX | JavaScript | HTML | PHP

Contents of primes.js file:


var min = 1,
max = 100;
// returns is number prime or not
function isPrime(n) {
    for(var i = 2; i < n; i++) {
        if(n % i === 0) {
            return false;
        }
    }
    return n > 1;
}
for(var i=0;i<100;i++) {
    var number = parseInt(Math.floor(Math.random() * max) + min); // get random number
    var result = 'Number: ' + number.toString() + ', ' + ((isPrime(number))? ' ' : 'not ' ) + 'prime';
    console.log(result); // show result to console
}


http://www.AssignmentExpert.com/

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS