Prime numbers are a very important concept in scientific calculations, and prime numbers are also called prime numbers. Prime numbers are the purest numbers, numbers without any other components, and other numbers can be said to be multiplied by prime numbers. Therefore, understanding prime numbers is of great significance to mathematics and programs.
The title is: Ask for 1000! How many 0s are there at the end of the result
The code copy is as follows:
1000! = 1×2×3×4×5×...×999×1000
The code copy is as follows:
public static void main(String[] args) {
/*The factorial of 1000 is already an astronomical number, so it is impossible to calculate it. Let's see how many 0*/
/* Solution: Multiply two prime numbers 2 and 5 to get 10. We can think that as many groups 2 and 5 there are, there are as many 0 at the end */
/* Operation method: Operations 1 to 1000, see how many times each number can be divisible by 2 and 5, and count them separately. Assuming that it is divisible by 2 and 8 times and divisible by 5 12 times, then we can think there are 8 Group (2, 5), i.e. 8 0*/
//The sum of the times divisible by 2
int count2 = 0;
//The sum of the times divisible by 5
int count5 = 0;
//Transfer all numbers
for (int number = 1; number <= 1000; number ++) {
int dynamicNumber = number;// A copy of this number is used for dividing non-numbers
while (dynmicNumber % 2 == 0) { //Count how many times the number can be divisible by 2, but it is not counted separately, but counted globally
count2++;
dynamicNumber /= 2;
}
while (dynmicNumber % 5 == 0) { //Count how many times the number can be divisible by 2, but it is not counted separately, but counted globally
count5++;
dynamicNumber /= 5;
}
}
System.out.println("The number of ending 0 is: " + Math.min(count2, count5));
/* Prime numbers are a very important concept in scientific calculations. Primitives can be understood as a very simple meaning, such as: Bai Su paper, Bai Suzhen, element. Prime numbers are also called prime numbers, and matter can also be understood as simple meaning, mass, matter, essence.
* The connection between quality and quality is quality. If quality is used to describe people, it can be understood as: the most basic morality, the most primitive human nature, etc.
* Prime numbers are the purest numbers, numbers without any other components, and other numbers can be said to be multiplied by prime numbers.
* Therefore, understanding prime numbers is of great significance to mathematics and programs. */
}