【Project Euler】【Problem 10】Summation of primes

本文介绍了一种计算200万以内所有质数之和的方法,并给出了完整的C语言实现代码。通过一个简单的质数判断函数配合循环累加,最终得出质数之和为142913828922。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Summation of primes

Problem 10

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.


Answer:
142913828922
翻译

质数的和

问题 10

10以内所有质数的和是: 2 + 3 + 5 + 7 = 17.

求2000000以内所有质数的和。


答案:
142913828922
解法一:
#include <stdio.h>
#include <math.h>

bool isPrime(long num)
{
    int _sqrt = (long)sqrt(num);

    if (num == 2 || num == 3)
    {
        return true;
    }

    for (int i = 2; i <= _sqrt; i++ )
    {
        if (num % i == 0)
        {
            return false;
        }
    }
    return true;
}


int main(int argc, char *argv[])
{
	long long result = 0;
	long maxNum = 2000000;
	int _count = 1;
	for (long i = 2; i<maxNum; i++)
	{
		if (isPrime(i))
		{
			result += i;
			_count++;
		}
	}
	printf("result is %I64d \n",result);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值