当前位置:网站首页>N states of prime number solution
N states of prime number solution
2022-04-23 07:19:00 【Amyniez】
The first one is : The basic method
//4. Judge i Prime or not , Primes are numbers that can only be 1 And the number divisible by itself
// lookup 1-100 The prime between
#include <math.h>
#include <stdio.h>
int main()
{
int i = 0;
int count = 0;
for (i = 1; i <= 100; i++)
{
// Trial division
int j = 0;
for (j = 2; j <= i; j++)
{
if (i%j == 0)
{
break;
}
}
if (j == i)
{
count++;
printf("%d ", i);
}
}
printf("\ncount = %d\n", count);
return 0;
}
The second kind : Modify the search method , Use the square method to reduce the number of searches
int main()
{
int i = 0;
int count = 0;
for (i = 1; i <= 100; i++)
{
// Trial division
int j = 0;
for (j = 2; j <= sqrt(i); j++)// Square root
{
if (i%j == 0)
{
break;
}
}
if (j >sqrt (i))
{
count++;
printf("%d ", i);
}
}
printf("\ncount = %d\n", count);
return 0;
}
The third realm : Look for prime numbers in odd numbers
int main()
{
int i = 0;
int count = 0;
for (i = 1; i <= 100; i+=2)// Look for prime numbers in odd numbers
{
// Trial division
int j = 0;
for (j = 2; j <= i; j++)
{
if (i%j == 0)
{
break;
}
}
if (j == i)
{
count++;
printf("%d ", i);
}
}
printf("\ncount = %d\n", count);
return 0;
}
版权声明
本文为[Amyniez]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230610096579.html
边栏推荐
- BottomSheetDialogFragment + ViewPager+Fragment+RecyclerView 滑动问题
- Project, how to package
- adb shell常用模拟按键keycode
- [exynos4412] [itop4412] [android-k] add product options
- Itop4412 cannot display boot animation (4.0.3_r1)
- [dynamic programming] Yang Hui triangle
- 三种实现ImageView以自身中心为原点旋转的方法
- Fill the network gap
- 三子棋小游戏
- 给女朋友写个微信双开小工具
猜你喜欢
随机推荐
MySQL notes 5_ Operation data
5种方法获取Torch网络模型参数量计算量等信息
ThreadLocal, just look at me!
Kotlin征途之data class [数据类]
torch. mm() torch. sparse. mm() torch. bmm() torch. Mul () torch The difference between matmul()
红外传感器控制开关
BottomSheetDialogFragment 与 ListView RecyclerView ScrollView 滑动冲突问题
电脑关机程序
Data class of kotlin journey
Personal blog website construction
【2021年新书推荐】Red Hat Certified Engineer (RHCE) Study Guide
三种实现ImageView以自身中心为原点旋转的方法
Component learning (2) arouter principle learning
MySQL笔记3_约束_主键约束
adb shell常用模拟按键keycode
Bottomsheetdialogfragment conflicts with listview recyclerview Scrollview sliding
【2021年新书推荐】Artificial Intelligence for IoT Cookbook
PyTorch中的一些常见数据类型转换方法,与list和np.ndarray的转换方法
三子棋小游戏
ThreadLocal,看我就够了!