I wrote a prime factorization program in Java

Prime factorization in Java

I've been taking Java lectures in earnest since the day before yesterday, and I learned about do-while statements. I decided to write a prime factorization program, so this is a memorandum. (Based on the primality test program I wrote yesterday.)

Soinsu.java


package sample_0306;

import java.util.Scanner;

public class Soinsu {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int num;
		System.out.println("Prime factorization");

		while (true) {
			System.out.print("Please enter an integer greater than or equal to 2. :");
			num = sc.nextInt();

			if (num < 2) {
				System.out.println("An integer less than 2.");
			} else
				break;
		}

		int x = num;
		System.out.print(num + " = ");

		for (int i = 2;i <= num;) {
			if (x % i == 0) { //Find the smallest factor
				System.out.print(i);
				if (x != i)
					System.out.print(" * "); //Stop inserting * when x and the factor are equivalent
				x /= i; //Substitute the number divided by the smallest factor
			} else
				i++;
		}
	}

}

Contents

  1. Play the while statement whose input content is less than 2 and have it re-entered.
  2. Escape the while statement with break, and judge if the input is 2 or more with the if statement.
  3. Divide in order from 2, and if it is divisible, display the number.
  4. If x and the factor are not equivalent (still can be factored into prime factors), * is displayed.
  5. If it is not divisible, add 1 to i and loop.

comment

This is the code for displaying 21 = 3 * 7. I would be grateful if you could give me some advice.

Recommended Posts

I wrote a prime factorization program in Java
I wrote a primality test program in Java
I made a primality test program in Java
I created a PDF in Java.
I wrote a Stalin sort that feels like a mess in Java
I wrote a route search program in TDD and refactored it
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I made a rock-paper-scissors game in Java (CLI)
I wrote a Lambda function in Java and deployed it with SAM
I made a simple calculation problem game in Java
I wrote about Java downcast in an easy-to-understand manner
I tried to create a Clova skill in Java
I tried to make a login function in Java
Call a program written in Swift from Processing (Java)
I made roulette in Java.
Find a subset in Java
Null-safe program in Java (Eclipse)
Calculate prime numbers in Java
I tried metaprogramming in Java
I want to perform high-speed prime factorization in Ruby (ABC177E)
I just wanted to make a Reactive Property in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I tried to convert a string to a LocalDate type in Java
I tried to make a client of RESAS-API in Java
I wrote a C parser (like) using PEG in Ruby
I wrote a code to convert numbers to romaji in TDD
I made a program in Java that solves the traveling salesman problem with a genetic algorithm
I sent an email in Java
Ruby: I made a FizzBuzz program!
3 Implement a simple interpreter in Java
I made a shopify app @java
A simple sample callback in Java
Create a Servlet program in Eclipse
I made an annotation in Java.
I tried using JWT in Java
Get stuck in a Java primer
I can't create a Java class with a specific name in IntelliJ
[Note] What I learned in half a year from inexperienced (Java)
[Note] What I learned in half a year from inexperienced (Java) (1)
[Note] What I learned in half a year from inexperienced (Java) (3)
About returning a reference in a Java Getter
[Java] I participated in ABC-188 of Atcorder.
What is a class in Java language (3 /?)
I tried using Elasticsearch API in Java
I tried a calendar problem in Ruby
When seeking multiple in a Java array
[Ruby] A program that determines prime numbers
[Creating] A memorandum about coding in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
I tried the new era in Java
[JAVA] Project Euler, I got stuck in Q8, so make a note
[Introduction to Java] How to write a Java program
Java creates a table in a Word document
Java creates a pie chart in Excel
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
Create a TODO app in Java 7 Create Header
Try making a calculator app in Java
Sample program that returns the hash value of a file in Java
I wrote EX25 of AtCoder Programming Guide for beginners (APG4b) in java.
Implement something like a stack in Java