当前位置:网站首页>762. Prime number calculation setting in binary representation
762. Prime number calculation setting in binary representation
2022-07-25 23:25:00 【Mr Gao】
762. Prime number calculation set in binary representation
Here are two integers left and right , In the closed zone [left, right] Within the scope of , Count and return Calculate the set digit as a prime number Number of integers .
Calculate set digit In binary representation 1 The number of .
for example , 21 The binary representation of 10101 Yes 3 A calculation set .
Example 1:
Input :left = 6, right = 10
Output :4
explain :
6 -> 110 (2 A calculation set ,2 Prime number )
7 -> 111 (3 A calculation set ,3 Prime number )
9 -> 1001 (2 A calculation set ,2 Prime number )
10-> 1010 (2 A calculation set ,2 Prime number )
total 4 Calculate a number set to prime .
Example 2:
Input :left = 10, right = 15
Output :5
explain :
10 -> 1010 (2 A calculation set , 2 Prime number )
11 -> 1011 (3 A calculation set , 3 Prime number )
12 -> 1100 (2 A calculation set , 2 Prime number )
13 -> 1101 (3 A calculation set , 3 Prime number )
14 -> 1110 (3 A calculation set , 3 Prime number )
15 -> 1111 (4 A calculation set , 4 Not prime )
total 5 Calculate a number set to prime .
Just use the conventional method to do this problem , Just write two functions , The solution code is as follows :
int f(int n){
int count=0;
while(n){
if(n%2==1){
count++;
}
n=n/2;
}
return count;
}
bool f2(int count){
if(count<=3&&count>1){
return true;
}
if(count<=1){
return false;
}
for(int i=2;i<=count/2;i++){
if(count%i==0){
return false;
}
}
return true;
}
int countPrimeSetBits(int left, int right){
int count=0;
for(int i=left;i<=right;i++){
if(f2(f(i))){
count++;
}
}
return count;
}
边栏推荐
- Enabling partners, how can Amazon cloud technology "get on the horse and get a ride"?
- How does PHP remove an element from an array based on the key value
- Simulink学习笔记(三)——Simulink自动代码生成(二)「建议收藏」
- File contains vulnerability
- Mongodb features, differences with MySQL, and application scenarios
- TS function
- Analysis of the influence of ESM direction finding error on positioning error
- E-commerce RPA, a magic weapon to promote easy entry
- OASYS system of code audit
- Rendering, filtering (filtering) and sorting of lists
猜你喜欢

学习探索-波浪

动态内存管理

Expression of directional signal -- complex exponential signal

Tips for using (1)

Unity 使用宏

Discuz magazine / news report template (jeavi_line) utf8 GBK / DZ template download

Apple CMS V10 template /mxone Pro adaptive film and television website template

Unity uses macros

谷粒学苑P98踩坑 e.GlobalExceptionHandler : null

POI特效 市场调研
随机推荐
物理防火墙是什么?有什么作用?
How does Navicat modify the language (Chinese or English)?
Serialize operator
Several commonly used traversal methods
行云管家V6.5.1/2/3系列版本发布:数据库OpenAPI能力持续强化
【微信小程序】页面导航
About using NPM command under the terminal, the installation error problem is solved (my own experience)
动态内存管理
Network Security Learning notes-1 file upload
Enterprise level inventory management system of code audit
Tencent map API request source is not authorized, this request source domain name
Dynamic memory management
[QNX Hypervisor 2.2用户手册]9.6 gdb
asp日期函数(磁盘函数不正确怎么办)
日期类的实现
Take root downward, grow upward, and explore the "root" power of Huawei cloud AI
wordpress去掉网站发布时间
XxE & XML external entity injection utilization and bypass
@Import
npm+模块加载机制