题目61:找出唯一具有循环性质的6个四位数定形数集合的元素和
Cyclical figurate numbersTriangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygonal) numbers and are generated by the following formulae:
The ordered set of three 4-digit numbers: 8128, 2882, 8281, has three interesting properties.
1. 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).
2. Each polygonal type: triangle , square , and pentagonal , is represented by a different number in the set.
3. 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.
题目:
三角形数,四角形数,五角形数,六角形数,七角形数和八角形数都是定形数,他们分别由以下公式产生:
三个四位数形成的有序集合: 8128, 2882, 8281,有三个有趣的性质:
1. 这个集合是循环的:每个数的后两位数是下一个数的前两位数,包括第三个和第一个的关系。
2. 三种定形数中的每一种都能被这三个数中的一个不同的数代表:三角形数 , 四角形数 , 和五角形数 。
3. 这是唯一具有以上性质的四位数的集合。
找出唯一的一个六个四位数的循环集合,使得从三角形数到八角形数中的每一种都能由该集合中的一个不同的数代表。
求这个集合中元素之和。
Mark (, 28684)
Time:0.016s
import sys
import time
st = time.time()
def calc_numbers(start, end, proc):
n = 1
out = []
while True:
z = proc(n)
if z >= start and z < end: out.append(z)
if z >= end: return out
n += 1
p3 = calc_numbers(1000, 10000, lambda n : n * (n + 1) / 2)
p4 = calc_numbers(1000, 10000, lambda n : n * n)
p5 = calc_numbers(1000, 10000, lambda n : n * (3 * n - 1) / 2)
p6 = calc_numbers(1000, 10000, lambda n : n * (2 * n - 1))
p7 = calc_numbers(1000, 10000, lambda n : n * (5 * n - 3) / 2)
p8 = calc_numbers(1000, 10000, lambda n : n * (3 * n - 2))
def find_loop(lists, start, end, found_numbers = []):
if len(lists) == 1 and start * 100 + end in lists:
found_numbers.append(start * 100 + end)
found_numbers.append(n)
print (found_numbers, sum(found_numbers))
ed = time.time()
print ('Time:%.3fs'%(ed-st))
sys.exit()
for cur_list in lists:
for c in cur_list:
if c / 100 == start:
lists_copy = lists
lists_copy.remove(cur_list)
find_loop(lists_copy, c % 100, end, found_numbers + )
for n in p8:
find_loop(, n % 100, n / 100)
本帖最后由 飘飞的白杨 于 2017-2-24 00:17 编辑
import sys
from time import time
ttt = time()
def getlist(lam):
'''获取x角形数的列表'''
l = []
for i in range(1, 1000):
n = lam(i)
if n < 1000: continue
if n >= 10000: return l
l.append(n)
def progrem61_2(numbers):
if len(numbers) == 6 and numbers[-1] % 100 == numbers // 100:
print(numbers, sum(numbers), "\ntime:%.5fs"%(time()-ttt))
sys.exit()
for i in range(6):
if flag: continue
flag = True
for j in p:
if j // 100 == numbers[-1] % 100:
progrem61_2(numbers+)
flag = False
def progrem61_1():
flag[-1] = True
# 八角形数最少,从八角形数中选择第一个数
for i in p8:
progrem61_2()
p3 = getlist(lambda n: n * (n + 1) // 2)
p4 = getlist(lambda n: n * n)
p5 = getlist(lambda n: n * (3 * n - 1) // 2)
p6 = getlist(lambda n: n * (2 * n - 1))
p7 = getlist(lambda n: n * (5 * n - 3) // 2)
p8 = getlist(lambda n: n * (3 * n - 2))
p =
flag = * 6 # 标志数组,用于判断是否遍历过
progrem61_1() 本帖最后由 guosl 于 2022-1-10 12:44 编辑
/*
答案:28684
耗时:0.0000797秒
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <omp.h>
using namespace std;
vector<int> v;//v,...,v分别是三角数,...,八角数
int x;//记录查找的结果
int p = { 0,1,2,3,4,5 };//改变表的查找次序用
void GenTriangle(void)//生成三角数表
{
int k = 1;
while (true)
{
int n = k * (k + 1) / 2;
if (n > 9999)
break;
else if (n >= 1000)
v.push_back(n);
++k;
}
}
void GenSquare(void)//生成四角形数表
{
int k = 1;
while (true)
{
int n = k * k;
if (n > 9999)
break;
else if (n >= 1000)
v.push_back(n);
++k;
}
}
void GenPentagonal(void)//生成五角形数表
{
int k = 1;
while (true)
{
int n = k * (3 * k - 1) / 2;
if (n > 9999)
break;
else if (n >= 1000)
v.push_back(n);
++k;
}
}
void GenHexagonal(void)//生成六角形数表
{
int k = 1;
while (true)
{
int n = k * (2 * k - 1);
if (n > 9999)
break;
else if (n >= 1000)
v.push_back(n);
++k;
}
}
void GenHeptagonal(void)//生成七角形数表
{
int k = 1;
while (true)
{
int n = k * (5 * k - 3) / 2;
if (n > 9999)
break;
else if (n >= 1000)
v.push_back(n);
++k;
}
}
void GenOctagonal(void)//生成八角形数表
{
int k = 1;
while (true)
{
int n = k * (3 * k - 2);
if (n > 9999)
break;
else if (n >= 1000)
v.push_back(n);
++k;
}
}
bool dfs(int k, int f, int b)//深度搜索找匹配链
{
if (k == 6)
return (f == b);//检查是否可以和第一个数匹配
int a1 = b * 100, a2 = a1 + 99;
//找出下一个表中可能匹配的范围
auto itr1 = lower_bound(v].begin(), v].end(), a1);
auto itr2 = upper_bound(v].begin(), v].end(), a2);
for (auto itr = itr1; itr != itr2; ++itr)//具体查找
{
//找到可能的匹配
x = *itr;//记录查找的结果
if (dfs(k + 1, f, x % 100))//往下找
return true;
}
return false;
}
int main(void)
{
double t = omp_get_wtime();
//生成各种数的表
GenTriangle();
GenSquare();
GenPentagonal();
GenHexagonal();
GenHeptagonal();
GenOctagonal();
do
{
bool bFind = false;
for (int i = 0; i < (int)v].size(); ++i)
{
int a0 = v];
int b = a0 % 100;//后二位
int f = a0 / 100;//前二位
x = v];//记录查找的结果
bFind = dfs(1, f, b);
if (bFind)
break;
}
if (bFind)
break;
} while (next_permutation(p, p + 6));//改变一下表的查找次序
int nSum = 0;
for (int i = 0; i < 6; ++i)
nSum += x;
t = omp_get_wtime() - t;
cout << nSum << endl << t << endl;
return 0;
} 本帖最后由 B1tetheDust 于 2022-10-29 15:48 编辑
import time as t
start = t.perf_counter()
def divide_4_digit_num(a_num_list):
new_list = []
for each_num in a_num_list:
new_list.append(, str(each_num)])
return new_list
def belong_to(a_num):
range_list =
for belong_i in range(6):
if a_num <= range_list:
return belong_i
def find_cyclic_num():
Tri = # length = 96
Squ = # length = 68
Penta = # length = 56
Hexa = # length = 48
Hept = # length = 43
Octa = # length = 40
Tri_list, Squ_list = divide_4_digit_num(Tri), divide_4_digit_num(Squ)
Penta_list, Hexa_list = divide_4_digit_num(Penta), divide_4_digit_num(Hexa)
Hept_list, Octa_list = divide_4_digit_num(Hept), divide_4_digit_num(Octa)
dict_list = {"Tri_list": Tri_list, "Squ_list": Squ_list, "Penta_list": Penta_list, "Hexa_list": Hexa_list,
"Hept_list": Hept_list, "Octa_list": Octa_list}
total = []
for lists in dict_list:
total += dict_list
len_total = len(total)
for i00 in range(len_total):
index_list =
for i10 in range(len_total):
if belong_to(i10) not in index_list and total == total:
index_list =
for i20 in range(len_total):
if belong_to(i20) not in index_list and total == total:
index_list =
for i30 in range(len_total):
if belong_to(i30) not in index_list and total == total:
index_list =
for i40 in range(len_total):
if belong_to(i40) not in index_list and total == total:
cur_num_list = , total, total, total, total]
index_list = [belong_to(i00), belong_to(i10), belong_to(i20), belong_to(i30),
belong_to(i40)]
for i50 in range(len_total):
if belong_to(i50) not in index_list and \
total == total and \
total == cur_num_list:
cur_num_list.append(total)
return cur_num_list
res = find_cyclic_num()
print(res)
print(sum( + i) for i in res]))
print("It costs %f s" % (t.perf_counter() - start))
[['25', '12'], ['12', '81'], ['81', '28'], ['28', '82'], ['82', '56'], ['56', '25']]
28684
It costs 0.530015 s
页:
[1]