public static String factorX(long x){
String factor="";
long number = x;
long i = 2;
while (i < number) {
if (number % i == 0) {
factor += i+", ";
number /= i;
} else {
i++;
}
}
return factor;
For example if I put 120, i get 2,2,2,3. I am missing factor 5. Everything else works find;
is just that thing that is not displayed.....................................................................
Change:
while (i < number) {
to:
while (i <= number) {
and you're good to go. However, this wouldn't print 1 or 2 as the prime factors of the numbers 1 and 2, respectively. So you might want to add something like this as well:
if(x < 2) {
return "" + x;
}
return factor;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.