39: The nth small prime number

Source: Internet
Author: User

39: The nth small prime number
Total time limit: 1000ms memory limit: 65536kB
Describe
Enter a positive integer n to find the small prime number of nth.
Input
A positive integer of not more than 10000 n.
Output
The nth small prime number.
Sample input
10
Sample output
29

Method One: The Honest enumeration count finds to the nth prime number and then outputs.

1#include <stdio.h>2#include <math.h>3 intMainintargcChar*argv[])4 {5     LongI,n,count;6     Longj,flag,t;7scanf"%d", &n);//need to find nth prime number8Count=0;//already looking up to count a9     TenI=2; One     //I gradually increased from 2 onwards. Determine if the value of I is a prime number and count A      while(count<N) -     { -         //Judging whether I prime thej=2; flag=0; t=sqrt (i); -          while(j<=t) -         { -             if(i%j==0) +             { -flag=1; +                  Break; A             } atJ + +; -         } -         //If I is a prime number, Count counter plus 1 -         if(flag==0) count++; -i++;//ready to judge the next number -     } inprintf"%d\n", I-1); -     return 0; to}

Method Two: Offline calculation, first print out 10000 prime numbers into the array, and then ... Oh

Modify the above code slightly to generate prime numbers and output them to the file:

1#include <stdio.h>2#include <math.h>3 intMainintargcChar*argv[])4 {5     LongI,n,count;6     Longj,flag,t;7Freopen ("39.out","W", stdout);8     //scanf ("%d", &n); //need to find nth prime number9n=10003;TenCount=0;//already looking up to count a One      AI=2; -     //I gradually increased from 2 onwards. Determine if the value of I is a prime number and count -      while(count<N) the     { -         //Judging whether I prime -j=2; flag=0; t=sqrt (i); -          while(j<=t) +         { -             if(i%j==0) +             { Aflag=1; at                  Break; -             } -J + +; -         } -         //If I is a prime number, Count counter plus 1 -         if(flag==0) in         { -count++; toprintf"%d,", i); +         } -i++;//ready to judge the next number the     } *     //printf ("%d\n", i-1); $     return 0;Panax Notoginseng}
View Code

Then copy the data from the file into the code and construct the array. Hey, whoa.

1#include <stdio.h>2 intMainintargcChar*argv[])3 {4     Longa[10003]=5 {//In order to open the page faster, do not finish the data.6         2,3,5,7, One, -, -, +, at, in, to,......7     };8     intN;9scanf"%d",&n);Tenprintf"%d\n", a[n-1]); One     return 0; A}

Yes, opportunistic.

39: The nth small prime number

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.