zoukankan      html  css  js  c++  java
  • (Problem 35)Circular primes

    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?

    #include<stdio.h>
    #include<math.h>
    #include<string.h>
    #include<ctype.h>
    #include<stdlib.h>
    #include<stdbool.h>
    
    bool isprim(int n)
    {
    	int i=2;
    	for(; i*i<n; i++)
    	{
    		if(n%i==0)  return false;
    	}
    	return true;
    }
    
    bool circular_prime(int n)
    {
    	int i,j,flag=1;
    	char s[6];
    	int sum=0;
    	sprintf(s,"%d",n);
    	int len=strlen(s);
    	for(i=0; i<len; i++)
    	{
    		if(s[i]!='1' && s[i]!='3' && s[i]!='7' && s[i]!='9')
    			return false;
    	}
    	for(i=0; i<len; i++)
    	{
    		for(j=i; j<i+len-1; j++)
    		{
    			sum+=s[j%len]-'0';
    			sum*=10;
    		}
    		sum+=s[j%len]-'0';
    		if(!isprim(sum)) return false;
    		sum=0;
    	}
    	return true;
    }
    
    
    int main()
    {
    	int sum=4;    //已包含2,3,5,7
    	for(int i=11; i<1000000; i++)
    	{
    		if(circular_prime(i))   
    			sum++;
    	}
    	printf("%d\n",sum);
    	return 0;
    }
    

    Answer:
    55

  • 相关阅读:
    Linux下oracle数据库操作
    springcloud学习一
    nginx反向代理实现前后端分离
    GIT版本控制(码云)
    VUE环境搭建
    线程池的使用
    neo4j图形数据库实战
    idea 下tomcat字符集问题
    webservice客户端生成方式
    Spring声明式事务不回滚的问题
  • 原文地址:https://www.cnblogs.com/cpoint/p/3367346.html
Copyright © 2011-2022 走看看