Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    IsPrime? using do/while

    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.

    Code:
    <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>
    Last edited by seulki; 12-14-2010 at 06:18 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,243
    Thanks
    203
    Thanked 2,555 Times in 2,533 Posts
    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:-

    Code:
    <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
    Last edited by Philip M; 12-14-2010 at 12:41 PM.

  • Users who have thanked Philip M for this post:

    seulki (12-14-2010)

  • #3
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you Philip ^^ from the things you pointed out :].. i was able to figure it out while keeping to the do/while ^^


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •