-
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.
-
Client Software Programmer
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;
-
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.
-
explain
Originally Posted by Terrencing
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.
-
Client Software Programmer
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
-
Client Software Programmer
So j can never progress and the array will never push
-
Thanks!
I change the = sign with == and is work perfectly.
-
Client Software Programmer
-
Client Software Programmer
U still might want to put var j above the for loop and keep it as !=
-
an other issue
Originally Posted by Alloy Bacon
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?
-
Client Software Programmer
Shouldnt be. Post ur updated script
-
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());
-
Client Software Programmer
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 (i % 3 == 0){
fact.push(i);
}
if (i % 2 == 0){
exp.push(i);
}
}
-
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.
-
Client Software Programmer
if (i % 5 == 2){
fact.push(i);
}
if (i % 2 == 1){
exp.push(i*i+1);
}
??
-
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.
-
Client Software Programmer
I dont factor often thats all i got lol hopefully someone else can help you maybe terrencing
-
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.
-
Client Software Programmer
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|