Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Display the Prime Factorization of a number

So for my assignment, I have to write a program that asks the user for an integer input and then print out that number's prime factorization. This is what I have:

import java.util.Scanner;

public class PrimeFactor {
    public static void main(String[] args) {
        System.out.print("Enter a positive number: ");
        Scanner scanner = new Scanner (System.in);
        int number = scanner.nextInt();
        int count;
        for (int i = 2; i<=(number); i++) {
            count = 0;
            while (number % i == 0) {
                number /= i;
                count++;
                if (count == 0) {
                    continue;
                }
            }
            System.out.println(i+ "**" + count);
        }
    }
}

The problem I have right now is that whenever I run it with, like, the number 15453, I get a list of every factor from 1 to 100 and its exponent when I only want the prime factors, and I'm stuck as to how to proceed.

like image 460
RikYC Avatar asked Sep 24 '12 17:09

RikYC


3 Answers

You are almost there! Move the if-continue block outside the for loop. Otherwise, it "continues" the inner-most loop, rather than the one you intended.

while (number % i == 0) {
    number /= i;
    count++;
}
if (count == 0) {
    continue;
}
System.out.println(i+ "**" + count);

Alternatively, you could enclose the System.out.println call in if (count != 0), because it's the only statement following the continue:

while (number % i == 0) {
    number /= i;
    count++;
}
if (count != 0) {
    System.out.println(i+ "**" + count);
}

Your program on ideone: link.

like image 130
Sergey Kalinichenko Avatar answered Oct 17 '22 23:10

Sergey Kalinichenko


public class _03_LargestPrimeFactor {

public static void main(String[] args) {

    long a = 600851475143L;

    for(int i=2; i<(a/i); i++){                         // no factors would exist beyond a/i for a particular i

        while( a%i == 0){                               // if i is a factor 
            a = a/i;                                    // divide a by i else we wont get a prime number
            System.out.print(a + " x " + i + "\n");
        }
    }

    if(a > 1)
    System.out.println("largest prime factor: " + a);
}

}

console:

8462696833 x 71

10086647 x 839

6857 x 1471

largest prime factor: 6857

like image 33
user3003976 Avatar answered Oct 17 '22 21:10

user3003976


For one thing, your continue is inside the while loop, where it has no effect whatsoever. The minimal fix would be

public class PrimeFactor {
    public static void main(String[] args) {
        System.out.print("Enter a positive number: ");
        Scanner scanner = new Scanner (System.in);
        int number = scanner.nextInt();
        int count;
        for (int i = 2; i<=(number); i++) {
            count = 0;
            while (number % i == 0) {
                number /= i;
                count++;
            }
            if (count == 0) {
                 continue;
            }
            System.out.println(i+ "**" + count);
        }
    }
}

But you have some other problems:

  • Your code is not properly "factored" (ironically, "factored" in this context means that is is not broken up into functions
  • The variable names are poorly chosen
  • You use a goto (continue in this case) when an if would suffice

Better code would be

public class PrimeFactor {
    public static void main(String[] args) {
        System.out.print("Enter a positive number: ");
        Scanner scanner = new Scanner (System.in);
        printFactors(scanner.nextInt());
    }
    public static void printFactors(int product) {
        for (int factor = 2; factor <= product; factor++) {
            int exponent = 0;
            while (product % factor == 0) {
                product /= factor;
                exponent++;
            }
            if (exponent > 0) {
                System.out.println(factor+ "**" + exponent);
            }
        }
    }
}
like image 1
Michael Lorton Avatar answered Oct 17 '22 21:10

Michael Lorton