Skip to content

Commit

Permalink
Refactor: Formats Python files with yapf
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankKair committed Jun 19, 2018
1 parent 3973a65 commit 87d4889
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/002/p002.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Fast Fibonacci Number
# http://mathworld.wolfram.com/FibonacciNumber.html
fib = lambda n: int(((1 + sqrt(5))**n - (1 - sqrt(5))**n)/(2**n * sqrt(5)))
fib = lambda n: int(((1 + sqrt(5))**n - (1 - sqrt(5))**n) / (2**n * sqrt(5)))

n = 2
s = 0
Expand Down
4 changes: 3 additions & 1 deletion src/003/p003.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ def prime(n):
if n % 2 == 0 or n < 2: return False
for i in range(3, int(n**0.5) + 1, 2):
if n % i == 0:
return False
return False

return True


def factors(n):
f, s = 2, set()
while n > 1:
Expand All @@ -18,4 +19,5 @@ def factors(n):
f += 1
return s


print(max([n for n in factors(600851475143) if prime(n)]))
4 changes: 3 additions & 1 deletion src/005/p005.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#!/usr/bin/env python
def gcd(a, b):
while b:
while b:
a, b = b, a % b
return a


def lcm(a, b):
return a * b // gcd(a, b)


x = lcm(1, 2)
for i in range(3, 21):
x = lcm(x, i)
Expand Down
3 changes: 2 additions & 1 deletion src/007/p007.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ def prime(n):
if n % 2 == 0 or n < 2: return False
for i in range(3, int(n**0.5) + 1, 2):
if n % i == 0:
return False
return False

return True


t, n = 0, 2
while t != 10001:
t += prime(n)
Expand Down
4 changes: 3 additions & 1 deletion src/010/p010.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#!/usr/bin/env python
import math


def sum_primes(n):
m = int(n + 1)
numbers = [True for i in range(m)]
for i in range(2, int(math.sqrt(n))):
if numbers[i]:
for j in range(i*i, m, i):
for j in range(i * i, m, i):
numbers[j] = False
sp = 0
for i in range(2, m):
if numbers[i]:
sp += i
return sp


print(sum_primes(2e6))
5 changes: 3 additions & 2 deletions src/011/p011.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
g4 = []
for i in range(17):
for j in range(17):
g4.append([[int(g[a][b]) for a in range(i, i+4)] for b in range(j, j+4)])
g4.append([[int(g[a][b]) for a in range(i, i + 4)]
for b in range(j, j + 4)])

v = []
for m in g4:
Expand All @@ -44,6 +45,6 @@
# Principal diagonal products
v += [prod([m[i][i] for i in range(4)])]
# Secondary diagonal products
v += [prod([m[i][3-i] for i in range(4)])]
v += [prod([m[i][3 - i] for i in range(4)])]

print(max(v))
7 changes: 6 additions & 1 deletion src/012/p012.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

from functools import reduce


def factors(n):
return set(reduce(list.__add__, ([i, n//i] for i in range(1, int(pow(n, 0.5) + 1)) if n % i == 0)))
return set(
reduce(list.__add__, ([i, n // i]
for i in range(1, int(pow(n, 0.5) + 1))
if n % i == 0)))


i, tnum = 2, 1
while len(factors(tnum)) < 500:
Expand Down
8 changes: 5 additions & 3 deletions src/014/p014.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#!/usr/bin/env python


def collatz(n):
seq = [n]
while n > 1:
if n % 2 == 0:
n = n/2
n = n / 2
else:
n = 3*n + 1
n = 3 * n + 1
seq.append(int(n))
return seq


sizes = [len(collatz(i)) for i in range(1, int(1e6))]

print(sizes.index(max(sizes))+1)
print(sizes.index(max(sizes)) + 1)
13 changes: 9 additions & 4 deletions src/017/p017.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#!/usr/bin/env python


def words(n):
u = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten',\
'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', \
'eighteen', 'nineteen']

d = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
d = [
'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty',
'ninety'
]

if n == 0:
return ''
Expand All @@ -14,15 +18,16 @@ def words(n):
return u[n - 1]

if n < 100:
word = d[n//10 - 2]
word = d[n // 10 - 2]
if n % 10 != 0:
word += words(n % 10)
return word

suffix = words(n - (n//100)*100)
word = u[n//100 - 1] + 'hundred'
suffix = words(n - (n // 100) * 100)
word = u[n // 100 - 1] + 'hundred'
if suffix:
word += 'and' + suffix
return word


print(len(''.join(words(i) for i in range(1, 1000)) + 'onethousand'))

0 comments on commit 87d4889

Please sign in to comment.