Alexandra and Prime Numbers (thinking)

Source: Internet
Author: User

Alexandra and Prime Numbers

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) total submission (s): 1658 Accepted Submission (s): 565

Problem Descriptionalexandra has a little brother. He is new to programming. One day he's solving the following problem:given an positive integer n, and judge whether N is prime. The problem above is quite easy, so Alexandra gave him a new task:given a positive integer N, find the minimal positive I Nteger M, such that n/m is prime. If such M doesn ' t exist, output 0. Help him!

Inputthere is multiple test cases (no more than 1,000). Each case contains only one positive integer N.N≤1 , A.. Number of cases with N>1,than, are No more than 100.

Outputfor each case, output the requested M, or output 0 if no solution exists.

Sample Input3456

Sample Output1212

The puzzle: Let the smallest positive integer m make n/m a prime number;

Water violence, but complete violence will be super, think of binary find, first find this smallest positive integer, if not to find prime; all find sqrt (N) end, so save a lot of time;

Code:

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;BOOLIs_prim (intx) {    if(x = =1)return false;  for(inti =2; I <= sqrt (x); i++){        if(x% i = =0)return false; }    return true;}intMain () {intN;  while(~SCANF ("%d",&N)) {        if(N = =0|| N = =1) {puts ("0");Continue; }        inti; intAns =0;  for(i =1; I <= sqrt (N); i++){            if(N% i = =0&& Is_prim (n/i)) {ans=i;  Break; }        }        if(ans) {printf ("%d\n", ans);Continue; }             for(intj = sqrt (N); J >1; j--){                if(N% J = =0&&Is_prim (j)) {ans= N/J;  Break; }} printf ("%d\n", ans); }    return 0;}

Alexandra and Prime Numbers (thinking)

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.