Skip to main content
Known Participant
May 1, 2007
Question

Prime Calculator

  • May 1, 2007
  • 47 replies
  • 4834 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

Known Participant
May 13, 2007
i don't understand,what does that mean... are you saying you implemented a function and ran it for that long... if so what was it and what was the output?
kglad
Community Expert
Community Expert
May 13, 2007
that's just an informational item for rr. he asked (above) if anyone could get the timeout to extend beyond 60 seconds.

the code below is an implementation of the rabin-miller method for determining if a number is prime. but you should read my message a few above this that discusses several issues related to your thread.

kglad
Community Expert
Community Expert
May 13, 2007
p.s. i've set the script timeout to 5 minutes and had flash correctly continue executing a for-loop for over 4 minutes and 50 seconds.
kglad
Community Expert
Community Expert
May 12, 2007
yes, i changed careers in 1980 and went into medicine. that kept me busy and stopped my mathematics research and publications.
Inspiring
May 12, 2007
Yeah, what kglad said. That is what I meant in my initial post, only he says it eloquently and with meaning. :)

For this type of thing Flash/AS is a fun bit of a toy, but anything serious in prime numbers you won't get far.

kglad – I didn't know you were that kind of doctor too!
Known Participant
May 12, 2007
thank you to everyone who has contributed to this. Francis Cheng... you have elevated any frustration that i clearly brought onto myself when i took on this task for my friend. Rothrock has contributed to this greatly and for that i am sincerely apprecative. in addtion, i do not think what i said perviously was appropriate becuase i was upset and tired with flash and the michael jackson comment was in deed out of line. i really appreciate the approach to took to assist me becuase after all... you dont owe me an explanation and i am very happy that you helped. the as3 is typical 'beginner' frustration and since i am spoiled with as2 i suppose i need to grow up and just learn it.

Francis, i will extensively read/research each link you have sent and like i stated earlier, i am very appreciative.

kglad... your answer is so simplistic and yet it does in fact seem feasible (of course i had to scour google to find out what that function is about). i am very greatful for your help as well.... thank you everyone... you have elevated all my frustration and i am very greatful.
kglad
Community Expert
Community Expert
May 12, 2007
the following is the continuation of a discussion that was being conducted in private:

with regard to the size of numbers that flash can handle, i believe, it is limited to 32-bit integers and cannot handle 64-bit integers reliably.

further, there's no known way to reduce the problem of deciding if a particular number is prime by evaluating numbers less than that particular number. and there's good reason to think it is impossible to do so. so, trying to reduce a primality problem of a 64-bit integer into problems involving 32-bit integers is going to fail.

regarding prime numbers, security and computers:

the reason prime numbers are used for security (in rsa, for example) is because there is no known formula/algorithm/machine that can factor large numbers in your lifetime. so, you're not going to use actionscript, nor any other programming language, to factor large numbers.

the greatest mathematicians in history have tried and failed to solve the simplest of problems: given a number, find its prime factors or even, given a number determine if it is prime. it's highly unlikely a brillant hacker will come along and find a way to crack large number factoring when the best mathematicians have failed. that's why people trust rsa (though i don't think many bankers understand that).

currently, the fastest methods to determine primality of a number use probabilistic methods. ie, given a particular number it's much easier to say it's ALMOST certainly prime than it is to say it is prime. and that's why i suggested you move from using the sieve of erastosthenes to one of the probabilistic methods based on fermat's little theorem.

and finally, just why are you trying to determine primality of numbers? if you're trying to use that for security purposes, actionscript is not adequate. 32-bit and even 64-bit numbers are not even close to being large enough for security use.

p.s. computers have greatly advanced work on prime numbers and one of the first (if not the first) proposed use of computers involved prime numbers (turing and the riemann hypothesis).

p.p.s. as machines advance and compute more rapidly, larger numbers are used for security purposes. currently, i think, numbers with several hundred digits are used to secure financial transactions.

with regard to my academic background:

B.A. in mathematics, University of California, Irvine, CA, 6/70.
M.S. in mathematics, Louisiana State University, New Orleans, LA, 6/72.
Ph.D. in mathematics, Purdue University, West Lafayette, IN, 6/76.
Postdoctoral Scholarship in Medical Genetics, University of California, Los Angeles, CA, 9/78.
M.D., Yale University School of Medicine, New Haven, CT, 6/82.
Pediatric Residency, University of California, Los Angeles, CA, 6/85.
Diplomate of the American Board of Pediatrics, 5/88.

kglad
Community Expert
Community Expert
May 12, 2007
if you're trying to find all the prime numbers in a certain range (with its lower bound significantly greater than 2), you should try using one of the probablistic methods based on fermat's little theorem.

the miller-rabin test is easy to implement and computationally feasible for flash.
Inspiring
May 11, 2007
Thank you Francis. Obviously it is your (collective your, not you personally) fault for setting the bar so extraordinarily high in the last two versions of Flash. :)

The documentation is quite good – compared to things like Word, etc. It is just a bit rough in a few places that have got me.

I will take a look at the links you have provided. I had not seen the migration chart. It would be nice, for example if things like a compile time migration error would somehow link or reference such a chart. That would lead people to find it when they need it.

Also, I just find it hard to read the help files. Having all the methods and properties on one "page" may be easier from a versioning/updating view, but often when I type something into the editor, select it and press F1 it takes me quite a while to actually locate that I have indeed come to the right entry. This is especially true for ones that are far down on the page and it might not scroll to the exact spot.

Also, it seems that often if I am using a property that is a class has inherited from something way above it. That the help doesn't take me to the correct entry. For example someone was helping me with loading multiple images using the Loader class. It turns out that the Loader class inherits the name property from the DisplayObject class. But if I do this:

myLoader.name

and select name and press f1 I am sent to the name property of the Error class. So unless I tracked down:

Loader class: nope no name property
DisplayObjectContainer: nope no name property
InteractiveObject: nope no name property
DisplayObject: Oh here it is.

Anyways that makes that a bit difficult. Here is the thread were that came up.

http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=15&catid=288&threadid=1265968&highlight_key=y

Another person started a thread about the syntax checking and I have been keeping it alive. Please see it here:

http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?catid=288&threadid=1264371

PS: Over all I am loving CS3. Please accept my congrats (and pass them along to everybody) on making such a nice product. I just want it to be a little better.
Inspiring
May 11, 2007
I have no doubt that AS3 will rock. So far I've been very impressed by its speed improvements. I just think they forgot two or three very important parts.

Syntax Checking
Decent documentation
Even the tiniest bit of help for those coming to AS3 from AS2.

Especially the documentation. In the last two versions of Flash the documentation was STELLAR. The answer to most any questions was easily had once you learned to ignore the search and just go to the class you were interested in. Yes for new folks finding the class was sometimes hit and miss, but overall it was quite simple if you used the help files.

Now you have to figure out the class you might be interested in.

Hope it doesn't inherit all of its properties and methods from some other class two or three levels up.

If so look those up too.

Read up on all the classes needed to supply the arguments. And/or all the classes used to supply return values.

And then hope very much that the specific method or property you are interested in actually has a code example at the bottom buried in the possible example.

Not to mention that many of the entries are just stubs that basically repeat the name of the method/property but in a complete sentence.

I'm sure over time I will get to love AS3, but it is incredibly frustrating at the moment.
May 11, 2007
I'm sincerely sorry that the ActionScript 3.0 documentation does not meet your needs. We not only appreciate your feedback, but also take your feedback very seriously. Just in case you haven't seen these, here are links to some articles that you may find useful:

http://www.adobe.com/devnet/flash/articles/first_as3_application.html Introduction to migration issues.
http://www.adobe.com/devnet/flash/articles/as3_debugger.html Introduction to the new debugger.
http://www.adobe.com/devnet/actionscript/articles/event_handling_as3.html Introduction to the new event handling system.
http://www.adobe.com/devnet/actionscript/articles/actionscript_tips.html Highlights differences between ActionScript 2.0 and ActionScript 3.0.
http://www.adobe.com/devnet/actionscript/articles/image_viewer.html A Colin Moock article that Walks you through the conversion of an AS2 app to AS3.

http://www.adobe.com/designcenter/video_workshop/?id=vid0130 Getting Started with AS3
(The Video workshop contains all kinds of useful tutorials, btw. They are like mini online courses.)

http://www.adobe.com/designcenter/flash/articles/flacs3_migration_08.html Part of this article describes AS2 v. AS3 and working with the script editor in Flash CS3.

This final link is part of the regular documentation, but I thought it was still worth mentioning in case you haven't seen it. The migration table lists the differences between ActionScript 2.0 and 3.0: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/migration.html

We definitely welcome any additional feedback or suggestions you or any one else may have about the documentation (we hear you about the need for more examples in the language reference). You may even want to start a new thread about the documentation to give it more visibility. We'll definitely work even harder next time to get the documentation back to where you expect it to be.
Inspiring
May 11, 2007
isaacewing,

> haha yeah, i completely agree, i HATE as3, it is the worst
> thing since micheal jackson.

I take it you're not old enough to remember when Michael Jackson was the
greatest thing since sliced bread? (And I'm only a 30-something.)

> i love as2, it was so perfect and everything flowed together
> and the syntax was robust.

AS2 is far from perfect, though it was certainly an improvement over
AS1, which in turn was an improvement over the ActionScript in Flash 4. Of
course, AS3 is far from perfect, too. I can't imagine a language that *is*
perfect ... but maybe you were exaggerating for emphasis. ;)

I don't see how it can be said that everything "flows together" in AS2.
For example -- off the top of my head -- let's say you want to load a JPG in
AS2. The concept here is loading, so a newcomer to Flash (but someone
familiar with object-oriented programming) might look for a Loader class.
As it happens, there is a Loader Component, but for the sake of discussion,
let's not venture into Component territory. Already, this newcomer feels
lost. How on earth should someone know to look in the MovieClip class in
order to find a method that loads a JPG?

And what method am I talking about? MovieClip.loadMovie(). That's load
*movie* -- now how does that "flow" with the idea of loading a static image
file? It doesn't, but we're Flashers, so we adapt. ;) Maybe, instead,
this newcomer stumbles upon the MovieClipLoader class. That would be an
alternative for loading JPGs, and a good one, because it raises events to
indicate load start, progress, complete, and more. But again, the name is
misleading, because MovieClipLoader loads far more than just SWFs (movies).

When this JPG is finally loaded -- by whatever method -- the developer
will find it is locked to a particular movie clip container. Whatever
MovieClip instance holds the JPG is, from then on, the instance that is
essentially married to it. No divorce allowed..

In AS3, SWFs, JPGs, GIFs, etc. are loaded with the Loader class. When
loaded, the image is added to the display list of an object that supports
the addChild() method -- such as a movie clip. In AS3, this JPG can be
"re-parented" to another movie clip container (or sprite, etc.) as often as
desired. Talk about robust! In AS2 this sort of thing simply isn't
possible.

By the way, there's a secret involved in my reply, isaacewing ... hold
on just a bit, and I'll get to that soon. ;)

Let's look at another example of how AS2 is arguably not a language that
"flows together"; let's look at events. In AS2, it's possible to handle
events in at least four ways. To wit: a) the on()/onClipEvent() functions;
b) dot notation handlers, such as myButton.onRelease = function(){}; c) the
addListener() method; and d) the addEventListener() method. How would a
newcomer know which approach to use? That's a good question! The anwer(s)
depend(s) on context, on which object is dispatching the events. It's one
of those "AEIOU and sometimes Y" situations, where experience -- or constant
referral to the Language Reference -- is the only thing that helps.

Contrast this with AS3, where these various approaches are all neatly
collapsed into one: addEventListener(). Someone new to the language need
only learn one technique, and it's consistent almost throughout.

Almost? Yes, almost. Certain video-related events are exceptions, and
I was shocked to learn that, because the AS3 marketing touts an overhauled
event handling system. So ... sure, AS3 isn't perfect, but I admitted that
going in. Experienced AS2 developers may reply to this message with dozens
of arguments on what makes AS2 better, and I welcome them to keep on
truckin' with AS2. ;)

Remember that secret I alluded to earlier? The secret is, I *love* AS2.
It has its foibles, but I'm very familiar with them. In a sense, they're
"my" foibles, because I work with (and around) them so often. Do I like
AS3? Do I love it? Honestly ... yes and yes. Am I struggling to learn the
new language? You betcha. I also remember struggling to learn AS2, to
learn it on a truly useful level, which involves writing class files and
maybe even dipping into design patterns to coordinate families of custom
classes. I also love JavaScript, which is (at least currently) similar in
many respects to AS1. These are all dialects of the ECMAScript
specification. I guess, the answer is I love ECMAScript.

Let's everyone keep our chins up. Some people are going to prefer AS2
for one reason or another; some are going to prefer AS3. AS3 is more
powerful; it supports strong typing at runtime, which is one of the reasons
it performs better. Rothrock has discovered that by way of his mathmatical
endeavors. In his case, that's reason enough to hike the rocky trail of a
new language. AS3's API is considerably larger than AS2's and is, to my
thinking, better organized into logically related packages. On the other
hand, AS2 is more familiar.

There's room in this town for both languages, depending on the needs at
hand.

Rothrock, the book you're going to want is Colin Moock's Essential
ActionScript 3.0, which is now available (yesss!). At 944 pages, it rivals
his ActionScript for Flash MX, the Definitive Guide in scope and is
considerably longer than the 500-page Essential ActionScript 2.0, which
presumably corresponds to the larger API for AS3.

Use this URL, and Colin gets a small percentage of the sale.

http://www.amazon.com/exec/obidos/ASIN/0596526946/ref=nosim/moockorg

I imagine, given the sort of programming you dig, you may also be
interested in Keither Peters' new AS3-specific Making Things Move! ...

http://www.amazon.com/Foundation-Actionscript-3-0-Animation-Making/dp/1590597915/


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."


Inspiring
May 11, 2007
For me there is no real reason that I would HAVE to use AS3 and the support for AS2 is still quite good in CS3.

The main thing is that AS3 is so much faster for certain math things I'm trying to do. So that is good. So my plan is to poke at it here and poke at it there and we will see what we learn.