Project Euler Problem1-5

Q1: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

A1: 233168

s=0
for i in range(1,1000):
    if i%3==0 and i%5!=0:
        s+=i
    if i%5==0:
        s+=i
    i=i+1
print(s)

Q2: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
A2: 4613732

a,b=0,1
s=0
while a<4000000:
    if(a%2==0):
        s+=a
    a,b = b, a+b
print(s)

Q3: The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
A3: 6857

 import math

def is_prime(n):

  if n == 2 or n == 3: return True

  if n < 2 or n%2 == 0: return False

  if n < 9: return True

  if n%3 == 0: return False

  r = int(n**0.5)

  f = 5

  while f <= r:

    if n%f == 0: return False

    if n%(f+2) == 0: return False

    f +=6

  return True    



for i in range(2,int(math.sqrt(600851475143)),1):

    if(600851475143%i==0 and is_prime(i)):

        print(i)

Q4: A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
A4: 906609 

 def is_palindromic(n):

    a=len(str(n))

    b=str(n)

    for i in range(int(a/2)):

        if(b[i]!=b[a-i-1]):

            return False

    return True

max=10000

for i in range(100,999):

    for j in range(100,999):

        if is_palindromic(i*j):

            if(max<i*j):

                max=i*j

print(max)

Q5:  2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

A5: 232792560
def gcd(a,b):
    c=1
    for j in range(2,a):
        if(b%j==0 and a%j==0):
            c=j
    return c  
m=1
for i in range (2,20):
    m=m*(int(i/gcd(m,i)))
print(m)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值