Functional programming in Python Project Euler 1

Problem 1

"Multiples of 3 and 5"

Of the natural numbers less than 10, there are four that are multiples of 3 or 5, and the sum of these is 23.

In the same way, find the sum of the numbers that are multiples of 3 or 5 less than 1000.

List comprehension

You can use list comprehension notation to create a description close to a functional type.

List comprehension


multiplesOf3or5 = [n for n in range(1, 1000) if n % 3 == 0 or n % 5 == 0]
answer = sum(multiplesOf3or5)
print(answer)

Functional type

If you write it more functionally with the filter function,

Functional type


def isMultipleOf3or5(n):
    return n % 3 == 0 or n % 5 == 0

multiplesOf3or5 = filter(isMultipleOf3or5, range(1, 1000))
answer = sum(multiplesOf3or5)
print(answer)

In Python, the function name becomes a function object as it is, so you can just write the function name in the first argument of the filter function. It's much easier and more intuitive than a C # delegate.

Lambda expression

You can also replace the function with a lambda expression. Lambda expressions in Python lambda [argument 1], [argument 2]: [expression] I will write it as. (I wrote only two arguments, but of course three or more are OK.)

Lambda expression


multiplesOf3or5 = filter(lambda n: n % 3 == 0 or n % 5 == 0, range(1, 1000))
answer = sum(multiplesOf3or5)
print(answer)

If you use a lambda expression, it will look like a list comprehension. In this case, I feel that the list notation is more intuitive than the lambda expression.

Recommended Posts

Functional programming in Python Project Euler 1
Functional programming in Python Project Euler 3
Functional programming in Python Project Euler 2
[Note] Project Euler in Python (Problem 1-22)
Project Euler # 5 "Minimum Multiples" in Python
Programming in python
Project Euler # 15 "Lattice Path" in Python
Project Euler # 4 "Maximum Palindrome" in Python
Project Euler # 3 "Maximum Prime Factors" in Python
Project Euler # 11 "Maximum Product in Grid" in Python
Project Euler # 7 "1000 1st prime number" in Python
Project Euler # 16 "Sum of Powers" in Python
Project Euler # 9 "Special Pythagorean Triple" in Python
Project Euler # 14 "Longest Collatz Sequence" in Python
Try a functional programming pipe in Python
Project Euler # 2 "Even Fibonacci Numbers" in Python
Project Euler # 17 "Number of Characters" in Python
Project Euler # 1 "Multiples of 3 and 5" in Python
Python programming in Excel
What is "functional programming" and "object-oriented" in Python?
Project Euler # 8 "Maximum Product in Number String" in Python
Project Euler # 10 "sum of prime numbers" in Python
Project Euler # 12 "High Divisibility Triangular Number" in Python
Project Euler # 13 "Sum of Large Numbers" in Python
Project Euler # 6 "Difference in sum of squares" in Python
Get in touch with functional programming in JavaScript or Python 3
GUI programming in Python using Appjar
Project Euler 7
Project Euler 47
Project Euler 31
Project Euler 4
Project Euler 38
Project Euler 17
Project Euler 26
Project Euler 8
Project Euler 23
Project Euler 22
Project Euler 19
Project Euler 50
Project Euler 42
Project Euler 33
Project Euler 32
Project Euler 43
Project Euler 35
Project Euler 36
Project Euler 24
Project Euler 46
Project Euler 48
Project Euler 45
Project Euler 6
Project Euler 44
Create Python project documentation in Sphinx
Project Euler 39
Project Euler 40
Project Euler 49
Project Euler 29
Project Euler 27
Project Euler 41
Project Euler 18
Project Euler 11 "Maximum product in grid"
Project Euler 13