Skip to main content
Known Participant
May 1, 2007
Question

Prime Calculator

  • May 1, 2007
  • 47 replies
  • 4835 views
Hello everyone,

for the last week i have been trying to develop a prime number calculator. at first, it was a single input function that determined if it was prime or not. now it takes two numbers (min, max) but it takes to long and is not efficient enough to computer anything large within a certain range. (i.e. 0-15000). how would i make this algorithm significantly more efficient... without using the for loop to compare each number against a modulus check.

////////////////////////////////////////////////////////////////////
code attached with screen print of flash file
////////////////////////////////////////////////////////////////////
download zip version with all files:
rar version: prime number test (flash cs3+as2)
zip version: prime number test (flash cs3+as2)
thank you for any help
isaaac
This topic has been closed for replies.

47 replies

Inspiring
May 2, 2007
I don't know if matrix math will help you, it is just an example of some processor intensive stuff that I'm trying to do in Flash. So I'm just saying that be open to the idea that as you work with this you might discover some tricks specific to what you are doing that make the whole thing easier/faster.

As for the time test, yes, I just copied his Sieve code, removed the traces, and then tried it as a frame code. Make sure that even if you are using CS3, that you are publishing for the correct player and the correct flavor of AS.
Known Participant
May 2, 2007
yeah, i tested the prime number calc with 2 million and after the initial 60 seconds it starts to die. i tested it to 72 seconds and man it was sluggish. i was also using flash 9 pro.

do you think it would help if i assembled the data in a 2d array?

i have heard of presenting the for loop with the large number first so as the information and data list (array's) become filled with data, the actual data is significantly smaller keeping the speed at a fair equilibrium. also, doing a sort or reversing the matrix at the end is always an easy way of finishing all that off.

i didn't realize calculations were faster on local variables, it seems like that is common sense because they are local as oppose to somewhere else where they would have to be located each time they are needed.

what do you mean by matrix by its transpose?

well well well, kglad is a professional mathematician huh... wow haha that would help... we are two rookies to the game, im guessing your pretty good at a.s. in flash, im likewise but im not mathematician. did you know if they want to get their phd in math, they have to either prove or disprove something... oh my god, how hard would that be??? i don't think i could ever do that.i don't know him but i would like to meet him. do you know when he gets back or maybe when he left.

when you say you ran nsurvey's code ni as3 and it timed at 120 ms, what input value did you use (30k). yeah all this is in flash 3... download the zip or rar file i attached to this at the end of my origianal post... it has all the files in it.
Inspiring
May 2, 2007
I don't know what to tell you. When we get to math like this it is just at the end of my ability to grasp. Personally I'm working on doing some large matrix manipulations and that is taxing my brain so I don't have much left to offer for your issue. :)

In many ways I've managed to speed up what I'm doing, but Flash really isn't cut out for the maths. So at a point I just have to bite the bullet and spread out the calculations. When publishing for Flash 9 you can change the timeout from the default 15 seconds, but it seems to max out at 60? (Anybody else confirm that?)

Here are a couple of tricks I've found for my matrices:

Iterate backwards through loops, ie for(var i=0;i<256*256;i++) is slower than for (var i=256*256-1;i>=0;i--); I think there is even a slightly faster version of this, but I can't remember exactly how it is formatted.

Calculations are quicker on local variables.

Look for tricks. In my case I'm multiplying a 16 x 65536 matrix by its transpose. In that case the result will always be a symmetric matrix. So I only have to compute a bit over half and then just copy that to the other triangle.

I don't know if any of that helps you. Maybe we can keep this topic alive until kglad comes back from his cruise! (If you don't know, he is one of the most helpful folks here AND he happens to be a professional mathematician.)

PS: Are you using Flash CS3? When I publish Nsurveyor's code for AS3 it runs in about 120 milliseconds. (I removed the trace of the primes because that slows things down a bit.) If I change the publish settings to AS2 it takes about 1500 milliseconds. Wow! A whole order of magnitude is insane. I'm going to have to rework my matrix stuff for AS3 I think!
Known Participant
May 2, 2007
i have implemented the script for help although it still is not efficient enough... i am testing numbers starting from range 1,000,000. the end function when it is all finished will take a min and a max for input and go from min to the max but for now, it goes from 0 (i know its not a prime, but it is a start) to whatever is defined as the max number. i used the sieve of eratosthenes function and found it to be very good until i started testing it in the 1,200,000 range it gives me the 'error, not responding' message (i know it is from the timeout set by flash after 15(s). that is not the problem (sort of), i need it to be more efficient. i want to use numbers in the 64-bit range (it is for password and encryption generation). i was reading the http://en.wikipedia.org/wiki/Sieve_of_Atkin page and seems like that is the best algorithm to infuse into this project however i am not sure how to set that up. the pseudocode is a little to... disoriented... anyone... any suggestions... anything... please!!! thank you rothrock and nsurveyor for the help already, you have been very nice and i do appreciate it very much.

ALSO, i was reading a million... maybe even five million... different pages relating to the comparisons between sieve of eratosthene/atkin and came to the following conclusion. since they are based on wheel factorization why couldn't you somehow infiltrate their code with pi and simplify those together using the pi for accuracy and speed... does that make sense.
Inspiring
May 2, 2007
Hey NSurveyor – how is it going?

That is really cool. I suspected there was a better way and know we know!
May 2, 2007
Hey. I'm doing pretty good. I haven't been on the forums in quite awhile... busy with schoolwork and other things, but I'm trying to fit it back in. And I haven't done much flash work either... only interesting thing would be 100% AS Tetris - http://geocities.com/saif7463/tetris.swf . How're you?

Yup, the Sieve of Eratosthenes has always been fascinating to me. The steps are so simple and primitive, yet it produces the primes.
May 1, 2007
Your primality test is quite slow, because it checks all the way up to the number itself until it finds a divisor. However, we only need to go up to sqrt(x), because if there were a prime factor,q > sqrt(x), then q/x < sqrt(x) and q/x would divide the number.

I was confused as to why you have your program show the non-primes as opposed to the primes? You use "tavn" and "numv" in the final output... I wasn't sure if this was intended, so I left it alone.

Also, there's no need to initialize variables as undefined, as they already will be. And, you can't delete variables after return, but they are local variables anyways so it doesn't matter.

Using the attached code, I got all primes less than 20000 in 1.376 seconds.
May 2, 2007
A much better way to find primes is to use a prime sieve, as opposed to checking if each number in a range is prime. One very simple and easy to implement would be the Sieve of Eratosthenes. Basically, you start with all numbers from 2 to m. You start with 2, and cross out all multiples of 2 (from 2*2). The next uncrossed out number (3) is your next prime, and then you cross out all multiples of 3 from (3*3 as 3*2 is already gone). The next uncrossed out number (5) is your next prime, and then you cross out all multiples of 5 (from 5*5 as 5*4, 5*3, 5*2 are all gone), etc. Check out http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes for more information.

Using this method, I could calculate all primes less than 30000 in 0.994 seconds.
Inspiring
May 1, 2007
Well you've got a couple of things going against you. Finding primes is hard and Flash is bad at math!

There might be better algorithms out there so first be sure you do a good google search.

By taking too long do you mean you get the "A script is causing Flash to run slow." message? If so, you can break up your for loop by using a setInterval to pass your prime_test loops chunks of 100 or 1000 or whatever seems to work best. If it gets through a chunk then it sets another short interval before starting the next. Be very careful when doing this because you can get into an endless loop. So be SURE to save every time before you test it.

Other than that I can't really think of much you could do.