Hello, After a few unsuccessful tries, I can't make a script that'll check weather a number is prime or not. I found a function that supposedly does this, but I have no Idea how it works: Number.prototype.isPrime = function(){ if (this == 2) return 1; if (this < 2 || (this & 1 == 0)) return 0; var s = Math.floor(Math.sqrt(this)); for (i = 2; i <= s; i += 1) { if (this % i == 0) return 0; } return 1; } I found http://www.layer51.com/proto/d.aspx?f=530. It has a usage on the page... but its pretty useless (oh, the irony)... Can anyone help me to use this function, point me towards an easier one, or give me any way to find out whether a number is prime or not? Thanks, -Donald J
[quoted text, click to view] > It has a usage on the page... but its pretty useless (oh, the irony)...
In fact, the usage part confuses me, too. Bokel and the other guys mentioned in the credits section are guru type flashers, so there had to be some confusing stuff in their work ;) Maybe it helps you to see when I explain the usage a bit differently: test = 3567; if (test.isPrime() == 1) { trace(test+" is prime!"); } else { trace(test+" is not prime!"); } You'd have to put the prototype AS you quoted into your flash file (frame 1, main timeline would be good). Whenever you need to check whether a number is prime you can use my if statement above. You'd surely want to exchange the TRACEs to something better suited to your needs. ;) ---
Don, how many primes do you need to check? You could just get a list of them and put them in an array, check the number against the array when needed. something like: //here's the first 30 primes, but there are bigger lists available var primes:Array = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113]; function checkPrime(input:Number):Boolean { for(var i=0; i<primeNums.length; i++) { if(primes[i]==input) { return true; break; } } } //example call to the function - return is 'undefined' (if not prime) the condition will not be satisfied if(checkPrime(37)) { //execute event }
You could just write a simple isPrime() function like so: function isPrime(num:Number):Number { if (num < 2) { return false; } for (var i = 2; i < num; i++) { if (num % i == 0) { return false; } } return true; } A number is prime if it has no natural divisors aside from 1 and itself. So you loop from 2 up to 1 less than your number. If any of those numbers can evenly divide into your number the number is not prime. To check that you use the mod (%) operator. It returns the remainder of integer division - so if there is no remainder (0) then the number evenly divided HTH -- Dave - Head Developer http://www.blurredistinction.com Adobe Community Expert http://www.adobe.com/communities/experts/
yes myself as well :) although, Christian's and Dave's methods were both far superior to my cheat ;) I guess I do have a tendancy to wonder about how large numbers would effect the processing speed slowdown during the loop, haven't tested this though, just thinking out loud :)
The usage is just to show that if you have a number assigned to a variable, such as b, then you can check if it is prime by: b.isPrime(); The crazy way they assign b, is really of no importance to the usage example, so it could also just read: b = 599 trace(b.isPrime()); Dave, what you have said is almost right. As we see in the example, you don't have to loop all the way up to n-1. If you don't find a factor by the time you have reached the square root of n, then you won't find any after that either. There was a long thread about primes here a month or two ago. I think it might have been in the Actionscript section. So you might want to look for that.
One quick thought on the above procedure. A classic way of finding primes is known as the Sieve of Eratosthenes. It does what is described above except that there's no need to check every single number from 2 to N-1. Every time you check a number, you can remove all the multiples of that number from your list to check. Starting with 2, for instance, you can forget about checking 4,6,8... So check 3, then eliminate 6,9,12 and so on. After you do this for the first 5 or 6 primes, most of your numbers in the list will have been eliminated. Will this actually be faster than dividing every number in between? Don't know but it might be fun to try. P.S. there is lots of available java script code to do this,
Don't see what you're looking for? Try a search.
|