JavaScript - Prime Factors
i am using trying to use the following code to find the prime factors of a number:
Code: function prime(number){//returns prime factors of input in array test=2;//number to test divisibility factor_count=0//counts number of factors factors=[]; while (test<number){ quotiant=number/test quotiant_round=Math.floor(quotiant); if(quotiant==quotiant_round){//number is divisible by test factor_count=factor_count+1; factors[factor_count]=test; number=number/test; test=test-1;//counters adding one } test=test+1; } return (factors) } running it manually in chromes JavaScript console i get: prime(2)=[] prime(4)=[undefined, 2] prime(8)=[undefined, 2, 2] prime(6)=[undefined, 2] prime(9)=[undefined, 3] prime(27)=[undefined, 3, 3] prime(18)=[undefined, 2, 3] i am using the = to separator my input from its output Similar TutorialsI need some help putting the factors of a number (y) into an array For example if y = 20: [1, 2, 4, 5, 10, 20] Once I have this, I can use it to factor a quadratic: (ax = c)(bx = d) if c = 3 and d = 5 then I could check if 1c was a decimal, if 20d was a decimal and then vice versa Basically, I can use these numbers to see if b or d is a whole number and if it isn't, try the next one. If none of them are, then I can do y(ax + c)(bx + d) Thanks! I have the following code attached that gives me a prime number. I can't seem to put the logic behind having this code print from an alert the prime numbers from 1-100. Of course, this code is only giving me "the" prime number. I just don't know where to begin, or how to store each number. Array maybe?
Code: var finding_primes = function() { var number = 31, prime = true; for ( var i = 2; i < number; i++ ) { if ( number % i == 0 ) prime = false; } if (prime) { alert( number + " is prime."); } else { alert( number + " is not prime."); } } I would like to create a script that allows for a user to input two integers and to have the script show all the prime numbers that fall between them. Any thoughts? Thanks Basically I want to factor a number and select the two closest numbers to the middle of the factors for example: 20 factors to 1, 2, 4, 5, 10, 20 I want it to return 4 and 5 If the number is has an odd number of factors (has to be a perfect square) I want it to select the middle number and say it twice for example: 64 factors to 1, 2, 4, 8, 16, 32, 64 I want it to return 8 and 8 Would I use an array or what? My code should probably look like: PHP Code: x = //Factors array y = //number of items in x if (/*y is even*/){ i = y/2 i2 = i+1 a = [i] b = [i2] } else{ i = //somehow the center number? i2 = i a = [i] b = [i2] } |