今天的朋友圈被“世界完全对称日”刷屏了!——这个节日是指公历纪年日期中数字左右完全对称的日期。
这样的对称日,时间间隔并不固定,最近的四次分别是2010年1月2日(20100102)、2010年1月2日(20100102)、2011年11月2日(20111102)和2021年2月2日(20200202);但是再往前的一次居然要追溯到1380年8月31日,也就是明朝洪武十三年;还有之后的近几十年时间里,每十年都会有一个对称日。
为验证这些结论是否正确,我来写段代码验证一下结论;顺便也计算一下每个世纪有多少个对称日,以及对称日总数。根据定义对称日只能是8位数,所以只要在1000.1.1~9999.12.31之间寻找。代码如下:
def SymmetricalDay(year1,year2):
res = []
import datetime as dt
for i in range(year1,year2):
for j in range(1,13):
for k in range(1,32):
try: t = dt.date(i,j,k).strftime('%Y%m%d')
except: pass
if t==t[::-1]:
res.append(t)
return res
total = 0
for j in range(1000,10000,100):
count = 0
for i,d in enumerate(SymmetricalDay(j,100+j)):
count += 1
total += 1
print(d,end='\n' if i%5==4 else '\t')
if count!=0: print()
print(f'{j}-{100+j},Count:',count)
print('Total:',total)
输出结果:
10011001 10100101 10111101 10200201 10211201
10300301 10400401 10500501 10600601 10700701
10800801 10900901
1000-1100,Count: 12
11011011 11100111 11111111 11200211 11211211
11300311 11400411 11500511 11600611 11700711
11800811 11900911
1100-1200,Count: 12
12011021 12100121 12111121 12200221 12211221
12300321 12400421 12500521 12600621 12700721
12800821 12900921
1200-1300,Count: 12
13011031 13100131 13211231 13300331 13500531
13700731 13800831
1300-1400,Count: 7
1400-1500,Count: 0
1500-1600,Count: 0
1600-1700,Count: 0
1700-1800,Count: 0
1800-1900,Count: 0
1900-2000,Count: 0
20011002 20100102 20111102 20200202 20211202
20300302 20400402 20500502 20600602 20700702
20800802 20900902
2000-2100,Count: 12
21011012 21100112 21111112 21200212 21211212
21300312 21400412 21500512 21600612 21700712
21800812 21900912
2100-2200,Count: 12
22011022 22100122 22111122 22200222 22211222
22300322 22400422 22500522 22600622 22700722
22800822 22900922
2200-2300,Count: 12
2300-2400,Count: 0
2400-2500,Count: 0
2500-2600,Count: 0
2600-2700,Count: 0
2700-2800,Count: 0
2800-2900,Count: 0
2900-3000,Count: 0
30011003 30100103 30111103 30200203 30211203
30300303 30400403 30500503 30600603 30700703
30800803 30900903
3000-3100,Count: 12
31011013 31100113 31111113 31200213 31211213
31300313 31400413 31500513 31600613 31700713
31800813 31900913
3100-3200,Count: 12
32011023 32100123 32111123 32200223 32211223
32300323 32400423 32500523 32600623 32700723
32800823 32900923
3200-3300,Count: 12
3300-3400,Count: 0
3400-3500,Count: 0
3500-3600,Count: 0
3600-3700,Count: 0
3700-3800,Count: 0
3800-3900,Count: 0
3900-4000,Count: 0
40011004 40100104 40111104 40200204 40211204
40300304 40400404 40500504 40600604 40700704
40800804 40900904
4000-4100,Count: 12
41011014 41100114 41111114 41200214 41211214
41300314 41400414 41500514 41600614 41700714
41800814 41900914
4100-4200,Count: 12
42011024 42100124 42111124 42200224 42211224
42300324 42400424 42500524 42600624 42700724
42800824 42900924
4200-4300,Count: 12
4300-4400,Count: 0
4400-4500,Count: 0
4500-4600,Count: 0
4600-4700,Count: 0
4700-4800,Count: 0
4800-4900,Count: 0
4900-5000,Count: 0
50011005 50100105 50111105 50200205 50211205
50300305 50400405 50500505 50600605 50700705
50800805 50900905
5000-5100,Count: 12
51011015 51100115 51111115 51200215 51211215
51300315 51400415 51500515 51600615 51700715
51800815 51900915
5100-5200,Count: 12
52011025 52100125 52111125 52200225 52211225
52300325 52400425 52500525 52600625 52700725
52800825 52900925
5200-5300,Count: 12
5300-5400,Count: 0
5400-5500,Count: 0
5500-5600,Count: 0
5600-5700,Count: 0
5700-5800,Count: 0
5800-5900,Count: 0
5900-6000,Count: 0
60011006 60100106 60111106 60200206 60211206
60300306 60400406 60500506 60600606 60700706
60800806 60900906
6000-6100,Count: 12
61011016 61100116 61111116 61200216 61211216
61300316 61400416 61500516 61600616 61700716
61800816 61900916
6100-6200,Count: 12
62011026 62100126 62111126 62200226 62211226
62300326 62400426 62500526 62600626 62700726
62800826 62900926
6200-6300,Count: 12
6300-6400,Count: 0
6400-6500,Count: 0
6500-6600,Count: 0
6600-6700,Count: 0
6700-6800,Count: 0
6800-6900,Count: 0
6900-7000,Count: 0
70011007 70100107 70111107 70200207 70211207
70300307 70400407 70500507 70600607 70700707
70800807 70900907
7000-7100,Count: 12
71011017 71100117 71111117 71200217 71211217
71300317 71400417 71500517 71600617 71700717
71800817 71900917
7100-7200,Count: 12
72011027 72100127 72111127 72200227 72211227
72300327 72400427 72500527 72600627 72700727
72800827 72900927
7200-7300,Count: 12
7300-7400,Count: 0
7400-7500,Count: 0
7500-7600,Count: 0
7600-7700,Count: 0
7700-7800,Count: 0
7800-7900,Count: 0
7900-8000,Count: 0
80011008 80100108 80111108 80200208 80211208
80300308 80400408 80500508 80600608 80700708
80800808 80900908
8000-8100,Count: 12
81011018 81100118 81111118 81200218 81211218
81300318 81400418 81500518 81600618 81700718
81800818 81900918
8100-8200,Count: 12
82011028 82100128 82111128 82200228 82211228
82300328 82400428 82500528 82600628 82700728
82800828 82900928
8200-8300,Count: 12
8300-8400,Count: 0
8400-8500,Count: 0
8500-8600,Count: 0
8600-8700,Count: 0
8700-8800,Count: 0
8800-8900,Count: 0
8900-9000,Count: 0
90011009 90100109 90111109 90200209 90211209
90300309 90400409 90500509 90600609 90700709
90800809 90900909
9000-9100,Count: 12
91011019 91100119 91111119 91200219 91211219
91300319 91400419 91500519 91600619 91700719
91800819 91900919
9100-9200,Count: 12
92011029 92100129 92111129 92200229 92200229
92200229 92211229 92300329 92400429 92500529
92600629 92700729 92800829 92900929
9200-9300,Count: 14
9300-9400,Count: 0
9400-9500,Count: 0
9500-9600,Count: 0
9600-9700,Count: 0
9700-9800,Count: 0
9800-9900,Count: 0
9900-10000,Count: 0
Total: 333
由计算结果可知,之前讲的结论都是正确的;得出新结论有:
1 .有公元纪年以来,前20个世纪只有43个;
2. 从21世纪开始,每100年都有12个;
3. 9200-9300年,python程序重复计算9220.02.29了2次(14-2=2)。
所以,“完全对称日”总共有:43+12x24=331。
程序的执行结果是333个,错误的原因是9220.02.29重复计算了2次,后一个“92200229”实际上是9220.02.30,9220.02.31不存在的日子,不知道是不是python的datetime中转换引起的bug。
(本文完)
欢迎加入CSDN社区!http://https://bbs.csdn.net/forums/PythonTogether
http://xn--https-kt3b//bbs.csdn.net/forums/PythonTogether
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://hannyang.blog.csdn.net/article/details/121689044
内容来源于网络,如有侵权,请联系作者删除!