[TOJ 4705] maximum prime number, toj4705 Prime Number

Source: Internet
Author: User

[TOJ 4705] maximum prime number, toj4705 Prime Number

 

Description

Given a number of n, I asked if we could extract several consecutive bits from the order from left to right and combine them into a prime number (greater than 1, and can only be divided by 1 and itself is called a prime number), if there are multiple possibilities, take the maximum of all possible numbers.

For example, in 1234, we can take 1234,123,234, 4, and find the prime numbers are 2, 3, 23, and the maximum prime number is 23.

Input

There are multiple groups of input data. Each group occupies one row and each row has a positive integer of n (2 <= n <= 2147483647 ).

 

Output

For each output group, if yes, the maximum prime number is output; otherwise, None is output.

Sample input

1234

Sample output

23

 

Note: When the number of digits is the same, pay attention to the prime number of the maximum value !!

#include<iostream> #include<cstring>#include<cmath>#include<algorithm>#include<sstream>using namespace std;int prime(int a){    int i;    if(a==1||a==0)    return 1;    for(i=2;i<=sqrt(a);i++)    {        if(a%i==0)        return 1;    }    return 0;}int ok(int m){    int i,s=1;    for(i=1;i<=m;i++)        s=s*10;    return s;}int weishu(int a){    int s=0;    while(a!=0)    {        a=a/10;        s++;    }    return s;}int main(){    int i,j,t,n,ws,k,p,a,maxx;    while(scanf("%d",&n)!=EOF)    {        t=ws=weishu(n);        //cout<<"ws="<<ws<<endl;        k=n;        maxx=-1;        for(i=1;i<=ws;i++)        {               if(t==10)               {                   if(prime(k)==0)                   {                       maxx=k;                       cout<<k<<endl;                       break;                }                t--;            }            else            {                p=ok(t);                t--;                //cout<<"i="<<i<<" p="<<p<<endl;                k=n;                for(j=1;j<=i;j++)                {                    //cout<<"k="<<k<<endl;                    if(prime(k%p)==0)                    {                        a=k%p;                        maxx=max(a,maxx);                    }                    k=k/10;                }                if(maxx!=-1)                {                    cout<<maxx<<endl;                    break;                }            }            if(maxx!=-1)break;        }        if(maxx==-1)cout<<"None"<<endl;    }        return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.