EularProject 61:Cyclical figurate numbers

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");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值