EularProject 61:Cyclical figurate numbers

来源:互联网 发布:淘宝威客平台 编辑:程序博客网 时间:2024/05/25 16:37

Andrew Zhang
Sep 4, 2017

Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygonal) numbers and are generated by the following formulae:

Triangle P3,n=n(n+1)/2 1, 3, 6, 10, 15, …
Square P4,n=n2 1, 4, 9, 16, 25, …
Pentagonal P5,n=n(3n−1)/2 1, 5, 12, 22, 35, …
Hexagonal P6,n=n(2n−1) 1, 6, 15, 28, 45, …
Heptagonal P7,n=n(5n−3)/2 1, 7, 18, 34, 55, …
Octagonal P8,n=n(3n−2) 1, 8, 21, 40, 65, …
The ordered set of three 4-digit numbers: 8128, 2882, 8281, has three interesting properties.

The set is cyclic, in that the last two digits of each number is the first two digits of the next number (including the last number with the first).
Each polygonal type: triangle (P3,127=8128), square (P4,91=8281), and pentagonal (P5,44=2882), is represented by a different number in the set.
This is the only set of 4-digit numbers with this property.
Find the sum of the only ordered set of six cyclic 4-digit numbers for which each polygonal type: triangle, square, pentagonal, hexagonal, heptagonal, and octagonal, is represented by a different number in the set.

code:

#include <vector>#include <iostream>using namespace std;vector<vector<bool>> state(6, vector<bool>(10000));vector<bool> visited(6);bool func(int prefix, int count, int &start){    int i, j;    for(i = 0; i < 6; i++) {        if(visited[i] == false) {            for(j = prefix * 100 + 10; j < prefix * 100 + 100; j++) {                if(state[i][j]) {                    if(count == 2) {                        if(j % 100 == start) {                            cout<< j <<endl;                            return true;                        } else {                            return false;                        }                    } else {                        visited[i] = true;                        if(func(j % 100, count + 1, start)) {                            cout << j << endl;                            return true;                        }                        visited[i] = false;                    }                }            }        }    }    return false;}int main(){    int i, j;    int origin;    i = 1;    while ((j = i * (i + 1) / 2) && (j < 10000)) {        if(j > 999)            state[0][j] = true;        i++;    }    i = 1;    while ((j = i * i) && (j < 10000)) {        if(j > 999)            state[1][j] = true;        i++;    }    i = 1;    while ((j = i * (3 * i - 1) / 2) && (j < 10000)) {        if(j > 999)            state[2][j] = true;        i++;    }    i = 1;    while ((j = i * (2 * i - 1)) && (j < 10000)) {        if(j > 999)            state[3][j] = true;        i++;    }    i = 1;    while ((j = i * (5 * i - 3) / 2) && (j < 10000)) {        if(j > 999)            state[4][j] = true;        i++;    }    i = 1;    while ((j = i * (3 * i - 2)) && (j < 10000)) {        if(j > 999)            state[5][j] = true;        i++;    }    for(j = 1000; j < 10000; j++) {        int prefix = j % 100;        if(prefix > 9) {            for(i = 0; i < 6; i++) {                if(state[i][j]) {                    visited[i] = true;                    origin = j / 100;                    if(func(j % 100, 1, origin)) {                        cout << j << endl;                        cout<<"==============="<<endl;                    }                    visited[i] = false;                }            }        }    }    cout<<"search finished"<<endl;    system("pause");}
原创粉丝点击