+ Reply to Thread
Results 1 to 6 of 6

Thread: Javascript: Prime or not

  1. #1
    reachpradeep's Avatar
    reachpradeep is offline Learning Programmer
    Join Date
    Mar 2007
    Location
    India
    Posts
    41
    Rep Power
    0

    Javascript: Prime or not

    This script is a slightly modified version of "Unique Random Numbers". In this script it becomes easier to implement more than one instances of "Picking Unique Random Numbers".

    This JavaScript picks up a number of unique random elements from an array.
    For example; if you have an array myArray consisting of 10 elements and want to pick 5 unique random elements. Suppose initially myArray[3] is picked randomly, then myArray[3] should not be picked again.

    Code:
    <html>
    <head>
    <title>Unique Random Numbers II</title>
    <!--BEGIN HEAD SECTION CODE-->
    <script language="JavaScript">
    // Unique Random Numbers II
    // -Picks a number of unique random numbers from an array
    // By Premshree Pillai
    // http://www.qiksearch.com, http://javascript.qik.cjb.net
    
    function pickNums(nums, numArr, pickArr, count, doFlag, iterations)
    {
     iterations+=1;
     var currNum = Math.round((numArr.length-1)*Math.random());
     if(count!=0)
     {
      for(var i=0; i<pickArr.length; i++)
      {
       if(numArr[currNum]==pickArr[i])
       {
        doFlag=true;
        break;
       }
      }
     }
     if(!doFlag)
     {
      pickArr[count]=numArr[currNum];
      document.write('<b>' + numArr[currNum] + '</b> <font color="#808080">|</font> ');
      /* Modify above line for a different format output */
      count+=1;
     }
     if(iterations<(numArr.length*3)) // Compare for max iterations you want
     {
      if((count<nums))
      {
       pickNums(nums, numArr, pickArr, count, doFlag, iterations);
      }
     }
     else
     {
      location.reload();
     }
    }
    </script>
    </head>
    <!--END HEAD SECTION CODE-->
    <body bgcolor="#FFFFFF">
    
    <!--BEGIN BODY SECTION CODE-->
    <script language="JavaScript">
    var numArr1 = new Array("0","1","2","3","4","5","6","7","8","9"); // Add elements here
    var pickArr1 = new Array(); // The array that will be formed
    var count1=0;
    var doFlag1=false;
    var iterations1=0;
    
    pickNums(5, numArr1, pickArr1, count1, doFlag1, iterations1); // Call the function, the argument is the number of elements you want to pick.
                 // Here we pick 5 unique random numbers
    </script>
    <!--END BODY SECTION CODE-->
    
    </body>
    </html>

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    RikkoSuperb's Avatar
    RikkoSuperb is offline Newbie
    Join Date
    Aug 2007
    Location
    holywood, county down, northern ireland
    Posts
    20
    Rep Power
    0
    Very useful script reachpradeep, Im looking at the code to try to understand what is happening and why. Is it possible to alter the length of the array and its contents??

  4. #3
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20
    Nice code, but as your title suggests, what does this have to do with 'prime' numbers? ie) 3, 5, 7, 11, 13

  5. #4
    RikkoSuperb's Avatar
    RikkoSuperb is offline Newbie
    Join Date
    Aug 2007
    Location
    holywood, county down, northern ireland
    Posts
    20
    Rep Power
    0
    True enough Sidewinder, when you run the code you never seem to get 5 prime numbers

  6. #5
    RikkoSuperb's Avatar
    RikkoSuperb is offline Newbie
    Join Date
    Aug 2007
    Location
    holywood, county down, northern ireland
    Posts
    20
    Rep Power
    0
    ah, I get you now.
    // I suppose life does become clearer once you read the added comments...!

  7. #6
    Khaotic is offline Learning Programmer
    Join Date
    Apr 2009
    Location
    Brandon, Mississippi, United States
    Posts
    62
    Rep Power
    12

    Re: Javascript: Prime or not

    Very nice +1
    Check out my site: www.khaoticirc.net

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. is number a prime or not?
    By jackson6612 in forum C and C++
    Replies: 16
    Last Post: 04-30-2011, 11:05 PM
  2. check prime
    By NicholasIT in forum C and C++
    Replies: 12
    Last Post: 07-05-2010, 01:39 PM
  3. showing prime;
    By NicholasIT in forum C and C++
    Replies: 6
    Last Post: 06-26-2010, 12:11 AM
  4. prime factors
    By ghostrider_ in forum General Programming
    Replies: 12
    Last Post: 04-22-2010, 08:04 AM
  5. Prime Factorization Help
    By captainhampton in forum C and C++
    Replies: 3
    Last Post: 03-17-2009, 11:41 AM

Tags for this Thread

Bookmarks

Posting Permissions

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