Project Euler 50-56

Project Euler: https://projecteuler.net/
Project Euler | 欧拉计划: https://pe-cn.github.io/
Project Euler 1-7
Project Euler 8-14
Project Euler 15-21 & 67
Project Euler 22-28
Project Euler 29-35
Project Euler 36-42
Project Euler 43-49
Project Euler 50-56
Project Euler 57-63
如果有错误,还望指出,欢迎互相交流学习
随缘更新

Problem 50: Consecutive prime sum

Which prime, below one-million, can be written as the sum of the most consecutive primes?

def eratosthenes_sieve(max_number):sieve = [0] * (max_number + 1)sieve[0], sieve[1] = -1, -1for i in range(2, max_number + 1):if sieve[i] == 0:for j in range(2 * i, max_number + 1, i):sieve[j] = 1return sieve
primes = []
for i, elem in enumerate(eratosthenes_sieve(1000001)):if elem == 0: # 从题目示例开始查找primes.append(i)length = 1
s = primes[0]
while True:s += primes[length]if s >= 1000000: breaklength += 1        while length > 1:Sum = sum(primes[:length])if Sum in primes: breaki = 0found = Falsewhile not found:Sum += primes[length + i] - primes[i]if Sum >= 1000000: breakfound = Sum in primesi += 1if found: breaklength -= 1
print(Sum)
997651

Problem 51: Prime digit replacements

Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family.

  • counter: 一个计数器工具提供快速和方便的计数。
    https://docs.python.org/zh-cn/3/library/collections.html#collections.Counter
    -(1) 至少要是一个四位数,我们可以从数字1111开始搜寻;
    -(2) 重复的数位只能是三或三的倍数;
    -(3) 重复的数字只能是0, 1, 2三个数。
  • 参考https://www.cnblogs.com/metaquant/p/11820821.html
def eratosthenes_sieve(max_number):sieve = [0] * (max_number + 1)sieve[0], sieve[1] = -1, -1for i in range(2, max_number + 1):if sieve[i] == 0:for j in range(2 * i, max_number + 1, i):sieve[j] = 1return sieve
primes = []
for i, elem in enumerate(eratosthenes_sieve(1000001)):if elem == 0 and i>1111: primes.append(i)
from collections import Counterdef is_replacable_prime(n):s = str(n)count = Counter(s)num,d = count.most_common(1)[0]if d % 3 == 0 and num in set('012'):k = 1for j in range(int(num)+1,10):new = s.replace(num,str(j))if int(new) in primes:k += 1if k == 8:return Truereturn Falsedef main():for p in primes:if is_replacable_prime(p):return p
main()
121313

Problem 52: Permuted multiples

Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.

for a in range(1,1000000):b = sorted(str(a))for i in range(2, 7):if len(str(a*i)) > len(b):breakif b != sorted(str(a*i)):breakelse:print(a)break
142857

Problem 53: Combinatoric selections

How many, not necessarily distinct, values of nCr, for 1 ≤ n ≤ 100, are greater than one-million?

from itertools import combinations
import math
def C(n,m): # 计算组合数return math.factorial(n)//(math.factorial(m)*math.factorial(n-m))ans = 0
for i in range(23,101):for j in range(1,i):if C(i,j)>1000000:ans += 1
print(ans)
4075

Problem 54:Poker hands

How many hands does Player 1 win?

  • 参考j123 https://projecteuler.net/best_posts=054
T=(([1],[3,1.5]),([3,1.7],[5]))# 烂牌,顺子,同花,同花顺
def rank(H, v=dict(zip(b'23456789TJQKA', range(13)))):C,V,S = zip(*reversed(sorted([(H.count(c), v[c], s) for c, s in H.split()])))return ([C[0], C[C[0]]] if C[0]>1 else T[len(set(S))==1][V[0]-V[4]==4]), Vprint(sum([rank(s[:14]) > rank(s[15:]) for s in open('data/p054_poker.txt', 'rb')]))
376

Problem 55:Lychrel numbers

How many Lychrel numbers are there below ten-thousand?

def is_palindrome(n):n = str(n)if n == n[::-1]:return Truereturn Falsedef is_lychrel(n):for i in range(50):n += int(str(n)[::-1])if is_palindrome(n):return Falsereturn Trueans = 0
for i in range(1, 10001):if is_lychrel(i):ans += 1
print(ans)
249

Problem 56: Powerful Digit Sum

Considering natural numbers of the form, a b a^b ab, where a, b < 100, what is the maximum digital sum?

  • 大数乘法
ans = []
for a in range(2,100):NUM = [1]carry = 0for b in range(2,100):carry = 0for j in range(len(NUM)):temp = NUM[j] * a + carryif temp>9:NUM[j] = temp%10carry = temp//10else:carry = 0NUM[j] = temp      while carry>0:temp = carryNUM.append(temp%10)carry = temp//10ans.append(sum(NUM))
print(max(ans))
972

欧拉计划 Project Euler 50-56相关推荐

  1. 欧拉计划(project euler)最详细中文题解

    欧拉计划是一个在线解题网站,题目以各类数学问题为主,通常需要结合一定的数学与编程知识,写出适当的程序求解问题(详细介绍可以参见我的文章).相比于力扣等刷题网站,欧拉计划上的题目有着更丰富的知识背景,在 ...

  2. 欧拉计划 P429 (数论)

    欧拉计划 P429 Sum of squares of unitary divisors(数论) 传送门:https://projecteuler.net/problem=429 题目大意: 定义一个 ...

  3. 一欧拉函数(Euler‘s totient function)

    算法总结 一欧拉函数(Euler's totient function) 欧拉函数的定义: 在数论中,对于正整数N,少于或等于N ([1,N]),且与N互质的正整数(包括1)的个数,记作φ(n). φ ...

  4. 欧拉计划:第53题 Combinatoric selections

    欧拉计划:第53题 Combinatoric selections 无意中知道有这个用编程解决数学题的网站,个人对数学又比较感兴趣,所以打算做个欧拉计划系列,将自己的一些见解分享给大家,第一次写文章, ...

  5. 欧拉计划(鱼C论坛)@20161107

    1.10以下的自然数中,属于3或5的倍数的数字有3,5,6,9,它们之和为23找出1000以下的自然数中,属于3或5倍数的所有数字之和. """ 欧拉计划 10以下的自然 ...

  6. python求直角三角形个数的公式_Python3 欧拉计划 问题71-75

    EulerProject.png 问题66-70参见:https://www.jianshu.com/p/d0fad6213433 71.有序分数 考虑形如n/d的分数,其中n和d均为正整数.如果n ...

  7. 【欧拉计划第 13 题】 大数之和 Large sum

    Problem 13 Large sum Work out the first ten digits of the sum of the following one-hundred 505050-di ...

  8. 欧拉计划17——数字字母计数

    如果数字1到5用单词写出来:1.2.3.4.5,则总共使用3 + 3 + 5 + 4 + 4 = 19个字母. 如果用文字写出从1到1000(含1000)之间的所有数字,那么将使用多少个字母? 注意: ...

  9. 欧拉计划 31~40

    硬币求和 #include <bits/stdc++.h> using namespace std;int a[10] = {1, 2, 5, 10, 20, 50, 100, 200}, ...

最新文章

  1. vue 使用scss
  2. (一)网络与信息安全概论入门阶段笔记
  3. Echarts学习记录——如何去掉网格线及网格区域颜色
  4. Asp.Net MVC中使用ACE模板之Jqgrid
  5. Ranger-AdminServer安装
  6. Oracle sqlplus使用总结
  7. android学习—— context 和 getApplicationContext()
  8. 代码生成器AutoGenerator
  9. 阿里云播放器组件 vue-aliplayer
  10. 中美两本有影响力数理统计学教材的对比及其启示(龚凤乾)
  11. itextpdf使用总结
  12. win10安装杜比驱动
  13. 1970年图灵奖--詹姆斯·威尔金森生平
  14. QMH、AMC和STM之间的关系
  15. Floyd Thomas - Principles of Electric Circuits_ Conventional Current-Pearson (2021) 电路基础书籍推荐
  16. 微信扫码登陆或注册设计流程
  17. Neo4j因果集群路由策略详解及驱动访问
  18. 外屏和宽屏浪费了?HarmonyOS折叠屏设计规范教你用起来
  19. 安利一个自动求导网站
  20. Android 内容复制到剪贴板

热门文章

  1. ”WinForm上位机+OV7670摄像头+STM32+蓝牙“图像采集系统(二)PC-MCU蓝牙通信及WinForm上位机开发
  2. SQL长度:fasterxml.jackson.databind.JsonMappingException: Numeric value () out of range of int
  3. win10系统修改文件后缀名
  4. 树莓派基础实验31:MPU6050陀螺仪加速度传感器实验
  5. 电脑白板软件如何使用_什么是Microsoft白板,以及如何使用它?
  6. digg bt_Digg主页! 呜呜!
  7. 计算机联锁监测哪些内容,地铁信号 计算机联锁及监测系统设备测试计划纲要...
  8. 《设计的品格 探索×呈现×进化的InDesign美学》—第1课1.5节字符
  9. linux 查看tomcat时区,项目系统时区问题
  10. iOS alloc 源码探究