1st 10 prime numbers OR check whether a number is prime or not


/ Published in: ActionScript 3
Save to your folder(s)

Looking forward for the most optimized solution


Copy this code and paste it in your HTML
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
  3. <mx:Script>
  4. <![CDATA[
  5. var total:Number = 10;
  6. var primeArr:Array = new Array();
  7. public function init():void{
  8. var num:Number = total+1;
  9. var str:String;
  10. var i:Number=3;
  11. do{
  12. if(primeNumber(i) == true)
  13. {
  14. primeArr.push(i);
  15. }
  16. i++;
  17. }while(primeArr.length<10)
  18. for(var j=0;j<primeArr.length;j++){
  19. txt.text += "\n "+primeArr[j];
  20. }
  21. }
  22. function primeNumber(num):Boolean
  23. {
  24. var bool:Boolean;
  25. var nums = Math.floor(Math.sqrt(num));
  26. var i:Number = 1;
  27. bool = true;
  28. while (++i <= nums) {
  29. if ((num % i) == 0) {
  30. bool = false;
  31. i = nums;
  32. }
  33. }
  34. return bool;
  35. }
  36. ]]>
  37. </mx:Script>
  38. <mx:TextArea x="118" y="57" width="553" height="412" id="txt"/>
  39. </mx:Application>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.