Euler Project question 1 in python way

# if we list all 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.
import time
t0 = time.time()
sum = 0
for i in range(1, 1000):
  if (i % 3 == 0) or (i % 5 == 0):
    sum = sum + i
print(sum)
t1 = time.time()
print "Process usage", t1 - t0

# result
# 233168
# Process usage 0.00100016593933



adding at 11.10.2014

# using operator, list, for loop&conditions to simplify creating a list, best version
import time
start = time.time()
print sum([i for i in range(1, 1000) if i % 3 == 0 or i % 5 == 0])
print "Process usage:", time.time() - start

# result
# 233168
# Process usage: 0.000999927520752

# using xrang()
# import time
# start = time.time()
# print sum([i for i in xrange(1, 1000) if i % 3 == 0 or i % 5 == 0])
# print "Process usage:", time.time() - start

# result
# 233168
# Process usage: 0.00200009346008

时间: 2024-12-26 21:53:57

Euler Project question 1 in python way的相关文章

Euler Project question 25 in python way

# This Python file uses the following encoding: utf-8# The Fibonacci sequence is defined by the recurrence relation: # Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.# Hence the first 12 terms will be: # F1 = 1# F2 = 1# F3 = 2# F4 = 3# F5 = 5# F6 = 8# F7

Euler Project question 16 in python way

# This Python file uses the following encoding: utf-8# 215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. # What is the sum of the digits of the number 21000?import timestart = time.time()print "2**1000's sum of digits is %s and cost %s

Euler Project question 29 in python way

# This python file uses the following encoding: utf-8# Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5: # 22=4, 23=8, 24=16, 25=32# 32=9, 33=27, 34=81, 35=243# 42=16, 43=64, 44=256, 45=1024# 52=25, 53=125, 54=625, 55=3125# If they

Euler Project question 14 in python way

# This Python file uses the following encoding: utf-8# 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 above and starting with 13, we generate the following s

Euler Project question 31 in python way

# This python file uses the following encoding: utf-8# In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation: # 1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).# It is possible to make £2 in

Euler Project question 15 in python way

# This Python file uses the following encoding: utf-8# 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 corner. # How many such routes are there through a

Euler Project question 2 in python way

# 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 n

Euler Project question 48 in python way

# The series, 11 + 22 + 33 + ... + 1010 = 10405071317. # Find the last ten digits of the series, 11 + 22 + 33 + ... + 10001000. import timestart = time.time()print "last ten digits was %s and cost %s seconds" % (str(sum([i**i for i in range(1, 1

Euler Project question 30 in python way

# Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits: # 1634 = 14 + 64 + 34 + 44# 8208 = 84 + 24 + 04 + 84# 9474 = 94 + 44 + 74 + 44# As 1 = 14 is not a sum it is not included. # The sum of these