...

View Full Version : Resolved IsPrime? using do/while



seulki
12-14-2010, 10:09 AM
I'm kind of stumped at how to fix this, but here is what i have so far ^^
// <==things displayed after this is what i have to do.
I was wondering if there was a better way to reword it :x or if i'm missing something >.<;
Thank you.



<html>
<!-- IsPrime.html -->
<!-- This page determines if a given positive integers is a Prime number. -->
<!-- =================================================== -->

<head>
<title> IsPrime </title>
<script type="text/javascript"
src="http://dave-reed.com/book/random.js">
</script>
<script type="text/javascript">
function IsPrime()
// Assumes: IntBox contains a positive integer
// Results: Uses the IntBox value to determine if it is a Prime number
// (A Prime number IS ONLY EVENLY DIVISIBLE(remainder of 0) BY 1 and ITSELF)
// and displaying the result in the 'PrimeBox' as either TRUE or FALSE
// Example: IntBox = 7, then PrimeBox = TRUE
// Example: IntBox = 8, then PrimeBox = FALSE
// Algorithm:
// 'dividend' starts with a value of 2 and 'isprime' starts with a value of 'true'
// DO: get the remainder of dividing the number by the current value of the dividend
// IF the remainder is even, then set isprime to false
// ELSE increment the dividend by one
// WHILE (the dividend is less than the number AND isprime is true)
{
var dividend, isprime, number;
dividend = 2;
isprime = true;
number = Math.sqrt(document.getElementById("IntBox").value);
number = parseFloat(number);
do {
remainder = number/dividend;
if ((remainder % dividend) == 0) {
isprime = false;
document.getElementById("PrimeBox").value = "False";
}
else {
dividend = dividend + 1;
}
}
while (dividend < number && isprime = true) {
document.getElementById("PrimeBox").value = "True";
}
}
</script>
</head>

<body>
<div style="text-align:center">
<h2>Prime number determination</h2>
<p>
Enter a positive integer =
<input type="text" id="IntBox" size="6" value="1" />
</p>
<p>
<input type="button" value="Click here to determine if number Is Prime" onclick="IsPrime();" />
</p>
<p>
Results =
<input type="text" id="PrimeBox" size="15" value="MAYBE..." />
</p>

</div>
</body>
</html>

Philip M
12-14-2010, 12:10 PM
while (dividend < number && isprime = true)
number = parseFloat(number); // should be Math.ceil(number) to round up
The instructions say IF the remainder is even, then set isprime to false
See what is wrong here?
remainder = number/dividend;
if ((remainder % dividend) == 0) {


Try this version:-


<html>
<head>

<script type = "text/javascript">

function calculate(form) {

var num = parseInt(form.number.value);
if (isNaN(num) || num < 0) {
form.result.value = ("That is not a valid number! Try again!");
document.myform.number.value = "";
setTimeout("document.myform.number.focus()", 25);
}
if (num == 1 || num == 2 || num == 3) {
form.result.value = (num + " is prime!");
return false;
}
var snum = Math.ceil(Math.sqrt(num));
for (var i=2; i<=snum; i++) {
if (num%i == 0) {
var prime = false;
form.result.value = (num + " is not prime. It is divisible by " + i + ".");
break;
}
if (num%i != 0) {var prime = true};
}
if (prime) form.result.value=(num + " is prime!");
}
</script>
</head>
<body>

<center>
<form name = "myform">
<h2>Prime Number Identifier</h2>
Please enter a number:<br>
<input type=text name=number size=7>
<input type=button value="Calculate" onClick="calculate(this.form)">
<br>
<input type="text" name="result" size= "40" value="">
</form>
</center>

</body>
</html>

Robert Pires has a nice habit of passing the ball to his own team. - Football Commentator SkyTV

seulki
12-14-2010, 06:18 PM
Thank you Philip ^^ from the things you pointed out :].. i was able to figure it out while keeping to the do/while :D ^^



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum