(Problem 41)Pandigital prime
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.
What is the largest n-digit pandigital prime that exists?
1 #include<stdio.h> 2 #include<math.h> 3 #include<string.h> 4 #include<ctype.h> 5 #include<stdlib.h> 6 #include<stdbool.h> 7 8 bool isprim(int n) 9 { 10 int i=2; 11 if(n==1) return false; 12 for(; i*i<=n; i++) 13 { 14 if(n%i==0) return false; 15 } 16 return true; 17 } 18 19 bool pandigital(int n) 20 { 21 char s[10],d[10]={0}; 22 int i=0; 23 sprintf(s,"%d",n); 24 int len=strlen(s); 25 while(i<len) 26 { 27 switch(s[i]-'0') 28 { 29 case 1: d[1]++;break; 30 case 2: d[2]++;break; 31 case 3: d[3]++;break; 32 case 4: d[4]++;break; 33 case 5: d[5]++;break; 34 case 6: d[6]++;break; 35 case 7: d[7]++;break; 36 case 8: d[8]++;break; 37 case 9: d[9]++;break; 38 default: break; 39 } 40 i++; 41 } 42 for(i=1; i<=len; i++) 43 { 44 if(d[i]!=1) return false; 45 } 46 if(!isprim(n)) return false; 47 else return true; 48 } 49 50 int main() 51 { 52 int i=10000001; 53 while(i>1000) 54 { 55 if(pandigital(i)) 56 { 57 printf("%d\n",i); 58 break; 59 } 60 i=i-2; 61 } 62 return 0; 63 }
Answer:
|
7652413 |
作者:cpoint
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.