In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to achieve leetcode statistics less than non-negative n prime numbers, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
Leetcode 204.
The requirements of the topic:
Count the prime numbers that are less than the non-negative number n.
Input and output examples:
Input: 100
Output: 25
Ideas for solving the problem:
The topic is relatively simple, and the prime number problem is also a common problem in all kinds of problems. The first thing to understand is, what is a prime number? A prime number, also known as a prime number, refers to a number in which a natural number greater than 1 no longer has a factor except 1 and itself.
The simplest idea is to try to divide 2 by n to n Mel 1 and find that there is an integral division. You can confirm that this is not a prime. But it takes a lot of effort, isn't it? If you want to count the numbers, you must count all the numbers.
Consider again that the factor of n cannot be greater than sqrt (n), so traversing from n to sqrt (n) is sufficient. But this is still very slow, if n is not large, if n is very large, the consumption of transport capacity will be very serious.
Then analyze the prime number, from 2 to n, there will still be a lot of unreliable numbers, such as 4, 6, 8, and so on, they must be divisible by 2, that is to say, any number that is an integer multiple of 2-sqrt (n) is definitely not a prime. First of all, you get rid of the even numbers, then you get rid of half of the numbers, and so on.
So, create an array of type bool, which is used to mark whether it is a prime or not, with a length of nought 1. And initialize it, 0 false,2,3 is set to true, starting from 4, all even numbers are set to false, others are set to true.
Then set a cycle of I from 2 to sqrt (n), still skipping the even number. Then the inner loop marks the multiple of I with false, and when the sqrt (n) is also marked, the whole array is completed. Finally, the number of true is counted, and the result can be obtained.
But after the test, I found that there is a bug, when n is very large, the length will exceed the limit, but now I can not think of a better way, who can answer? Welcome to discuss.
Implementation of Java code:
Public class Solution {
Public int countPrimes (int n) {
Boolean prime [] = new boolean [nasty 1]
/ / initial array excludes even numbers and pays attention to the annotations of the first few units
For (int I = 0; I < nasty 1; iTunes +)
{
If (I = = 0 | | I = = 1)
Prime [I] = false
Else if (I < 4)
Prime [I] = true
Else if (I% 2 = = 0)
Prime [I] = false
Else
Prime [I] = true
}
/ / Mark other units
For (int I = 3; I < (int) Math.sqrt (n); I + = 2)
For (int j = I + I; j < n + 1; j + = I)
{
If (Prime [j])
{
Prime [j] = false
}
}
Int result = 0
/ / Statistical output results
For (int I = 1; I < nasty 1; iTunes +)
{
If (Prime [I])
Result++
}
Return result
}
}
These are all the contents of this article entitled "how to count primes less than a non-negative number n in leetcode". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.