Improve code

This commit is contained in:
2019-09-22 10:33:18 +02:00
parent cac80478da
commit 9db44b9d1f
18 changed files with 174 additions and 134 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/python3
import math
from math import floor, sqrt
from timeit import default_timer
from projecteuler import is_prime
@ -13,7 +13,7 @@ def max_prime_factor(num):
return max_prime_factor(num // 2)
else:
limit = math.floor(math.sqrt(num)) + 1
limit = floor(sqrt(num)) + 1
for i in range(3, limit, 2):
if num % i == 0:

View File

@ -33,8 +33,8 @@ def main():
curr = number[i:i+13]
prod = 1
for j in range(len(curr)):
prod = prod * curr[j]
for j in curr:
prod = prod * j
if prod > max_:
max_ = prod

View File

@ -10,7 +10,7 @@ def main():
sum_ = 0
for i in range(2000000):
if primes[i]:
if primes[i] == 1:
sum_ = sum_ + i
end = default_timer()

View File

@ -9,8 +9,8 @@ def main():
sum_ = 0
for i in range(len(res)):
sum_ = sum_ + int(res[i])
for i in res:
sum_ = sum_ + int(i)
end = default_timer()

View File

@ -31,7 +31,9 @@ def main():
fp.close()
for i in range(len(triang)):
l = len(triang)
for i in range(l):
triang[i] = list(map(int, triang[i]))
max_ = 0

View File

@ -11,8 +11,8 @@ def main():
sum_ = 0
for i in range(len(n)):
sum_ = sum_ + int(n[i])
for i in n:
sum_ = sum_ + int(i)
end = default_timer()

View File

@ -1,11 +1,11 @@
#!/usr/bin/python3
import math
from math import floor, sqrt
from timeit import default_timer
def sum_of_divisors(n):
limit = math.floor(math.sqrt(n)) + 1
limit = floor(sqrt(n)) + 1
sum_ = 1

View File

@ -15,14 +15,16 @@ def main():
names.sort()
sum_ = 0
i = 1
for i in range(len(names)):
l = len(names[i])
for name in names:
l = len(name)
score = 0
for j in range(l):
score = score + ord(names[i][j]) - ord('A') + 1
score = score * (i + 1)
score = score + ord(name[j]) - ord('A') + 1
score = score * i
sum_ = sum_ + score
i = i + 1
end = default_timer()

View File

@ -1,11 +1,11 @@
#!/usr/bin/python3
import math
from math import floor, sqrt
from timeit import default_timer
def is_abundant(n):
limit = math.floor(math.sqrt(n)) + 1
limit = floor(sqrt(n)) + 1
sum_ = 1
for i in range(2, limit):

View File

@ -7,14 +7,14 @@ def is_circular_prime(n):
global primes
if primes[n] == 0:
return 0
return False
tmp = n
count = 0
while tmp > 0:
if tmp % 2 == 0:
return 0
return False
count = count + 1
tmp = tmp // 10
@ -22,9 +22,9 @@ def is_circular_prime(n):
n = n % (10 ** (count - 1)) * 10 + n // (10 ** (count - 1))
if primes[n] == 0:
return 0
return False
return 1
return True
def main():
start = default_timer()

View File

@ -11,7 +11,7 @@ def is_tr_prime(n):
while tmp > 0:
if not is_prime(tmp):
return 0
return False
tmp = tmp // 10
i = 10
@ -19,11 +19,11 @@ def is_tr_prime(n):
while tmp != n:
if not is_prime(tmp):
return 0
return False
i = i * 10
tmp = n % i
return 1
return True
def main():
start = default_timer()

View File

@ -3,30 +3,7 @@
from numpy import zeros
from timeit import default_timer
def is_pandigital(value, n):
i = 0
digits = zeros(n + 1, int)
while i < n and value > 0:
digit = value % 10
digits[digit] = digits[digit] + 1
value = value // 10
i = i + 1
if i < n or value > 0:
return 0
if digits[0] != 0:
return 0
for i in range(1, n+1):
if digits[i] != 1:
return 0
i = i + 1
return 1
from projecteuler import is_pandigital
def main():
start = default_timer()

View File

@ -1,22 +1,23 @@
#!/usr/bin/python3
import math
from numpy import ndarray
from math import sqrt, floor
from numpy import ndarray, zeros
def is_prime(num):
if num < 4:
return num == 2 or num == 3
if num % 2 == 0 or num % 3 == 0:
return 0
return False
limit = math.floor(math.sqrt(num)) + 1
limit = floor(sqrt(num)) + 1
for i in range(5, limit, 6):
if num % i == 0 or num % (i + 2) == 0:
return 0
return False
return 1
return True
def gcd(a, b):
if b == 0:
@ -48,7 +49,7 @@ def sieve(n):
primes[i] = 0
primes[i+1] = 1
limit = math.floor(math.sqrt(n))
limit = floor(sqrt(n))
for i in range(3, limit, 2):
if primes[i]:
@ -59,7 +60,7 @@ def sieve(n):
def count_divisors(n):
count = 0
limit = math.floor(math.sqrt(n))
limit = floor(sqrt(n))
for i in range(1, limit):
if n % i == 0:
@ -81,6 +82,37 @@ def is_palindrome(num, base):
tmp = tmp // base
if num == reverse:
return 1
return True
return False
def is_pandigital(value, n):
i = 0
digits = zeros(n + 1, int)
while i < n and value > 0:
digit = value % 10
if digit > n:
return False
digits[digit] = digits[digit] + 1
value = value // 10
i = i + 1
if i < n or value > 0:
return False
if digits[0] != 0:
return False
for i in range(1, n+1):
if digits[i] != 1:
return False
i = i + 1
return True
def is_pentagonal(n):
i = (sqrt(24*n+1) + 1) / 6
return i.is_integer()
return 0