[Brush questions diary] 762. The calculation of prime numbers in binary representation is set

[TOC]

Get into the habit of writing together! This is the 5th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

[Brush questions diary] 762. The calculation of prime numbers in binary representation is set

The 25th article of this writing diary, the title is: 762. The calculation of prime numbers in binary representation is set , simple

1. Topic description:

On the 3rd day of the Qingming holiday, after returning to Shenzhen, I made some adjustments to see what the leetcode question is today? Still have to keep it in the habit, always take some time every day to do something you want to do

It's a simple question to take a closer look, I should be able to go to bed earlier tonight

2. What idea does this question examine? What is your thinking?

We still have to take a closer look at the information the question itself conveys to us:

  • The question will give an interval, the data in the interval are all integers, we need to calculate the number of 1 in the binary number of each number in this interval
  • We need to know how to check if a number is prime
  • After completing the above 2 steps, we can use the fool-like traversal method to achieve this problem

To calculate how many 1s the binary number of a number contains, we can use the tossing and dividing method to process , of course, we can also use library functions to process, here we use golang, then if we directly use the library function to calculate , It can be used like this:bits.OnesCount(具体的 uint 数字)

So the question comes to what is a prime number?

If a number is greater than 1, and the number is divisible only by 1 and itself, and not by any other number, then the number is prime

Regarding how to check that a number is a prime number, we learned and practiced it when we were studying the C language in college, so we won't go into details here.

3. Coding

According to the above logic and analysis, we can translate it into the following code

The encoding is as follows:

func countPrimeSetBits(left int, right int) (res int) {
	// 直接傻瓜式的遍历
    for i:=left;i<=right;i++ {
        // golang 中 使用 bits.OnesCount() 来计算 uint 数中 1 个个数
        if isPrimeNum(bits.OnesCount(uint(i))){
            res++
        }
    }

    return
}

// 校验是否是质数
func isPrimeNum(num int) bool {
    if num < 2 {
        return false
    }

    for i:=2; i*i <= num; i++ {
        if num %i == 0 {
            return false
        }
    }
    return true
}
复制代码

The coding of this question is actually very conventional.

  • The first step is to traverse the array given by the question
  • The second step is to calculate the number of 1s in the binary number of the specific number
  • Check if incoming data is a prime number

4. Summary:

Then, for this question, its space complexity is obviously O(1), because we only introduce constant-level space consumption

So what is its time complexity? xdm Do you have a clear concept? If you are interested, you can try to calculate its time complexity. For the time being, I will write it out indefinitely.

Original title address: 762. The prime number calculation set in binary representation

I am here today, what I have learned, if there are any deviations, please correct me

Welcome to like, follow, favorite

Friends, your support and encouragement are the motivation for me to persist in sharing and improve quality

Okay, here it is this time

Technology is open, and our mentality should be open. Embrace change, live in the sun, and move forward.

I'm the little devil boy Nezha , welcome to like, follow and collect, see you next time~

Guess you like

Origin juejin.im/post/7083139872822657061