Python练习题 046:Project Euler 019:每月1日是星期天

本题来自 Project Euler 第19题:https://projecteuler.net/problem=19

‘‘‘
How many Sundays fell on the first of the month
during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

Answer: 171
‘‘‘

from datetime import *

firstDay = date(1901,1,1)
lastDay = date(2000,12,31)

delta1 = timedelta(days=1)
firstSunday = date(1900,1,1)
while firstSunday == date(1900,1,1):
    if firstDay.isoweekday() == 7:
        firstSunday = firstDay  #找出第1个星期天
        break
    else:
        firstDay += delta1

delta7 = timedelta(days=7)
sundayCount = 0  #星期天的数量
while firstSunday <= lastDay:
    if firstSunday.day == 1:  #若为每月的1日
        sundayCount += 1
    firstSunday += delta7  #7天7天递增
print(sundayCount)

好吧,欧拉计划第18题做不出来,先跳过,先做第19题吧。

这题思路挺简单:在区间之内,先找出第1个星期天,然后7天7天地找,只要是每月的第1天,计数器就加1,很快就有答案。

话说,Python好像不能像MySQL那样,直接拿一个日期加上数字n,计算n天之后的日期;而是得借助timedelta,设定间隔时间,再进行运算,感觉……挺麻烦的。不过,用isoweekday()直接判断星期几、用day()直接判断是每月的第几天,倒是挺方便的~~~

时间: 2024-08-02 11:04:34

Python练习题 046:Project Euler 019:每月1日是星期天的相关文章

Python练习题 048:Project Euler 021:10000以内所有亲和数之和

本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable numbers Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b

Python练习题 047:Project Euler 020:阶乘结果各数字之和

本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial digit sum n! means n × (n ? 1) × ... × 3 × 2 × 1 For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, and the sum of the digits in the number 10! i

Python练习题 035:Project Euler 007:第10001个素数

本题来自 Project Euler 第7题:https://projecteuler.net/problem=7 # Project Euler: Problem 7: 10001st prime # By listing the first six prime numbers: # 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. # What is the 10 001st prime number? # Answer

Python练习题 034:Project Euler 006:和平方与平方和之差

本题来自 Project Euler 第6题:https://projecteuler.net/problem=6 # Project Euler: Problem 6: Sum square difference # The sum of the squares of the first ten natural numbers is, # 1**2 + 2**2 + ... + 10**2 = 385 # The square of the sum of the first ten natur

Python练习题 042:Project Euler 014:最长的考拉兹序列

本题来自 Project Euler 第14题:https://projecteuler.net/problem=14 ''' Project Euler: Problem 14: Longest Collatz sequence The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule

Python练习题 041:Project Euler 013:求和、取前10位数值

本题来自 Project Euler 第13题:https://projecteuler.net/problem=13 # Project Euler: Problem 13: Large sum # Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. # Answer: 5537376230 numbers = '''371072875339021027987979982

Python练习题 038:Project Euler 010:两百万以内所有素数之和

本题来自 Project Euler 第10题:https://projecteuler.net/problem=10 # Project Euler: Problem 10: Summation of primes # The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. # Find the sum of all the primes below two million. # Answer: 142913828922 def f(x):

Python练习题 037:Project Euler 009:毕达哥拉斯三元组之乘积

本题来自 Project Euler 第9题:https://projecteuler.net/problem=9 # Project Euler: Problem 9: Special Pythagorean triplet # A Pythagorean triplet is a set of three natural numbers, # a < b < c, for which, a**2 + b**2 = c**2 # For example, 3**2 + 4**2 = 9 +

Python练习题 043:Project Euler 015:方格路径

本题来自 Project Euler 第15题:https://projecteuler.net/problem=15 ''' Project Euler: Problem 15: Lattice paths Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right