当前位置:网站首页>素数求解的n种境界
素数求解的n种境界
2022-04-23 06:10:00 【Amyniez】
第一种:基本方法
//4.判断i是否为素数,素数是指只能被1和其本身所整除的数字
//查找1-100之间的素数
#include <math.h>
#include <stdio.h>
int main()
{
int i = 0;
int count = 0;
for (i = 1; i <= 100; i++)
{
//试除法
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;
}
第二种: 修改查找的方式,用开平方的方法减小查找次数
int main()
{
int i = 0;
int count = 0;
for (i = 1; i <= 100; i++)
{
//试除法
int j = 0;
for (j = 2; j <= sqrt(i); j++)//开平方
{
if (i%j == 0)
{
break;
}
}
if (j >sqrt (i))
{
count++;
printf("%d ", i);
}
}
printf("\ncount = %d\n", count);
return 0;
}
第三种境界:在奇数中寻找素数
int main()
{
int i = 0;
int count = 0;
for (i = 1; i <= 100; i+=2)//在奇数中寻找素数
{
//试除法
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://blog.csdn.net/amyniez/article/details/122723823
边栏推荐
猜你喜欢

红外传感器控制开关

winform滚动条美化

Itop4412 LCD backlight drive (PWM)

【2021年新书推荐】Practical Node-RED Programming

Itop4412 HDMI display (4.4.4_r1)

WebView displays a blank due to a certificate problem

微信小程序 使用wxml2canvas插件生成图片部分问题记录

Cause: dx.jar is missing

Bottomsheetdialogfragment conflicts with listview recyclerview Scrollview sliding

ViewPager2实现画廊效果执行notifyDataSetChanged后PageTransformer显示异常 界面变形问题
随机推荐
oracle对表字段的修改
MySQL笔记3_约束_主键约束
Antd Design Form表单检验
Bottomsheetdialogfragment conflicts with listview recyclerview Scrollview sliding
oracle清除sql的缓存
数据库的事务
ARGB透明度换算
“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated
从0开始封装一套项目的网络请求框架
MySQL5. 7 insert Chinese data and report an error: ` incorrect string value: '\ xb8 \ XDF \ AE \ xf9 \ X80 at row 1`
Itop4412 surfaceflinger (4.4.4_r1)
oracle表的约束详解
[recommendation for new books in 2021] professional azure SQL managed database administration
Abnormal record-22
Cancel remote dependency and use local dependency
Android面试计网面经大全【持续更新中。。。】
接口幂等性问题
组件化学习(3)ARouter中的Path和Group注解
this. getOptions is not a function
常见的正则表达式