Java Displays Basic Factorization of Number

So, for my appointment, I need to write a program that asks the user for integer input, and then prints this digital 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 that I have right now is that whenever I run it, for example, number 15453, I get a list of all factors from 1 to 100 and its indicator, when I need only the main factors, and I'm stuck like act.

+6
source share
7 answers

You are almost there! Move the if-continue block outside the for loop. Otherwise, it will “continue” the inner cycle, and not the one you intended.

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

Alternatively, you can enclose a call to System.out.println in if (count != 0) , because this is the only statement following continue :

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

Your ideone program: link .

+4
source
 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); } } 

Prefixes:

8462696833 x 71

10086647 x 839

6857 x 1471

Largest Simple Ratio: 6857

+1
source

You are close:

  • The System.out.println statement should be inside the for loop and display only count>0
  • Remove if(count == 0) { continue; } if(count == 0) { continue; } , this is useless since you just increased the count
0
source

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

Not sure why you type multiplication twice! Here is the cleared 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("*"); } } } } } 
0
source

You can also get some help from the function below.

 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 coefficient of a given number.

0
source

Firstly, your continue is inside the while loop, where it has no effect. A 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 other problems:

  • Your code is not “factorized” correctly (ironically, “factored” in this context means that it is not broken down into functions
  • Variable names are poorly selected
  • In this case, you use goto ( continue ) when if will

Best code will 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); } } } } 
0
source

Source: https://habr.com/ru/post/926131/


All Articles