1

I am currently trying to make a program which is able to prime factorize any given number, but I am having some problems. I have already coded a program which generates the first x primes, and I thought I could use this program to solve my former task.

The problem I encounter is that the program can only prime factorize small numbers. When I input a higher number for var tall, the answer is wrong. Does anyone have a suggestion on how I can get the program to work properly?

I am new to the language, and I am sure its possible to write the code much more effective.

import flash.events.MouseEvent;
factorize.addEventListener(MouseEvent.CLICK, func1);
var primeNumbers: Array = new Array(2, 3); //the first primal numbers
var maxNum = 100;

function check(num) {
    for (var i = (num - 1); i > 1; i--) {
        if ((num % i) == 0) {
            return false;
        }
    }
    return true;
}

var lastNum: int = primeNumbers[primeNumbers.length - 1];
var nextNum: int = lastNum + 1;

while (primeNumbers.length < maxNum) {
    if (check(nextNum) == true) {
        primeNumbers.push(nextNum);
        nextNum++;
    } else nextNum++;
}
trace(primeNumbers);



function func1 (evt:MouseEvent) { //factorizing function
    var tall:int = 18; //the number i want i factorize
    var num:int = 0;
    var factor:Array = new Array();

    while (num<tall) {
        while (int(tall/primeNumbers[num]) == tall/primeNumbers[num]) {
            trace(tall+"/"+primeNumbers[num]+"="+tall/primeNumbers[num])
            factor.push(primeNumbers[num]);

            var next = tall/primeNumbers[num];
            while (int(next/primeNumbers[num]) == next/primeNumbers[num]) {
                factor.push(primeNumbers[num])
                trace(next+"/"+primeNumbers[num]+"="+next/primeNumbers[num])


                var next2 = next/primeNumbers[num];
                while (int(next2/primeNumbers[num]) == next2/primeNumbers[num]) {
                    factor.push(primeNumbers[num])
                    trace(next2+"/"+primeNumbers[num]+"="+next2/primeNumbers[num])


                    var next2 = next/primeNumbers[num];
                    while (int(next2/primeNumbers[num]) == next2/primeNumbers[num]) {
                        factor.push(primeNumbers[num])
                        trace(next2+"/"+primeNumbers[num]+"="+next2/primeNumbers[num])
                        num++;
                    }
                num++;
                }
            num++;
            }
        num++;
        }
    num++;
    }
    trace(tall + " = " + factor);
}
BiscuitBaker
  • 1,421
  • 3
  • 23
  • 37
Smeestad
  • 37
  • 7
  • "the answer is wrong", could you please attach an example, with the expected outcome? (just for claritys sake) Also, you might want to look into [Recursive Functions](http://www.designswan.com/archives/as3-recursive-functions-vs-loop-functions.html) which are commonly used for factorial calculations. –  Dec 09 '14 at 09:24
  • Thank you for the answer! When the variable (var tall) i want to factorize is for example equal to 6, 12 or 18, my code consequently traces "6=2*3", "12=2*2*3" etc. But if try with the var tall = 288, the output is "288=2*2*2*2*3", which is incorrect. @DodgerThud – Smeestad Dec 09 '14 at 09:49
  • I think the method i am trying to use is pretty much useless, it won't be able to factorize variables with a high amount of prime factors(?). – Smeestad Dec 09 '14 at 09:53

1 Answers1

0

Try this :

function prime_factorization( n:int ): Array {

    var a:Array = [] ;

    for (var i:int = 2; i <= n / i; i++) {  

        while (n % i == 0) {

            a.push(i) ;
            n = n / i ;

        }

    }

    if (n > 1) a.push(n) ;

    return a ;

}

trace(prime_factorization(12)) ;            // gives : 2,2,3
trace(prime_factorization(288)) ;           // gives : 2,2,2,2,2,3,3
trace(prime_factorization(35206)) ;         // gives : 2,29,607
akmozo
  • 9,829
  • 3
  • 28
  • 44
  • After a couple of tries i managed to modify my code so it was able to factorize values up to 400(which was my original task). Your code was much more effective, and is able to factorize any integer. Thank you! So do you not need an array consisting of all the primal numbers to prime factorize? – Smeestad Dec 09 '14 at 12:18
  • @akmozo Your code is correct but: 1. You should always specify the type of value your function is supposed to return (here: Array). 2. To declare a new array, use Array literals rather than new Array(). Do this:[], not this:new Array() 3. Terminate each statement with a semicolon. For more information: [AS3 coding standards](http://wiki.opensemanticframework.org/index.php/AS3_Coding_Standards#Array_literals) – helloflash Dec 09 '14 at 12:25
  • @MattisTrygstad No, we don't need an array of all the primal numbers because the `while` loop do the job. – akmozo Dec 09 '14 at 13:08