I am making a small AS3 function of the Collatz conjecture. This is the code:
import flash.events.Event;
import flash.events.MouseEvent;
var numb:int=0
var amount:int=0
button.addEventListener(MouseEvent.CLICK, buttonclick)
function buttonclick(buttonclicked:MouseEvent):void{
numb=int(input.text)
trace(numb)
amount=0
}
stage.addEventListener(Event.ENTER_FRAME, equate)
function equate(equate:Event):void{
dynam.text=amount.toString() + "\n" + numb.toString();;
if(numb !=0 && numb !=1){
if(numb % 2 == 0){
numb=numb/2
amount+=1
}
else{
numb=numb*3+1
amount+=1
}
}
}
However, sometimes the textinput becomes a negative number.
123123123123 turns into -1430928461
12312312312321312 turns into -1715645152
And so on.
I do not know why, but I think it has to do with
numb=int(input.text)
But I do not want to do absolute value, because then the equation would make the wrong results. If you want it to stop on the negative number (and test it yourself) do this:
if(numb !=0 && numb !=1 && numb>1)
Instead of
if(numb !=0 && numb !=1)
12312312312321312 turns into -1715645152
That amount of digits won't fit into integer data type. Replace each int
with Number
and test again.
ie : var numb:Number=0;
and var amount:Number=0;
and numb=Number(input.text);