I wrote a primality test program in Java

Primality test in Java

I've been taking Java lectures in earnest since yesterday, and I learned about do-while statements. I decided to write a primality test program, so this is a memorandum.

Sosu.java


package sample;

import java.util.Scanner;

public class Sosu {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int num;
		boolean b = true;

		while (true) {
			System.out.println("Primality test");
			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;
			}
		}
		if (num == 2) {
			System.out.println(num + "Is a prime number.");
		} else {
			for (int i = 2; i < num; i++) {
				if (num % i == 0) {
					System.out.println(num + "Is not a prime number.");
					b = false;
					break;
				}
			}
			if (b) {
				System.out.println(num + "Is a prime number.");
			}
		}
	}

}

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. When the input is 2, it is a prime number.
  4. If the input is greater than 2, divide it by a number greater than or equal to 2 and less than num, and if it is divisible somewhere, it is not a prime number.
  5. Those that are not divisible are prime numbers. The final judgment is made by boolean.

comment

To be honest, there seems to be a better way, so if I come up with something I've learned in the future, I'll correct it. Also, I would be grateful if you could give me any advice, such as how to do this here in its current form.

Recommended Posts

I wrote a primality test program in Java
I made a primality test program in Java
I wrote a prime factorization program in Java
I wrote Goldbach's theorem in java
[Beginner] I made a program to sell cakes in Java
Primality test 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
[RSpec] I wrote a test for uploading a profile image.
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
What I learned when building a server in Java
Call a program written in Swift from Processing (Java)
I wrote a test with Spring Boot + JUnit 5 now
I made roulette in Java.
Fastest Primality Test C # Java C ++
Find a subset in Java
Null-safe program in Java (Eclipse)
I tried metaprogramming in Java
I wrote a test code (Junit & mockit) for the code that calls the AWS API (Java)
I wrote a CRUD test with SpringBoot + MyBatis + DBUnit (Part 1)
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 investigated Randoop, a JUnit test class generator for 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
Implement Table Driven Test in Java 14
I sent an email in Java
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
Do I need a test if I do DDD in a language with types?
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)
I passed the Java test level 2 so I will leave a note
[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
How to test a private method in Java and partially mock that method
I was addicted to a simple test of Jedis (Java-> Redis library)
[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