导航菜单
首页 >  » 正文

C语言编程求素数的个数,计算1到1000000000(10亿)以内的素数个数,有多少个?附上程序 c语言用函数从键盘输入10个整数,统计非负数个数,负数个数,并计算非负数之和,负数之和,非负数之积

C语言编程求素数的个数,计算1到1000000000(10亿)以内的素数个数,有多少个?附上程序

不知道有没有国际最优,但我这个算法很顶尖了:计算1亿以内的素数个数不到2秒钟!

1到10000000000(10亿)共有素数50847534个,计算时间大概20多秒!

程序如下:

#include<iostream> using namespace std; int main() {int CompositeNumFilterV3(int);  int m,c;  cin>>m;  c=CompositeNumFilterV3(m);  cout<<c<<endl; return 0; }

//求素数的程序 int CompositeNumFilterV3(int n) {  int i, j;  //素数数量统计  int count = 0;  // 分配素数标记空间,明白+1原因了吧,因为浪费了一个flag[0]  char* flag = (char*)malloc( n+1 );  // 干嘛用的,请仔细研究下文  int mpLen = 2*3*5*7*11*13;  char magicPattern[2*3*5*7*11*13]; // 奇怪的代码,why,思考无法代劳,想!   for (i=0; i<mpLen; i++)  {   magicPattern[i++] = 1;    magicPattern[i++] = 0;    magicPattern[i++] = 0;    magicPattern[i++] = 0;    magicPattern[i++] = 1;    magicPattern[i] = 0;  }  for (i=4; i<=mpLen; i+=5)    magicPattern[i] = 0;  for (i=6; i<=mpLen; i+=7)    magicPattern[i] = 0;  for (i=10; i<=mpLen; i+=11)    magicPattern[i] = 0;  for (i=12; i<=mpLen; i+=13)    magicPattern[i] = 0;

  // 新的初始化方法,将2,3,5,7,11,13的倍数全干掉   // 而且采用memcpy以mpLen长的magicPattern来批量处理   int remainder = n%mpLen;  char* p = flag+1;  char* pstop = p+n-remainder;  while (p < pstop)  {    memcpy(p, magicPattern, mpLen);    p += mpLen;  }   if (remainder > 0)   {     memcpy(p, magicPattern, remainder);   }   flag[2] = 1;   flag[3] = 1;   flag[5] = 1;   flag[7] = 1;   flag[11] = 1;   flag[13] = 1; 

  // 从17开始filter,因为2,3,5,7,11,13的倍数早被kill了    // 到n/13止的,哈哈,少了好多吧   int stop = n/13;   for (i=17; i <= stop; i++)   {     // i是合数,请歇着吧,因为您的工作早有您的质因子代劳了     if (0 == flag[i]) continue;      // 从i的17倍开始过滤    int step = i*2;    for (j=i*17; j <= n; j+=step)    {     flag[j] = 0;    }   }      // 统计素数个数   for (i=2; i<=n; i++)   {     if (flag[i]) count++;   }       // 因输出费时,且和算法核心相关不大,故略     // 释放内存,别忘了传说中的内存泄漏    free(flag);      return count; }

c语言用函数从键盘输入10个整数,统计非负数个数,负数个数,并计算非负数之和,负数之和,非负数之积

// c语言,用函数实现从键盘输入10个整数,统计非负数个数,负数个数,并计算非负数之和,负数之和,非负数之积
void func() {
int i = 0;
int arr[10] = { 0 };
int count_a = 0; // 非负整数数量
int count_b = 0; // 负数数量
int sum_a = 0; // 非负整数之和
int sum_b = 0; // 负数之和
long long pro = 1; // 非负数之积
// 读取 10 个整数, 并计算
for (; i < 10; i++) {
scanf("%d", &arr[i]);
if (arr[i] < 0) { // 负数
count_b++;
sum_b += arr[i];
} else { // 非负数
count_a++;
sum_a += arr[i];
pro *= arr[i];
}
}
printf("非负数个数: %d. ", count_a);
printf("负数个数: %d. ", count_b);
printf("非负整数之和: %d. ", sum_a);
printf("负数之和: %d. ", sum_b);
printf("非负数之积: %d. ", pro);
}

education does count 课文翻译

Gates: Education does count 盖茨:谁说教育不重要? Hundreds of students send me e-mail each year asking for advice about education. They want to know what to study, or whether it’s OK to drop out of college1 since that’s what I did. A smaller number of parents send message, often poignant2, seeking guidance for their son or daughter. “How can we steer out child toward success3” they ask. My basic advice is simple and heartfelt. “Get the best education you can. Take advantage of high school and college. Learn how to learn.” It’s true that I dropped out of college to start Microsoft, but I was at Harvard for three years before dropping out — and I’d love to have the time to go back. As I’ve said before, nobody should drop out of college unless they believe they face the opportunity of a lifetime. And even then they should reconsider. The computer industry has lots of people who didn’t finish college, but I’m not aware of any success stories that began with somebody dropping out of high school. I actually don’t know any high school dropouts, let alone any successful ones. In my company’s early years, we had a bright part-time programmer who threatened to drop out of high school to work full-time. We told him no. Quite a few of out people didn’t finish college, but we discourage dropping out.  Having a diploma certainly helps somebody who is looking to us for a job. College isn’t the only place where information exists. You can learn in a library. But somebody handing you a book doesn’t automatically foster learning. You want to learn with other people, ask questions, try out ideas and have a way to test your ability. It usually takes more than just a book. Education should be broad, although it’s fine to have deep interests, too. In high school there were periods when I was highly focused on writing software, but for most of my high school years I had wide-ranging academic interests4. My parents encouraged this, and I’m grateful that they did.

(这个么?)

数百名学生向我发送电子邮件,每年要求的意见,关于教育.他们想知道什么研究,或有否的确定退学的大学生,因为这是我所做的. 一个较小的家长人数的发送讯息,往往痛苦,寻求指导,为他们的儿子或女儿.“我们如何能引导出儿童走向成功”他们问. 我的基本意见是,简单和衷心的.“得到最好的教育,您可以.利用高中和大学.学习如何学习“ . 它的真实,我退出了学院启动微软软件 ,但我是在哈佛大学为三年前退出-和我喜欢有时间回去.正如我已经说过,任何人都不应退学的大学生,除非他们相信他们所面对的机会,一辈子.即使如此,他们应该重新考虑. 电脑行业已经有很多人谁没有完成大学,但我不知道有任何成功的故事开始有人在高中开始逃学.其实,我不知道任何高辍学,更遑论任何成功的. 在我公司的最初几年,我们有一个光明的部分时间制程序员谁威胁要退出高中全职工作时间.我们告诉他没有. 也有不少的出人没有完成大学,但我们劝阻中途退学. 有一个文凭,当然有利于有人谁是期待着我们为一份工作. 大学不是唯一的地方的资料存在.您可以了解在一个图书馆.但有人交给你一本书,并不会自动促进学习.您想了解与其他人,问问题,尝试的想法和有办法来测试您的能力.它通常需要不止一本书. 教育应该是广泛的,虽然它的罚款,以有太深厚的利益. 在高中时有期时,我是高度集中的写作软件,但我大部分的高中多年来,我曾广泛的学术利益.我的父母鼓励这一点,我很感谢他们.

相关推荐: