Problem description

The following is a description of the problem.

Chinese

It’s very simple, just print out the primes up to 100.

Before we do this, we need to know the definition of prime numbers: prime numbers are also known as prime numbers. A natural number greater than 1 is called prime if it is not divisible by any natural number except 1 and itself. Otherwise it is called composite (specifying that 1 is neither prime nor composite).

Prime Numbers to be used in cryptography, a public key is to want to pass information on to join the prime, when coding coding then transmitted to the recipient, anyone to receive this information, without the key, the recipient have the decryption process (is actually a process of looking for prime), will be because the process of looking for prime Numbers (prime factors) too long, So that even if the information will be meaningless.

In the design of automobile transmission gear, the number of teeth of two adjacent gears is designed as a prime number to increase the minimum common multiple of the meshing times of two same teeth in the two gears, which can enhance durability and reduce faults.

Quality can be used at a higher level, in computer science, in cryptography. For example, Hash algorithms in Java use quality.

So many interview questions like to look at primes up to 100.

The primes up to 100 are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 43, 43, 47, 53, 59, 69, 67, 771, 73, 79, 83, 89, 97, and there are 25 primes in 100.

Ideas review and source code

The difficulty of the subject is not too great. But if you haven’t encountered or brushed this question before, you may be confused.

There are a few key points to this problem. First, 1 is a special prime number, so you need to start your loop at 2. This is a lot of times different from the loop that we use that starts at zero.

And this one requires two loops, because using two loops might confuse you a little bit easier.

To avoid this, it is recommended that you use functions.

You can create a function to determine whether a given number is prime. This function is also very simple, iterating from the number 2 for a given value. Assuming that the input to this function is now 5, your loop would start at 2, and then take 5 %2, which is the mod operator.

Obviously 5%2 =1, 5%3=2, 5%4 =1. In this traversal, if you can find a number that can be divisible, then the divider is not prime, and if you can find a number that can’t be divisible, then the number is prime.

This function just returns T/F.

The key to a quick solution to this problem is to use a defined function to do it. Try not to nest a loop inside a loop, because it will be hard to get out of the loop, and it will be hard to judge the start and end of the loop.

It is also important to note that some online test platforms may not let you use lists, so you should consider using arrays or output directly.

code

Github.com/cwiki-us-do…

www.ossez.com/t/prime-num…