Need help

i need an algorithm program that factorise numbers and print out its factors .For exemple , when we do input 150 , the program should print :2355which mean that 150 = 2*3*5*5and 10x for helping
[249 byte] By [sam_bijania] at [2007-10-1 0:28:08]
# 1

Ah. A simple (and slow) way is...

void factor(int n)// assumes positive input

{

if n == 1

return

for(int x = 2 ; x < n ; x++)

{

if(n % x == 0)

print x

factor(n / x)

return

}

}

Adeodatusa at 2007-7-7 16:15:18 > top of Java-index,Other Topics,Algorithms...
# 2

A way to speed up the shown algorithm would be to utilize a list of all primes and only test divisibility with those. However, then you have to deal with how to deal with problems that may occur if the input contains a prime divisor that is larger then the largest in your list. However, if you know the input will have a particular bound, you can easily utilize that to your advantage.

jboeinga at 2007-7-7 16:15:18 > top of Java-index,Other Topics,Algorithms...
# 3
Can u give it to me in the java language plzzThanks for helping
sam_bijania at 2007-7-7 16:15:18 > top of Java-index,Other Topics,Algorithms...
# 4

> Can u give it to me in the java language plzz

> Thanks for helping

Can you give it to me in the java language?

If your answer is "no," then you need to talk to your teacher, see if you can't a) get your failing butt up to a B by learning the language and doing some work or b) get transferred out of the class.

Note that c) cheat was stricken from the answer card as it was an innately immoral choice. And we wouldn't want to tempt you with one of those, now would we?

Adeodatusa at 2007-7-7 16:15:18 > top of Java-index,Other Topics,Algorithms...
# 5

I will not give you the code, as it is a good exercise for you to learn how to use the basic features of Java. I will say that the code will look much like the algorithm already given to you, except the counter in the for loop will be an array index. The rest you should be able to fill in yourself. If you can't, ask specific questions and provide code examples of what you tried and what part does not behave how you would like it to. That way, you'll learn more, and you'll get better responses from those of us trying to help.

jboeinga at 2007-7-7 16:15:18 > top of Java-index,Other Topics,Algorithms...
# 6

I did do the code and i cant print all the factor number , it just print 2

that is the code , can anyone tell me where is the problem :

public class FactorGenerator

{

public FactorGenerator(int aNumber)

{

number = aNumber;

}

public int factor()

{

if (number==1)

return 1;

for(int i=2;i<number;i++)

{

if(number%i==0)

{

number = number/i;

return i ;

}

}

return number;

}

private int number;

}>

sam_bijania at 2007-7-7 16:15:18 > top of Java-index,Other Topics,Algorithms...
# 7
have you tried printing out the value of i before returning?
jboeinga at 2007-7-7 16:15:18 > top of Java-index,Other Topics,Algorithms...
# 8
Boy, do I feel stupid posting this :In the algorithm posted by Adeodatus, you only need to test integers from 2 to the square root (included if integer) of the given number.
weebiba at 2007-7-7 16:15:18 > top of Java-index,Other Topics,Algorithms...
# 9
>> In the algorithm posted by Adeodatus, you only need to test integers from 2 to the>> square root (included if integer) of the given number.You mean n / 2?
levi_ha at 2007-7-7 16:15:18 > top of Java-index,Other Topics,Algorithms...
# 10

> You mean n / 2?

Nope, i mean Math.sqrt(n)

if (n % x == 0) then (n % (n / x) == 0)

Now if x >= sqrt(n) that means n / x <= sqrt(n) and you've already taken care of it earlier in your loop.

public class TestPrimeFactor {

public static void factor(long n) {

if (n == 1) return;

boolean wasFactored = false;

for (long x = 2; x <= (long)Math.sqrt(n); x++) {

if (n % x == 0) {

System.out.print(x + " ");

factor(n / x);

wasFactored = true;

break;

}

}

if (!wasFactored) System.out.print(n + " ");

}

public static void main(String[] args) {

factor(1201254554551233235);

}

}

weebiba at 2007-7-7 16:15:18 > top of Java-index,Other Topics,Algorithms...
# 11
Forgot to mention that sqrt(n) is strictly smaller than n / 2 starting from n = 5
weebiba at 2007-7-7 16:15:18 > top of Java-index,Other Topics,Algorithms...
# 12

Two remarks:

* that's not exactly adeodatus' algorithm: if you would leave out the isFactored, you'd never reach 19 for 38, for instance [of course, your algorithm works, but I was replying to in the algorithm posted by Adeodatus, you only need to test integers from 2 to the square root, which is not correct]

* the x++ is unnecessarily inefficient: treat 2 as a separate case and start the loop with 3, increment by two

levi_ha at 2007-7-7 16:15:18 > top of Java-index,Other Topics,Algorithms...
# 13

> * the x++ is unnecessarily inefficient: treat 2 as a

> separate case and start the loop with 3, increment by

> two

Now we're talking true optimization ;) . To generalize your idea, we'd have to use jboeing's suggestion of using only prime numbers for the test. And while we're at it, it might be a good idea to get rid of the unnecessary recursivity.

weebiba at 2007-7-7 16:15:18 > top of Java-index,Other Topics,Algorithms...
# 14

Hey guys,

I am not sure I am following this conversation accurately. However,

I dont think the code will work accurately using a for loop. The reason

is that after a factorial is found, the for loop will continue. It seems to me that it

should be stopped after each factorial is calculated.

Adeodatus has the right idea but I suggest using while instead. Try this:

public class TestFactors {

void factor(int n) {

if (n == 1 || n<2) {return;}

int x = 2;

while(n%x != 0){

x++;

} System.out.println(x);

if(n/x!=1){factor (n / x);}

return;

}

public static void main(String[] input) {

TestFactors next = new TestFactors();

next.factor(75);

}

}

azarrabia at 2007-7-7 16:15:18 > top of Java-index,Other Topics,Algorithms...
# 15
The possible optimizations go on and on. But I honestly think he's already turned in his homework ;~)> Adeodatus has the right ideaAnd don't you forget it.~Cheers
Adeodatusa at 2007-7-20 1:47:41 > top of Java-index,Other Topics,Algorithms...
# 16

> Hey guys,

> I am not sure I am following this conversation

> accurately. However,

> I dont think the code will work accurately using a

> for loop. The reason

> is that after a factorial is found, the for loop will

> continue.

No, in the version I posted, there is a break statement.

weebiba at 2007-7-20 1:47:41 > top of Java-index,Other Topics,Algorithms...
# 17

System.out.print(n+" = ");

for(int i=2; i*i<=n;)

if(n%i != 0)i++; else{n/=i;System.out.print(i+"*");}

System.out.println(n);

no recursion, no square root, no step by two optimization, no while, no bs, and no particular merit over the previous perfectly adequate solutions.

marlin314a at 2007-7-20 1:47:41 > top of Java-index,Other Topics,Algorithms...
# 18
marlin314 that is pretty funny. anyway I declair you the winner. there is allways merit in accomplishing something with the fewest lines of code.zarab2k
azarrabia at 2007-7-20 1:47:41 > top of Java-index,Other Topics,Algorithms...