A Flash Developer Resource Site

Results 1 to 19 of 19

Thread: array - help

  1. #1
    Junior Member
    Join Date
    Dec 2015
    Posts
    11

    array - help

    Hi!

    I make a class to decomposes a number in prime factors.

    package
    {

    public class Decomposes
    {
    // the number
    public var a:uint;

    // arrray for factors and for exponent
    public var fact:Array = new Array();
    public var exp:Array = new Array();


    public var temp = a;

    public function Decomposes(a)
    {

    var i:uint = 2;

    for (i=2; i<temp; i++)
    {
    var j:uint = 0;
    var rest:uint = a % i;
    while (rest= 0)
    {
    j++;
    a= a/ i;
    rest= a % i;
    }
    if (j!=0)
    {
    fact.push(i);
    exp.push(j);
    }
    }
    }
    public function getFactors()
    {
    return fact;

    }
    public function getExponents()
    {
    return exp;

    }
    }

    }

    in Timeline -Frame 1 - Actions


    var aecomposes= new Decomposes(100);
    trace(t.getFactors());
    trace(t.getExponents());

    No error, blank Output

    What do I do wrong?
    Please help.

  2. #2
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    public var a:uint; doesnt have a value so its probably 0; so your for loop isnt going to iterate

    so your saying temp = 0 because a is 0 so try a:unit = 100;

  3. #3
    Senior Member
    Join Date
    Aug 2012
    Posts
    115
    he is state the value at first, no need to.

    He just declare 'a' to be an 'uint' unsigned integer from the start.

    'a' actually has vale of 100 when used with Decomposes(100) 'a' then become 100


    he should be use for (i=2; i<a; i++) instead, forget temp var

    but this very confusing math array, http://www.dummies.com/how-to/conten...e-factors.html
    http://www.mathsteacher.com.au/year9...ors/number.htm
    Last edited by Terrencing; 12-18-2015 at 03:29 AM.

  4. #4
    Junior Member
    Join Date
    Dec 2015
    Posts
    11

    explain

    Quote Originally Posted by Terrencing View Post
    he is state the value at first, no need to.

    He just declare 'a' to be an 'uint' unsigned integer from the start.

    'a' actually has vale of 100 when used with Decomposes(100) 'a' then become 100


    he should be use for (i=2; i<a; i++) instead, forget temp var

    but this very confusing math array, http://www.dummies.com/how-to/conten...e-factors.html
    http://www.mathsteacher.com.au/year9...ors/number.htm

    Thanks for reply.
    I try to change temp with the a number, the rezult it still blank output.
    I belive is a problem with the array, it not placing the elements.

    What I want to do is:
    Lets take de number :100.

    The script, in the for loop, for i=2, will divide de 100 with 2, the rest will be 0.
    In the while loop, because the rest is 0, the exponent count, j, will be incremented with 1.
    The new number a will be 50, the new rest also 0, so the while loop is need to work again, the j will be 2, new number 25, the rest 1.
    In this moment fact.push(i) place the 2 in fact array, in fact[0] position, and the exponent count in the exp[0] position.
    the if loop need to restart with the i=3, but the rest will be 1, and because of the if statment, nothing will dw placed in the array.
    And so on...
    for i=5, will place de 5 in fact array, and the exponent count(j=2) in the exp array.

    That was my idea.
    Something is not working.

    ! Sorry for my bad english.

  5. #5
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Oh ur right terrence i dont think i read the end... Although this is odd: Rest is always going to be 100 mod i and than u say while rest == 0 do math so i think u messed up there so rest is never 0

  6. #6
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    So j can never progress and the array will never push

  7. #7
    Junior Member
    Join Date
    Dec 2015
    Posts
    11
    Thanks!
    I change the = sign with == and is work perfectly.

  8. #8
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Nicee yo!

  9. #9
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    U still might want to put var j above the for loop and keep it as !=

  10. #10
    Junior Member
    Join Date
    Dec 2015
    Posts
    11

    an other issue

    Quote Originally Posted by Alloy Bacon View Post
    U still might want to put var j above the for loop and keep it as !=
    I find an other issue.
    The script does not work if the prime factor is bigger then 103.

    It's too big for an array element?

  11. #11
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Shouldnt be. Post ur updated script

  12. #12
    Junior Member
    Join Date
    Dec 2015
    Posts
    11
    My updated class script:

    package
    {

    public class Decompose
    {
    public var number;
    public var fact:Array = new Array();
    public var exp:Array = new Array();
    public var temp = number;

    public function Decompose(number)
    {

    var i:uint = 2;

    for (i=2; i<temp+1; i++)
    {
    var j:uint = 0;
    var rest:uint = number % i;
    while (rest == 0)
    {
    j++;
    number = number / i;
    rest = number % i;
    }
    if (j!=0)
    {
    fact.push(i);
    exp.push(j);
    }
    }
    }
    public function getFactors()
    {
    return fact;

    }
    public function getExponents()
    {
    return exp;

    }
    }

    }

    On Timeline, Actions:

    var tecompose = new Decompose(103);
    trace (t);
    trace(t.getFactors());
    trace(t.getExponents());

  13. #13
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    I would remove that whole script and throw this in your function instead and play with the % 3 and % 2 statements

    PHP Code:
    for(i=3;i<number;i++){
    if (
    == 0){
    fact.push(i);
    }
    if (
    == 0){
    exp.push(i);
    }


  14. #14
    Junior Member
    Join Date
    Dec 2015
    Posts
    11
    Sorry, but I dont understand. With that script how can I get 50=2^1*5^2?
    I now learn the actionscript, I'm a beginner.


    with the updated script I get for 50:

    2, 5
    1, 2
    meaning prime factor 2 has exponent 1, prime factor 5 has exponent 2.

  15. #15
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    if (i % 5 == 2){
    fact.push(i);
    }

    if (i % 2 == 1){
    exp.push(i*i+1);
    }

    ??

  16. #16
    Junior Member
    Join Date
    Dec 2015
    Posts
    11
    It is fine for 50.

    But I change dynamically the number.

    Lets explain:

    For 50 I need to get 50 = 2*2*5*5 = 2^2*5^2.
    For 24 for example, 24 = 2*2*2*3=2^3 *3.

    For 77, 77=7*11=7^1*11^1.

    The script take my dynamically or random number, for example 605:

    in first place 605:2 has rest 1, so skip the while loop
    after that i will be increamented, i=3
    605:3 has rest 2, so it's not good
    next i=4, not good also
    next i=5, because 605:5 = 121 and rest 0, 5 is good, in while loop j++ will be 1 the new number 121, the new rest will be 1, because 121:5 result rest 1
    The rest !=0 so the i=5 will be placed in fact array, j=1 in exp array.

    The next numbers will be skiped, until 11, 11 will be good number(twice) and placed in array also, with exponent 2.
    This is the idea.

  17. #17
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    I dont factor often thats all i got lol hopefully someone else can help you maybe terrencing

  18. #18
    Junior Member
    Join Date
    Dec 2015
    Posts
    11
    I change temp variable with the number in for statement, and now it's working for bigger prime factors also.
    I test more, hope for no more issues.
    Thanks.

  19. #19
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Yeah just play around with the variables

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center