# help factor

• November 5th, 2010, 12:01 PM
gonfreecks
help factor
Code java:

```public class Factor {   public static void main(String[] args){ Scanner input=new Scanner (System.in);   String yn; int number; int current; String output = ""; int x; int i = 2;   System.out.print("Enter a number to factor : "); number = input.nextInt();         while (number % 2 == 0) { output += 2 +" "; number /= 2; }   x = number;   while (i <= x) { if (number % i == 0) { output += i + " "; x /= i;     } i++; }       System.out.println("Factors are : " + output); System.out.println(); System.out.print("Do you wish to do another (Y/N)? : "); //havent finished this part yet   } }```

i have this code which asks the user to enter a number and outputs the factors.....

but say the user enters 120... it should displey 2 2 2 3 5 but it only displays 2 2 2... help :(
• November 5th, 2010, 12:38 PM
javapenguin
Re: help factor
x%2 is either 0 or 1.
• November 5th, 2010, 12:40 PM
javapenguin
Re: help factor
output += 2 +" ";

Will only output 2
• November 5th, 2010, 01:02 PM
javapenguin
Re: help factor
120 % 2= 0;

output = "2 "
120/2 = 60;

number = 60;

60%2 = 0;
output = "2 2 "

60/2 = 30;

number = 30;

30%2 = 0;
output = "2 2 2 "
30/2 = 15;

15%2 = 1;
exits while loop

x = 15;

while (i <=15)

if (15%2 ==0)
it's not

i = 3;

if 15%3 = 0;

it is

output "2 2 2 3"

x/i = i

15/3 = 5;

x = 5;

i = 4;

output "2 2 2 3 5 "

15/5 = 3;

i =3;

15%4 !=0

i = 5

15% 5 = 0

15/5 = 3;

output "2 2 2 3 5"

exit while loop
• November 5th, 2010, 01:11 PM
javapenguin
Re: help factor
Code java:

```public class Factor {   public static void main(String[] args){ Scanner input=new Scanner (System.in);   String yn; int number; int current; String output = ""; int x; int i = 2; char c = 'c'; System.out.print("Enter a number to factor : "); number = input.nextInt();   boolean repeat = true; boolean isValid = false;   while (repeat == true) {       while (number % 2 == 0) { output += 2 +" "; // output = output.concat(2 + " "); works too I think   number /= 2; }   x = number;   while (i <= x) { if (number % i == 0) { output += i + " "; x /= i;     } i++; }       System.out.println("Factors are : " + output); System.out.println(); while (isValid ==false) { System.out.print("Do you wish to do another (Y/N)? : "); c = input.next().charAt(0); if (c == 'y' || c== 'Y') { repeat = true; isValid = true; }   else if (c == 'n' || c == 'N') { repeat = false; isValid = true;   }   else { System.out.println("Enter a y or n"); isValid = false; } } }     } }```
• November 5th, 2010, 02:10 PM
javapenguin
Re: help factor
~X(#-o#-o#-o#-o#-o#-o#-o#-o#-o

It appears that my while loops, while letting you repeat, don't clear the value in output.

Also, your code may not work for 121.

It appears to work for 120.

If I knew what would work, I'd save it to add to my own code to check to see if a number if a prime number and if it is odd or even. I'd have to change it since I was using longs in that one, but still.

I found a way to figure out the, unsimplified, factors of a number:

Code java:

```import java.util.*;     public class Factor2 {     public static void main(String[] args) { Scanner console = new Scanner(System.in); int number = 0; ArrayList<Integer> aList = new ArrayList<Integer>();   System.out.println("Enter a number to factor"); number = console.nextInt();   for (int num = 1; num <= number; num++) { if (number%num ==0) aList.add(num); }   String output = aList.toString(); System.out.println("The factors are: " + output); } }```

As for the glitch in my other code where it kept adding on, just add
output = "";
under

if (c== 'Y' || 'y')

However, for 121

output is 11.

It should say

ouput is 11 11
• November 5th, 2010, 02:32 PM
copeg
Re: help factor
Not sure if all the above posts helped or not, but
Quote:

but say the user enters 120... it should displey 2 2 2 3 5 but it only displays 2 2 2
If I run the code in the first post and enter 120, it displays 2 2 2 3 5...which is what you say it should display.
• November 5th, 2010, 02:48 PM
javapenguin
Re: help factor
I found that the code won't work for times when it involves perfect squares, cubes, fourths, etc.

Code java:

```import java.util.Scanner;     public class Factor {   public static void main(String[] args){ Scanner input=new Scanner (System.in);   String yn; int number; int current; String output = ""; int x; int i = 2; char c = 'c';   boolean repeat = true; boolean isValid = false;   while (repeat == true) { isValid = false; System.out.print("Enter a number to factor : "); number = input.nextInt();     while (number % 2 == 0) { output += 2 +" "; // output = output.concat(2 + " "); works too I think   number /= 2; }   x = number; System.out.println("x is:" + x); while (i <= x) { if (number % i == 0) { output += i + " "; /* if (i== Math.sqrt(number)) { output += i + " "; } */ System.out.println("Output is: " + output); x /= i; System.out.println("x is:" + x); System.out.println("i is: " + i);     } i++; System.out.println("i is: " + i); }       System.out.println("Factors are : " + output); System.out.println(); while (isValid ==false) { System.out.println("Do you wish to do another (Y/N)? : "); c = input.next().charAt(0); if (c == 'y' || c== 'Y') { repeat = true; isValid = true; output = ""; }   else if (c == 'n' || c == 'N') { repeat = false; isValid = true;   }   else { System.out.println("Enter a y or n"); isValid = false;   } } }     } }```

That and something is still wrong with my while loop.

I found that whatever is wrong is caused by a glitch in the second while loop.

I can get 2 2 if I enter 4.

but if I enter 36

I get 2 3 or something like that.
• November 5th, 2010, 04:09 PM
gonfreecks
Re: help factor
oh yeah my bad it runs 2 2 2 3 5 but if i run 150 it only runs 2 3 5 while it needs to output 2 3 5 5..

thanks for the help
• November 5th, 2010, 04:17 PM
gonfreecks
Re: help factor
and the posts of penguin is so advanced we havent even tackled some yet... but thanks for the effort though

and the 1st code you posted was an infinite loop :)
• November 5th, 2010, 04:57 PM
aussiemcgr
Re: help factor
Quote:

Originally Posted by gonfreecks
oh yeah my bad it runs 2 2 2 3 5 but if i run 150 it only runs 2 3 5 while it needs to output 2 3 5 5..

thanks for the help

Shot in the dark, since I'm just sort of walking through, but if you are not getting the last number each time, check to see if you exit the loop too early. It is a fairly common issue that you probably will never get over doing (I still do that sort of thing often enough).

*continues walking*
• November 5th, 2010, 05:10 PM
DavidFongs
Re: help factor
Quote:

Originally Posted by gonfreecks
oh yeah my bad it runs 2 2 2 3 5 but if i run 150 it only runs 2 3 5 while it needs to output 2 3 5 5..

thanks for the help

The reason that is happening is because after you find out 5 is a factor of x, you are dividing x by 5, and then incrementing i to 6. This is where it is broken, because the new value of x is also divisible by 5.

You can solve this very easily, by checking if it is still divisible by i, and if so, decrementing i, before it gets incremented, so it will check that value of i again.

Here is the code you originally posted, with a slight modification:

Code :

```import java.util.Scanner;   public class Factor {   public static void main(String[] args){ Scanner input=new Scanner (System.in);   String yn; int number; int current; String output = ""; int x; int i = 2;   System.out.print("Enter a number to factor : "); number = input.nextInt();         while (number % 2 == 0) { output += 2 +" "; number /= 2; }   x = number;   while (i <= x) { [B]if (number % i == 0)[/B] { output += i + " "; x /= i; //I added from here if(x % i == 0){ i--; } //to here } i++; }       System.out.println("Factors are : " + output);   } }```

There are still problems with this code by the way. I bolded the problem area.... I'll leave it up to you to fix it (should be a very obvious and quick fix.)
• November 6th, 2010, 05:18 PM
javapenguin
Re: help factor
Quote:

Originally Posted by DavidFongs
The reason that is happening is because after you find out 5 is a factor of x, you are dividing x by 5, and then incrementing i to 6. This is where it is broken, because the new value of x is also divisible by 5.

You can solve this very easily, by checking if it is still divisible by i, and if so, decrementing i, before it gets incremented, so it will check that value of i again.

Here is the code you originally posted, with a slight modification:

Code :

```import java.util.Scanner;   public class Factor {   public static void main(String[] args){ Scanner input=new Scanner (System.in);   String yn; int number; int current; String output = ""; int x; int i = 2;   System.out.print("Enter a number to factor : "); number = input.nextInt();         while (number % 2 == 0) { output += 2 +" "; number /= 2; }   x = number;   while (i <= x) { [B]if (number % i == 0)[/B] { output += i + " "; x /= i; //I added from here if(x % i == 0){ i--; } //to here } i++; }       System.out.println("Factors are : " + output);   } }```

There are still problems with this code by the way. I bolded the problem area.... I'll leave it up to you to fix it (should be a very obvious and quick fix.)

By any chance are you saying that it should be,
if (x% i ==0)

?
• November 7th, 2010, 02:49 AM
DavidFongs
Re: help factor
Quote:

Originally Posted by javapenguin
By any chance are you saying that it should be,
if (x% i ==0)

?

That is exactly what I am saying.