Search Here

Saturday, July 4, 2015

UVa - 967 ( Circular Solution ) [ C & C++ Code ]

=>Try Yourself First. For help Scroll Down .

Clearance
Let a Number 99371 which is a Prime . Move the first digit to the last , the number will be 93719. Move again the first digit of Previously made Number , now the number is 37199..... Then do the same thing until last digit comes . The total formations are :
99371 - > 93719 - > 37199 - > 71993 - > 19937 . If all the numbers will be Prime then it will be a Circular Prime .
Algorithm :
1. Firstly Pre-generate all the Circular Prime numbers in a list . And then run a loop for all the Prime Numbers and Find which number of the Circular Prime List is in between of the given Range . Count those and Print it.
2. Careful about Output Formats ."  Primes & Prime ".

!! Warning : Don't Copy the Circular Prime list from my Code . Generate it Yourself .  You will learn many more facts from that . !!

Code :



#include<bits/stdc++.h>
#define MAX 1000000

using namespace std;

int cplist[]={113,131,197,199,311,337,373,719,733,919,971,991,1193,1931,3119,3779,7793,7937,9311,9377,11939,19391,19937,37199,39119,71993,91193,93719,93911,99371,193939,199933,319993,331999,391939,393919,919393,933199,939193,939391,993319,999331};

int main()
{
    int a, b;
    while(scanf("%d",&a)==1)
    {
        if(a==-1)  break;
        scanf("%d",&b);
        int cnt=0;
        for(int i=0; i<42; i++)
        {
            if(cplist[i]>b) break;
            if(cplist[i]>=a && cplist[i]<=b)
                cnt++;
        }

        if(cnt==0) printf("No Circular Primes.\n");
        else if(cnt==1) printf("1 Circular Prime.\n");
        else printf("%d Circular Primes.\n",cnt);
    }
    return 0;
}



=>Question or Need Help ?? Leave a Comment .

No comments:

Post a Comment