Twin Prime Number

Source: Internet
Author: User

 

1 # include <iostream> 2 # include <time. h> 3 # include <math. h> 4 # include <assert. h> // use assert. the assert macro in H restricts the calling of invalid functions; 5 using namespace STD; 6 7 // determines whether it is a prime number 8 int is_prime (int x) // name the predicate (a function used to determine whether a transaction has a certain feature) in the form of is_xxx. the return value of int type is true if it is not 0, and false if it is 0; 9 {10 assert (x> = 0); // when x> = 0 fails, the program will terminate the exception and give a prompt; 11 if (x = 1) return 0; 12 int m = floor (SQRT (x) + 0.5); // rounding to avoid floating point errors, the cause of SQRT (X) is: if a number is 0 ~ SQRT (x) cannot be divisible, so it ranges from 0 ~ X cannot be divisible. 13 for (INT I = 2; I <= m; I ++) 14 {15 if (X % I = 0) // If X has other factors, it can be concluded that X is not a prime number; 16 return 0; 17} 18 return 1; 19} 20 21 int main () 22 {23 clock_t start, end; // time definition 24 int I, m; 25 CIN> m; 26 start = clock (); 27 for (I = m-2; I> = 3; I --) 28 {29 If (is_prime (I) & is_prime (I + 2) 30 {31 cout <I <''<I + 2 <Endl; 32 break; 33} 34} 35 end = clock (); 36 cout <"time used =" <(double) (end-Start)/clocks_per_sec <Endl; 37 return 0; 38 39}

 

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.