JavaScript - The Sieve Of Eratosthenes Situation
Hello all. I would like a helping hand on this assignment. Its on the above function. here is the assignment. It is on The Sieve of Eratosthenes
The Sieve of Eratosthenes An integer is a prime number if it is evenly divisible by only itself and 1. There are several ways to find all prime numbers within a given range of integers, and the Sieve of Eratosthenes is an algorithm for to do just that. It operates as follows: Create an array with all elements initialized to 1 (true). Array elements with prime subscripts will remain as 1. All other array elements will eventually be set to zero. Starting with array subscript 2 (subscript 1 must be prime), every time an array element is found whose value is 1, loop through the remainder of the array and set to zero every element whose subscript is a multiple of the subscript for the element with value 1. For array subscript 2, all elements beyond 2 in the array that are multiples of 2 will be set to zero (subscripts 4, 6, 8, 10, etc.); for array subscript 3, all elements beyond 3 in the array that are multiples of 3 will be set to zero (subscripts 6, 9, 12, 15, etc.); and so on. When this process is complete, the array elements that are still set to 1 indicate that the subscript is a prime number. These subscripts can then be printed. Write a script that uses an array of 1000 elements to determine and print the prime numbers between 1 and 999. Ignore element 0 of the array. ______________ Basically I am lost to what I am supposed to do on this assignment. I would like some clues on how to start this and what exactly my output should be? I know i posted this already. I figure I repost properly and get some more views on this. one. This is all in javascript. Thanks Similar TutorialsI don't know what it is, but I canNOT grasp this one. "Create an array with all elements initialized to 1 (true)." I did this successfully and have 100 1's across my document. var sieve = new Array(100); for (var i=0; i < sieve.length; i++) { sieve[i] = 1 } document.write("<td>"+sieve+"</td>"); Then it says, "Starting with array subscript 2 (subscript 1 must be prime), every time an array element is found whose value is 1, loop through the remainder of the array and set to zero every element whose subscript is a multuple of the subscript for the element with value 1." This to me sounds like a very confusing run-on sentence. I'm supposed to set all elements beyond 2,3,5, etc. in the array that are multiples of those numbers to 0. In other words, all numbers that are prime should be 1, and all others should be 0. I started with multipes of 2, and this is what I came up with to write under the previous code: for (var j=i; j < sieve.length; j+=i) { if (j >= 2 && j % 2 == 0) { sieve[j] = 0 } document.write("<td>"+sieve+"</td>"); This shows an error. Is my code completely stupid? I'm trying to stay within the teachings of the book, but I can't get it to work. It then asks to ' print ' the numbers. Does that mean all the prime numbers I've initialized to 1 should now show their real value? Can I just go back and erase sieve[i]=1? How do I print the numbers? Any help would be wonderful and very very very much appreciated. Thanks a lot! Hi All, i have this Regex which is below and im trying to validate a field when a button is clicked i may have this mest up but what the regex does it checks for 1 decimal place and returns true or false i put some comments in with the code Code: function IsNewBid() { var NewBid = document.getElementById('MainContent_txtNewBid').value; var result; result = ^[0-9]{1,10}[.]{1}[0-9]{1,2}$|^[0-9]{1,10}$(NewBid .Value) if (result 'Equals False So theres and error') Do something else{ carry on } } Any help will be highly appreciated.. Thank you for your time. i had to create a function to calculate percentage and round to integer need 2 parameters of item and sum function will return value of item variable divide the sum by 100 and round to nearest integer using Math.round this is my code that i came up with Code: function calcPercent(item,sum){ // script element to calculate the percentage which is rounded to the nearest integer var items = Math.round( (item/sum)*100); // script element to get the percentage return items; } calcPercent(1, 3); Does this make sense? thanks Is it possible to built a conditional for a hover situation ? for example I wanto to do something only if the mouse is still over a DIV. Code: Something like this: if (oObject.hover=true) { do something; } How can I tell Javascript to check situations for Hovers on things ? |