This was just a continuation of an old experiment. In this case you enter a number and it will get you that number's prime factors.
I've removed the limits, but now it's relatively easy to crash your browser (or at least the flash player).
// forked from PESakaTFM's forked from: Primes
/**
* This was just a continuation of an old experiment. In this case you enter a number and it will get you that number's prime factors.
* I've removed the limits, but now it's relatively easy to crash your browser (or at least the flash player).
**/
package
{
import com.bit101.components.InputText;
import com.bit101.components.PushButton;
import com.bit101.components.TextArea;
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
import flash.utils.ByteArray;
public class PrimeTest extends Sprite
{
private const info:String = "WARNING! This app could CRASH your browser if abused.";
private const cap:uint = uint.MAX_VALUE; // 4 294 967 295
private const itterateCount:int = 1000;
// A ByteArray should be able to store 536.9 MBs of data which is 1,073,741,823 (32-bit) INTs which gets us all the way to 24,563,311,217 (the 1,073,741,823th prime [http://www.wolframalpha.com/input/?i=1073741823th+prime]).
// That number can't even be stored in a UINT so we should be good with a ByteArray.
private var prime:ByteArray = new ByteArray();
private var output:TextArea;
private var input:InputText;
private var btn:PushButton;
private var count:uint = 3;
private var end:uint = 0;
private var last:uint = 0;
private var current:uint = 0;
private var _isPrime:Boolean = true;
private var wasWarned:Boolean = false;
private var wasSeriouslyWarned:Boolean = false;
public function PrimeTest()
{
init();
input = new InputText(this, 10, 10, "0", onChange);
input.maxChars = 10;
btn = new PushButton(this, 120, 8, "Find Primality", onLookup);
}
private function init():void
{
prime.writeInt(2);
prime.writeInt(3);
prime.writeInt(5);
prime.writeInt(7);
output = new TextArea(this, 10, 30, info);
output.width = 400;
}
private function onChange(event:Event):void
{
var str:String;
var check:int = int(input.text);
if(check >= cap)
{
check = cap;
input.text = check.toString();
}
}
private function onLookup(event:Event):void
{
var str:String;
var check:int = int(input.text);
if(check >= cap)
{
check = cap;
input.text = check.toString();
if(!wasSeriouslyWarned)
{
wasSeriouslyWarned = true;
output.text = "ARE YOU REALLY SURE YOU WANT TO DO THIS!?";
return;
}
}
if(!wasWarned && check-last > 6000000)
{
wasWarned = true;
output.text = "Are you sure? This calculation could take several seconds or even minutes.";
return;
}
var tick:int = new Date().time;
while(last < check)
{
getNextPrime();
}
str = "Adding Primes took " + int(new Date().time - tick) + "ms.\n\n";
if(check == 0)
{
output.text = info;
return;
}
tick = new Date().time;
if(isPrime(check))
{
str += check.toString() + " is prime";
}
else
{
str += check.toString() + " is NOT prime and has ";
do
{
prime.position = 0;
current = prime.readUnsignedInt();
while(check%current != 0)
current = prime.readUnsignedInt();
str += current.toString()+" X ";
check = check / current;
}while(!isPrime(check));
str += check.toString()+" prime divisors";
}
str += "\n\nLooking up primes took " + int(new Date().time - tick) + "ms.";
str += "\n\nThe prime array is now "+Number(prime.length/1048576).toPrecision(4)+" MBs in size";
output.text = str;
}
private function isPrime(value:int):Boolean
{
prime.position = 0;
while(value > prime.readUnsignedInt());
prime.position -= 4;
return value == prime.readUnsignedInt();
}
private function getNextPrime():void
{
if(count >= cap)
return;
end = current = prime.position = 0;
_isPrime = true;
while(1)
{
prime.position = 0;
end = Math.sqrt(count);
while(current < end)
{
current = prime.readUnsignedInt();
if(count%current == 0)
{
_isPrime = false;
break;
}
}
if(_isPrime)
{
last = count;
prime.position = prime.length;
prime.writeUnsignedInt(last);
count += 2; //we don't need to check even numbers.
break;
}
else
{
count += 2; //we don't need to check even numbers.
_isPrime = true;
}
if(count >= cap)
return;
}
}
}
}