【欧拉计划第 8 题】序列中最大的乘积 Largest product in a series

简介: 【欧拉计划第 8 题】序列中最大的乘积 Largest product in a series

Problem 8 Largest product in a series

The four adjacent digits in the 1000-digit number that have the greatest product are 9 ∗ 9 ∗ 8 ∗ 9 = 5832 9*9*8*9=58329∗9∗8∗9=5832.

Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?

问题 8 序列中最大的乘积

1000 位数字中乘积最大的相邻四个数字是 9 ∗ 9 ∗ 8 ∗ 9 = 5832 9*9*8*9=58329∗9∗8∗9=5832。

找出 1000 位数字中乘积最大的 13 个相邻数字。这个数字的乘积是什么?

数字文件

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

思路分析

本题用 Python 效率会高一些,且思路更简单

我们将所有数字存放到本地的 txt 文件中,使用 Python 导入程序并存放在一个 List 中,此时,从 List 的第 0 位一直到最后,求出十三位数字的乘积并存方到另一个 List 中(结果集),在结果集中找出最大值并输出

下面附上两段代码供参考

代码实现

'''
Author: chenwr_727
Date: 2016-12-19 21:47:36
LastEditTime: 2022-04-15 17:29:02
'''
from functools import reduce
digit_str = '73167176531330624919225119674426574742355349194934' + \
    '96983520312774506326239578318016984801869478851843' + \
    '85861560789112949495459501737958331952853208805511' + \
    '12540698747158523863050715693290963295227443043557' + \
    '66896648950445244523161731856403098711121722383113' + \
    '62229893423380308135336276614282806444486645238749' + \
    '30358907296290491560440772390713810515859307960866' + \
    '70172427121883998797908792274921901699720888093776' + \
    '65727333001053367881220235421809751254540594752243' + \
    '52584907711670556013604839586446706324415722155397' + \
    '53697817977846174064955149290862569321978468622482' + \
    '83972241375657056057490261407972968652414535100474' + \
    '82166370484403199890008895243450658541227588666881' + \
    '16427171479924442928230863465674813919123162824586' + \
    '17866458359124566529476545682848912883142607690042' + \
    '24219022671055626321111109370544217506941658960408' + \
    '07198403850962455444362981230987879927244284909188' + \
    '84580156166097919133875499200524063689912560717606' + \
    '05886116467109405077541002256983155200055935729725' + \
    '71636269561882670428252483600823257530420752963450'
i = 0
n = 13
prod_12 = 0
prod_max = 0
while i + n < len(digit_str):
    if prod_12 == 0:
        if '0' not in digit_str[i: i+n]:
            prod_ = reduce(lambda x,y: int(x) * int(y), digit_str[i: i+n])
        else:
            i = digit_str.index('0', i) + 1
            continue
    else:
        if digit_str[i+n-1] == '0':
            prod_12 = 0
            i += n
            continue
        else:
            prod_ = prod_12 * int(digit_str[i+n-1])
    if prod_ > prod_max:
        prod_max = prod_
    prod_12 = prod_ / int(digit_str[i])
    i += 1
print(prod_max)
'''
Author: metaquant
Date: 2016-12-19 21:47:36
LastEditTime: 2022-04-15 17:29:02
'''
from functools import reduce
def main():
    with open('your file location','r') as f:
        data = ''
        for line in f.readlines():
            data = data + line.strip()
    res = []
    for i in range(988):
        sub = [int(x) for x in data[i:i+13]]
        prod = reduce(lambda x,y:x*y, sub)
        res.append(prod)
    return max(res)

答案:23514624000


相关文章
|
存储 Oracle 关系型数据库
PostgreSQL体系架构介绍
PostgreSQL体系架构介绍
1925 0
PostgreSQL体系架构介绍
|
安全 测试技术 数据库
测试流程规范--准入准出规则
为了加强测试部软件测试的质量控制及与测试相关部门、人员更好理解测试各阶段的准入/准出条件而建立的准入/准出规范。
3859 0
测试流程规范--准入准出规则
|
移动开发 前端开发
RN运行项目Error:Unable to resolve module
RN运行项目Error:Unable to resolve module
1537 0
RN运行项目Error:Unable to resolve module
|
消息中间件 缓存 NoSQL
热点账户高并发记账方案
热点账户高并发记账方案
2463 0
热点账户高并发记账方案
|
SQL 关系型数据库 MySQL
Docker 部署离线版 Nginx 镜像容器,Tomcat 镜像容器,Mariadb 镜像容器
Docker 部署离线版 Nginx 镜像容器,Tomcat 镜像容器,Mariadb 镜像容器
3857 0
Docker 部署离线版 Nginx 镜像容器,Tomcat 镜像容器,Mariadb 镜像容器
|
11月前
|
存储 缓存 算法
SGLang Hierarchical Sparse Attention 技术深度解析
阿里云Tair联合SGLang、蚂蚁AI Infra及震旦团队,推出面向Sparse Attention的分层稀疏化框架:将全量KV Cache存于CPU,GPU仅驻留Top-k LRU Buffer,结合Sparse Diff Kernel与IO Kernel实现高效增量传输。实测DeepSeek DSA场景下,单请求显存从8GB降至200MB,吞吐提升3倍,突破长上下文推理的带宽与容量双重瓶颈。(240字)
SGLang Hierarchical Sparse Attention 技术深度解析
|
10月前
|
Web App开发 XML PHP
使用PHP获取网页内容的多种方法
/** * 使用PHP获取网页内容的多种方法案例分享 */
|
编解码 算法 固态存储
SSD ECC纠错“天网”之LDPC码
在之前的文章中有提到过,SSD FTL层有一个很重要的功能就是ECC纠错(ECC, Error Correction Code)。
|
开发工具 git
git clone 失败解决方法:Failed to connect to 127.0.0.1 port 31181 Connection refused
git clone 失败解决方法:Failed to connect to 127.0.0.1 port 31181 Connection refused
1568 1
git clone 失败解决方法:Failed to connect to 127.0.0.1 port 31181 Connection refused
|
存储 SQL 分布式计算
小迈科技 X Hologres:高可用的百亿级广告实时数仓建设
通过本文,我们将会介绍小迈科技如何通过Hologres搭建高可用的实时数仓。
2981 3
小迈科技 X Hologres:高可用的百亿级广告实时数仓建设