Special pandigital number (translated by someone)

Source: Internet
Author: User

I encountered a problem during programming. The maximum value of strtol's return value is 2 ^ 32-1. Using its return value for summation is useless, I wrote a strtol_adv function again (the returned value is long, and the upper limit is 2 ^ 64-1 ). /* 1406357289 is from 0 ~ The number of pandigital composed of digits 9. If d1 represents 1st digits, d2 represents 2nd digits, and so on, it is found to have the following properties: d2d3d4 = 406 can be 2 divisible d3d4d5 = 063 can be 3 divisible d4d5d6 = 635 can be 5 divisible d5d6d7 = 357 can be 7 divisible d6d7d8 = 572 can be 11 divisible d7d8d9 = 728 can be 13 d8d9d10 = 289 can be divided by 17. Can this problem be caused by 0 ~ 9. What is the total number of pandigital numbers that match the nature of the above column? Note: 1406357289 is the smallest one, and there are 5 more. 140635728914309528671460357289441735728941309528674160357289sum value 16695334890 * // ceshiyongde. cpp: defines the entry point of the console application. // # Include "stdafx. h "# include <iostream> using namespace std; long strtol_adv (char * str, int len) {long num = 0; int I; for (I = 0; I <= len-1; I ++) {num = num * 10 + str [I]-'0';} return num;} bool IsPassChongFu (int * a, int sub, int shiTanZhi) // {int I; for (I = 0; I <sub; I ++) {if (a [I] = shiTanZhi) return false;} return true;} bool ispasschushuyanzheng (int * a, int sub, int shiTanZhi) // verify {int num by removing the prime number; /* 1406357289 is an interesting number. First, it ranges from 0 ~ 9 These 10 digits are composed, and each digit is used only once. We call the first digit of 1406357289 as d1, and the second digit as d2 ......, The last digit is called d10. You will find that the following rule d2d3d4 = 406 can be divisible by 2 d3d4d5 = 063 can be divisible by 3 d4d5d6 = 635 can be divisible by 5 d5d6d7 = 357 can be divisible by 7 d6d7d8 = 572 can be divisible by 11 d7d8d9 = 728 can be divisible by 13 d8d9d10 = 289 can be divisible by 17 2, 3, 5, 7, 11, 13, and 17 are consecutive prime numbers */if (sub = 10-1) // 10 {num = a [7] x 100 + a [8] * 10 + shiTanZhi; if (num % 17) = 0) {return true ;} elsereturn false;} if (sub = 9-1) // {num = a [6] * 100 + a [7] * 10 + shiTanZhi; if (num % 13) = 0) {return true;} elsereturn false;} if (sub = 8-1) // {num = a [5] * 1 00 + a [6] * 10 + shiTanZhi; if (num % 11) = 0) {return true;} elsereturn false;} if (sub = 7-1) // {num = a [4] * 100 + a [5] * 10 + shiTanZhi; if (num % 7) = 0) {return true ;} elsereturn false;} if (sub = 6-1) // {num = a [3] * 100 + a [4] * 10 + shiTanZhi; if (num % 5) = 0) {return true;} elsereturn false;} if (sub = 5-1) // {num = a [2] * 100 + a [3] * 10 + shiTanZhi; if (num % 3) = 0) {return true ;} elsereturn false;} if (sub = 4-1) // {num = a [5] * 100 + a [6] * 10 + ShiTanZhi; if (num % 2) = 0) {return true;} elsereturn false;} return false;} void HuiSuoQiu () {char str [11]; char * stop; long sum = 0, temp; int a [10], I, j, k, t; int sub, shiTanZhi; int const MaxSub = 9; int const MaxShiTanZhi = 9; str [10] = '\ 0'; a [0] = 1; sub = 1; shiTanZhi = 0; while (true) {if (sub <0) break; if (sub> MaxSub) // generate feasible solution {for (I = 0; I <MaxSub + 1; I ++) {cout <a [I]; str [I] = a [I] + '0';} cout <endl; temp = strtol_adv (str, strl En (str); // temp = strtol (str, & stop, 10); sum + = temp; /// // sub --; shiTanZhi = a [sub] + 1;} if (shiTanZhi> MaxShiTanZhi) {sub --; shiTanZhi = a [sub] + 1 ;} else {// if (a [0] = 1 & a [1] = 4 & a [2] = 0) // if (a [0] = 1 & a [1] = 4 & a [2] = 0 & a [3] = 6) // 1406357289 // t = 0; if (IsPassChongFu (a, sub, shiTanZhi) {if (sub> = 3) {if (IsPassChuSuShuYanZheng (a, sub, shiTanZhi )) {a [sub] = shiTanZhi; sub ++; shiTanZhi = 0;} elseshiTanZhi ++;} Else {a [sub] = shiTanZhi; sub ++; shiTanZhi = 0 ;}} else {shiTanZhi ++ ;}}} cout <"sum value" <sum <endl;} int _ tmain (int argc, _ TCHAR * argv []) {/* 1406357289 is an interesting number, first, it is from 0 ~ 9 These 10 digits are composed, and each digit is used only once. We call the first digit of 1406357289 as d1, and the second digit as d2 ......, The last digit is called d10. You will find that the following rule d2d3d4 = 406 can be divisible by 2 d3d4d5 = 063 can be divisible by 3 d4d5d6 = 635 can be divisible by 5 d5d6d7 = 357 can be divisible by 7 d6d7d8 = 572 can be divisible by 11 d7d8d9 = 728 can be divisible by 13 d8d9d10 = 289 can be divisible by 17 2, 3, 5, 7, 11, 13, and 17 are consecutive prime numbers. Can I use 0 ~ 9 How many numbers can these 10 numbers form, such as 1406357289? (Each number can only be used once) Please output the sum of all possible numbers! */HuiSuoQiu (); // cout <cntSuShu <endl; getchar (); return 0 ;}

Related Article

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.