RikYC
RikYC

Reputation: 81

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.

Upvotes: 8

Views: 46121

Answers (8)

Brody
Brody

Reputation: 1

re did this in my comsic class and added some visuals to it and now it works with bigger numbers haven't tested the limit

public class PrimeFactoringMachine 

import java.util.Scanner;
{
    public static void main (String [] args)
    {
    System.out.println("im here to faztorize any number you give me");
    Scanner scanner = new Scanner (System.in);
    printFactors(scanner.nextInt());    
    }       
        public static void printFactors (int product)   
        {
            String toPrint = " ";
            toPrint.concat("\n");
            int numberOfTabs = 0;
            
            System.out.print("  " + product + " = ");
            
            int talk = product;
            boolean space = true;
            for (int factor = 2; factor <= product; factor++)
            {
                
                int exponent = 0;
                while (product % factor == 0)
                {
                    product /= factor;
                    
                    if(product == 1)
                    {
                        
                    }
                    else
                    {
                        toPrint = toPrint.concat("/   \\" + "\n");
                        for(int i = 0; i < numberOfTabs; i++)
                        {
                            toPrint = toPrint.concat("   ");
                        }
                        numberOfTabs++;
                        toPrint = toPrint.concat(Integer.toString(factor));
                        toPrint = toPrint.concat("   ");
                        toPrint = toPrint.concat(Integer.toString(product) + "\n");
                        for(int i = 0; i < numberOfTabs; i++)
                        {
                            toPrint = toPrint.concat("   ");
                        }
                    }
                    exponent++;
                }

                if (exponent > 0 && factor > 0)
                {   
                System.out.print ("(" +factor + "^" + exponent + ")");
                }
                else
                {
                
                }
                        
             }
            System.out.println();
            System.out.println(toPrint);
        }
            
}

Upvotes: 0

user2942648
user2942648

Reputation: 1

remove the if (count == 0) {continue;} statement from the while loop and put it after it, in the for loop.

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);
    }

Upvotes: -1

user3003976
user3003976

Reputation: 329

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

Upvotes: 2

pradeep
pradeep

Reputation: 1

you can also get some help from the below function.

public int getPrimeNumber(double number) {
    int j = 0;
    while (number % 2 == 0) {
        number = number / 2;
        j = 2;
    }

    for (int i = 3; i <= number; i = i + 2) {
        while (number % i == 0) {
            number = number / i;
            j = i;
        }
    }

    return j == 0 ? 1 : j;
}

This function will return the largest prime factor of the given number.

Upvotes: 0

Michael Lorton
Michael Lorton

Reputation: 44436

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);
            }
        }
    }
}

Upvotes: 1

samsina
samsina

Reputation: 1013

Not sure why you are printing the multiplication twice! Here is the cleaned up code:

public static void printPrimeNumbers(int prime) {

    int n;

    for (int i = 2; i <= prime; i++) {
        n = 0;
        while (prime % i == 0) {
            prime /= i;
            n++;

        }

        if (n != 0) {
            for (int j = n; j > 0; j--) {
                System.out.print(i);

                if (prime != 1) {
                    System.out.print("*");
                }
            }
        }
    }
}

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727137

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.

Upvotes: 4

mprivat
mprivat

Reputation: 21912

You're close:

  1. The System.out.println statement must be inside your for loop and only display for count>0
  2. Remove the if(count == 0) { continue; }, it's useless since you just incremented count

Upvotes: 0

Related Questions