简体   繁体   English

如何编写一个函数来打印出c中素数的平均值

[英]how to write a function that prints out mean value of prime numbers in c

I'm trying to solve a problem, where I am supposed to write a function that prints out the mean value of all prime numbers between 2 and N but I don't know how to start.我正在尝试解决一个问题,我应该编写一个函数来打印出 2 和 N 之间所有素数的平均值,但我不知道如何开始。 I have already written a function that prints out all the prime numbers.我已经写了一个函数来打印出所有的质数。

  1. sum the prime numbers as you find them instead of printing对找到的素数求和而不是打印
  2. also count the number of primes you found还要计算你找到的素数的数量
  3. then divide the sum by number of primes然后将总和除以素数的数量

for example, assuming you have written the function for prime :例如,假设您已经为 prime 编写了函数:

 count=0;
 for(i=2;i<n;i++) {
        if(isPrime(i)) {
            sum+=i;
            count++;
        }
    }
 mean = sum/count; // watch for integer division

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM