-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler.py
60 lines (46 loc) · 1.45 KB
/
euler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import math
def is_prime(n):
return n > 1 and all(n % i for i in range(2, int(n ** 0.5) + 1))
def divisors(n):
return [d for d in range(2,n//2+1) if n % d == 0 ]
# Returns the prime numbers of n
def primes(n):
return [ d for d in divisors(n) if \
all( d % od != 0 for od in divisors(n) if od != d ) ]
# Returns groesste gemeinsame Teiler for a and b
def ggt(a, b):
while b != 0:
c = a % b
a, b = b, c
return a
# Returns kleinstes gemeinsames Vielfaches for a and b
def kgv(a, b):
return (a * b) / ggt(a, b)
def SieveOfEratosthenes(n):
# Create a boolean array "prime[0..n]" and initialize
# all entries it as true. A value in prime[i] will
# finally be false if i is Not a prime, else true.
prime = [True for i in range(n + 1)]
p = 2
while (p * p <= n):
# If prime[p] is not changed, then it is a prime
if (prime[p] == True):
# Update all multiples of p
for i in range(p * 2, n + 1, p):
prime[i] = False
p += 1
prime[0]= False
prime[1]= False
# Print all prime numbers
return [p for p in range(n + 1) if prime[p]]
def binomial(n, k):
assert 0 <= k <= n
return math.factorial(n) // (math.factorial(k) * math.factorial(n - k))
def quersumme(n):
s = 0
while n:
s += n % 10
n //= 10
return s
def palindrom_check(a):
return str(a) == str(a)[::-1]