欧拉计划 第三十五题

The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.

There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

How many circular primes are there below one million?

这个数字197被称为循环素数,因为数字的所有旋转:197,971和719本身都是素数。

在100:2,3,5,7,11,13,17,31,37,71,73,79和97之下有十三个这样的素数。

一百万以下有多少个圆形素数?

思路

1.本题根素数有关,先将线性筛写出来;

2.循环右移公式:(n / 10) + (n % 10) * dh; 798: (798 / 10) + (798 % 10) * 100 = 879;dh = pow(10,floor(log10(x)))

3.主函数中判断是否为循环素数,count++;

#include <stdio.h>
#include <math.h>
#include <inttypes.h>

#define max 1000000

int prime[max + 5] = {0};//存放线性筛结果;
int isprime[max + 5] = {0};//素数为0,合数为1,circle函数容易进行判断;

void Prime(){
	for(int i = 2; i <= max; i++){
		if(!prime[i]) prime[++prime[0]] = i;
		for(int j = 1; j <= prime[0]; j++){
			if(i * prime[j] > max) break;
			prime[i * prime[j]] = 1;
			isprime[i * prime[j]] = 1;//合数置1;
			if(i % prime[j] == 0) break;
		}
	}
}

int circle(int x){
	if(isprime[x] != 0) return 0;//如果本数本来就不是素数,就不用进行循环判断了;
	int dh = pow(10,floor(log10(x)));
	for(int i = 0; i < floor(log10(x)) + 1; i++){
		x = x / 10 + (x % 10) * dh;
		if(isprime[x] != 0) return 0;
	}
	return 1;
} 

int main(){
	Prime();
	int count = 0;
	for(int i = 1; i <= prime[0]; i++){
		if(prime[i] > max) break;
		if(circle(prime[i])) count++;//prime[i]中存放第i个素数;
	}
	printf("%d\n",count);

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值