Represent an integer by the sum of at least two consecutive integers

来源:互联网 发布:com.cn域名值钱吗 编辑:程序博客网 时间:2024/06/03 13:39

There are some integers which can be represented by the sum of at least two consecutive integers. For example, 3 = 1 + 2, 9 = 4 + 5 = 2 + 3 + 4, 15 = 1 + 2 + 3 + 4 + 5 = 4 + 5 + 6 = 7 + 8, 18 = 5 + 6 + 7 = 3 + 4 + 5 + 6, 20 = 2 + 3 + 4 + 5 + 6, and so on. However, there are also some integers which do not have such forms of representation. For example, 2, 8, 64, and so on. The following program can test whether an integer input has such form or not. If it does, find all such forms and print them out; If it doesn't, return null and just print out the message to show no such form found.

  1. #include <iostream>
  2. #include <vector>
  3. #include <math.h>
  4. using namespace std;
  5. inline bool is_odd(int number) {
  6.     return number % 2 != 0;
  7. }
  8. // Return all the consecutive sum representations of a positive integer.
  9. vector<vector<int>* >* consecutive_sum(int number) {
  10.     if(number < 3) {
  11.         return NULL;
  12.     }
  13.     vector<vector<int>* >* con_sum = new vector<vector<int>* >;
  14.     vector<int>* one_con;
  15.     int bound = ((int)sqrt(2*number)) + 1; // The upper bound of the count of the sequence.
  16.     for(int count = 2; count < bound; ++count) {
  17.         if(is_odd(count)) { // The count of the sequence is odd.
  18.             if(number%count == 0 && number/count > count/2) { // Has a legal sequence, and store it.
  19.                 int start = number/count - count/2;
  20.                 one_con = new vector<int>;
  21.                 for(int i = 0; i < count; ++i, ++start) {
  22.                     one_con->push_back(start);
  23.                 }
  24.                 con_sum->push_back(one_con);
  25.             }
  26.         }
  27.         else { // The count of the sequence is even.
  28.             if(number%count == count/2 && number/count >= count/2) { // Has a legal sequence, and store it.
  29.                 int start = number/count - (count/2 - 1);
  30.                 one_con = new vector<int>;
  31.                 for(int i = 0; i < count; ++i, ++start) {
  32.                     one_con->push_back(start);
  33.                 }
  34.                 con_sum->push_back(one_con);
  35.             }
  36.         }
  37.     }
  38.     
  39.     return con_sum;
  40. }
  41. // Print all the forms of representation of an integer.
  42. void print_consecutive_sum(vector<vector<int>* >* con_sum) {
  43.     if(con_sum == NULL || con_sum->empty()) {
  44.         cout << "No consecutive sequences of this number." << endl;
  45.         return;
  46.     }
  47.     vector<vector<int>* >::iterator con_sum_iter, con_sum_end;
  48.     vector<int>::iterator con_one_iter, con_one_end;
  49.     for(con_sum_iter = con_sum->begin(), con_sum_end = con_sum->end(); con_sum_iter != con_sum_end; ++con_sum_iter) {
  50.         for(con_one_iter = (*con_sum_iter)->begin(), con_one_end = (*con_sum_iter)->end(); con_one_iter != con_one_end; ++con_one_iter) {
  51.             cout << *con_one_iter << " ";
  52.         }
  53.         cout << endl;
  54.     }
  55. }
  56. int main() {
  57.     int n;
  58.     cout << "Start...(Exit by entering ZERO)" << endl;
  59.     cin >> n;
  60.     while(n != 0) {
  61.         print_consecutive_sum(consecutive_sum(n));
  62.         cin >> n;
  63.     }
  64.     cout << "Exit." << endl;
  65.     return 0;
  66. }

Actually the idea to solve this problem is so easy that you can find it out just through the codes and the comments.

 

Moreover, you may ask what integers do not have such form of representation? OK, if we change the main function like this, all the integers which do not have such form of representation are printed out.

  1. int main() {
  2.     int n;
  3.     cout << "Start...(Exit by entering ZERO)" << endl;
  4.     cin >> n;
  5.     while(n != 0) {
  6.         /*
  7.         print_consecutive_sum(consecutive_sum(n));
  8.         */
  9.         vector<vector<int>* >* cs;
  10.         for(int i = 1; i < n; ++i) {
  11.             cs = consecutive_sum(i);
  12.             if(cs == NULL || cs->empty()) {
  13.                 cout << i << " ";
  14.             }
  15.         }
  16.         cout << endl;
  17.         cin >> n;
  18.     }
  19.     cout << "Exit." << endl;
  20.     return 0;
  21. }

Run this modified program, and you will find such integers share one common feature: They are all some power of 2. 1 = 20, 2 = 21, 4 = 22, 8 = 23, ..., 256 = 28, and so on. But why they don't have such form of representation? What is your opinion? Any related comments are welcome. ^_^

 

Referenced to <Beauty of Programming>.

 

原创粉丝点击