This post is for beginners to learn Java by themselves and write a program through trial and error with original ideas (while investigating a little), and publishes code that is not helpful at all with self-satisfaction. It is something to do.

At the same time as watching the growth of beginners with warm eyes, I think that there are naturally places where you may have the opinion "No, this is strange (# ﾟ Д ﾟ) !?", so feel free to comment in that case. please.

This is the second post. Looking back at the sentence I posted the other day, I misunderstood that the description method in Qiita was HTML format, but there is a Markdown description method, and this time I would like to write it in this way. Thank you very much for your professor m (_ _) m I hope it will be easier to see.

(* When I went home and compiled it, an error occurred, so I will post the corrected version.) (* I also use the Random class, but I learned this recently, so I tried using it for a while.)

The other day, I was asked to make a primality test program using Java, and I wrote the following code with a doy face.

```
System.out.println("Random primality test program");
java.util.Random rm = new java.util.Random();
int s = rm.nextInt(100);
if (s == 2 || s == 3) {
System.out.println(s + "Is a prime number");
} else if (s % 2 == 0 || s % 3 == 0) {
System.out.println(s + "Is not a prime number");
} else {
System.out.println(s + "Is a prime number");
}
```

(Aside from the tsukkomi that the code is dirty)

** First of all, you didn't know the "definition of prime numbers". ** **

(I like this kind of math problem because I am a liberal arts student, but I remember the formulas and so on beyond the universe.)

According to Wikipedia teacher

"A prime number is a natural number that has no divisors other than the trivial positive divisors (1 and itself) and is not 1. 』\

Well the point is

** I think that a natural number that can only be divided by the number itself and 1 is a prime number, and if it is divisible by another number, it is not a prime number. ** **

For example, it seems to be infinitely springing up like 2,3,5,7,11.

If I explain the above program a little, I can compile it without any error, but even numbers that are not prime numbers are judged to be prime numbers.

Such a program is made from the idea that if n is divisible by 2, it is an even number, and if n is divisible by 3, n is an odd number.

However, for example, 121 is the square of 11, so it is not a prime number, but it is not divisible by 2 or 3, so it is judged as a prime number.

(It's a little embarrassing at the level of starting over from the first year of high school.)

```
System.out.println("Primality test program");
int v = 123;
System.out.println("The entered number is "" + v + ""is");
if (v < 2) {
System.out.println("A number less than 2 has been entered");
} else if (v == 2) {
System.out.println(v + "Is a prime number");
} else {
boolean issosu = true;
for (int u = 2; u < v; u++) {
if (v % u == 0) {
is = false;
}
}
if (isSosu) {
System.out.println(v + "Is a prime number");
} else {
System.out.println(v + "Is not a prime number");
}
}
```

(Aside from the tsukkomi that the code is dirty)

I'm afraid this will correctly determine the prime number, but it took two days to complete. I talked with my younger brother about what kind of numbers are prime numbers, and if it is divisible by 2, it is an even number, and from the other numbers, a natural number that is not the square of a certain number is a prime number. Here is the answer that came out while relying on the net search

"** n that is not divisible by numbers less than natural number n is a prime number **"

In addition to this

"** n is a prime number if n is 2, error ** if n is less than 2"

After limiting n to numbers larger than 2 in advance, it is completed by creating a program that determines that "numbers that are not divisible by n> v" or "numbers that are not divisible by n> = n-1" are prime numbers. did.

However, if you simply use the for statement,

5 is not a prime number 5 is not a prime number 5 is a prime number 5 is not a prime number 5 is not a prime number ． ． ． I started to judge whether 5 (the number I entered) was a prime number with the number expressed in the for statement, and I struggled hard.

Well, I didn't know how to solve it, so I googled it a bit, but I defined the variable isSosu as a boolean type and set the initial value to true. Then, in the for statement, u is looped between numbers 1 less than v, and if u% v == 0, u is divisible by a natural number smaller than its own number. Substitute false for isSosu. By doing this, if it is divisible, it is false, and in other cases it can be considered again, and if (isSosu) {... and true case can be System.out.println. did. (Hmm, this is hard to understand.)

It was regrettable that I couldn't make it completely by myself because I couldn't pay attention to using boolean type and I googled it, but if a beginner builds a primality test program with the knowledge of an introductory book, this is I wonder if it will be like this, but I think there is probably a better and more efficient way to write it. I'm not good at math, and I haven't written much Java programs yet.

So, for the time being, I think that being able to write this primality test program is one of the barriers as proof when it is tested whether the program can be assembled. I think that a beginner will solve some problems when creating a program, but one of the answers is like this. I hope you can read it as if you want to see it. I think.

Recommended Posts