[Projecte1_] projecteuler_037

Source: Internet
Author: User
// Problem 37 // 14 February 2003 // The number 3797 has an interesting property. being Prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797,797, 97, and 7. similarly we can work from right to left: 3797,379, 37, and 3. /// find the sum of the only eleven primes that are both truncatable from left to right and right to left. //// note: 2, 3, 5, and 7 are not considered to be truncatable primes. # include <iostream> # include <windows. h> # include <cmath> # include <ctime> using namespace STD; // determines whether a number is a prime number. bool isprimenum (INT num) {If (Num % 2 = 0 & num> 2) | num <= 1) {return false;} int sqrtnum = (INT) SQRT (double) num); For (INT I = 3; I <= sqrtnum; I + = 2) {If (Num % I = 0) {return false ;}} return true ;} // determine whether it is a truncatable prime boo L checktruncatablenum (const int num) {int currentnum = num; // remove the number from the right to the left. The original number has been determined here, And the while (currentnum! = 0) {If (! Isprimenum (currentnum) {return false;} currentnum/= 10;} // remove the number int tendigit = 10 from left to right; currentnum = num % tendigit; while (currentnum! = Num) {If (! Isprimenum (currentnum) {return false;} tendigit * = 10; currentnum = num % tendigit;} return true;} void F1 () {cout <"Void F1 () "<Endl; large_integer timestart, timeend, freq; queryperformancefrequency (& freq); queryperformancecounter (& timestart); const int min_num = 11; // starting from 11, because the question requires exclusion of, const int max_count = 11; // a total of 11 int sum = 0; // The total number of records int COUNT = 0; // total records for (INT I = min_num; count <max_count; I + = 2) {If (checktruncatablenum (I) {cout <I <Endl; count ++; sum + = I ;}} cout <"Total:" <sum <Endl; queryperformancecounter (& timeend ); cout <"Total milliseconds is" <(double) (timeend. quadpart-timestart. quadpart) * 1000/freq. quadpart <Endl; time_t currenttime = Time (null); char timestr [30]; ctime_s (timestr, 30, effecttime ); cout <Endl <"by godmoon" <Endl <timestr;} // main function int main () {F1 (); Return 0 ;} /* void F1 () 2337537331331737379731373797739397 total is 748317 total milliseconds is 453.591by godmoonsat Nov 05 14:09:20 2011 */

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.