/    Sign up×
Community /Pin to ProfileBookmark

I need help with prime numbers…

Alright I have an assignment for class to create a simple prime number finding script. I have to create a textbox (done), and a button (done). The script is suppose to tell if the number entered in the box is a prime number (any number between 1 and 999) and alert the user whether it is in the form of a an alert window. I know how to pull out the number from the text box, but I’m supposed to use a ‘for’ loop to some how have the number entered in the text box divided by all numbers up to itself. I cannot figure this out. This is what i have:

for (var i=2; i<1000; i++){
document.prime.primeNumnber.value % i;
document.prime.primeNumber.value % i++;
break;
document.prime.primeNumber.value % document.prime.primeNumber.value;
}

i know that is completely wrong. can someone help me out?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@ZarelSep 18.2005 — Alright I have an assignment for class to create a simple prime number finding script. I have to create a textbox (done), and a button (done). The script is suppose to tell if the number entered in the box is a prime number (any number between 1 and 999) and alert the user whether it is in the form of a an alert window. I know how to pull out the number from the text box, but I'm supposed to use a 'for' loop to some how have the number entered in the text box divided by all numbers up to itself. I cannot figure this out. This is what i have:

for (var i=2; i<1000; i++){

document.prime.primeNumnber.value % i;

document.prime.primeNumber.value % i++;

break;

document.prime.primeNumber.value % document.prime.primeNumber.value;

}

i know that is completely wrong. can someone help me out?[/QUOTE]


Well, you should have something like this:

[CODE]for (var i=2; i<document.prime.primeNumnber.value; i++){
if (document.prime.primeNumnber.value % i == 0)
{
document.write("Number is prime");
break;
}
}[/CODE]
Copy linkTweet thisAlerts:
@silvurauthorSep 18.2005 — i still cannot get this to work. here is my code:

[CODE]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS
function checkNumber() {
var i;
for (var i=2; i<document.prime.primeNumber.value; i++){
if (document.prime.primeNumber.value % i == 0) {
window.alert("Number is prime");
break; }
else {
window.alert("Number is not prime");
break; }

}
}
// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script>
</head>
<body>
<form name="prime" action="">
<input type="text" name="primeNumber" /><br />
<input type="button" value="Check" onclick=checkNumber(); />
</form>
</body>
</html>
[/CODE]


it's correct from like an input of 1-8, but it's saying stuff like 9, 15, and 21 are also prime numbers.
Copy linkTweet thisAlerts:
@jalarieSep 19.2005 — <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Prime</title>

<script type="text/javascript">

<!-- HIDE FROM INCOMPATIBLE BROWSERS

function checkNumber() {

Val=document.prime.primeNumber.value;

for (var i=2; i<Val; i++){

if (Val % i == 0) {

window.alert("Number is not prime; it is evenly divisible by "+i+".");

return;

}

}

window.alert("Number is prime.");

return;

}

// STOP HIDING FROM INCOMPATIBLE BROWSERS -->

</script>

</head>

<body>

<form name="prime" action="">

<input type="text" name="primeNumber" /><br />

<input type="button" value="Check" onclick=checkNumber(); />

</form>

</body>

</html>
×

Success!

Help @silvur spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...