Improve code

This commit is contained in:
daniele 2019-09-28 11:32:21 +02:00
parent 7489196be8
commit ec1d1fa446
Signed by: fuxino
GPG Key ID: 6FE25B4A3EE16FDA
8 changed files with 16 additions and 26 deletions

View File

@ -30,7 +30,11 @@ int main(int argc, char **argv)
clock_gettime(CLOCK_MONOTONIC, &start);
/* N set to 1000000 as a reasonable limit, which turns out to be enough.*/
primes = sieve(N);
if((primes = sieve(N)) == NULL)
{
fprintf(stderr, "Error! Sieve function returned NULL\n");
return 1;
}
/* Checking only odd numbers with at least 4 digits.*/
for(i = 1001; i < N; i += 2)

View File

@ -24,7 +24,11 @@ int main(int argc, char **argv)
clock_gettime(CLOCK_MONOTONIC, &start);
primes = sieve(N);
if((primes = sieve(N)) == NULL)
{
fprintf(stderr, "Error! Sieve function returned NULL\n");
return 1;
}
/* Straightforward brute force approach.*/
for(p1 = 3; p1 < N && !found; p1 += 2)

View File

@ -26,7 +26,7 @@ def max_prime_factor(num):
# If num is divisible by i and i is prime, find largest
# prime factor of num/i.
while 1:
while True:
if num % i == 0:
if is_prime(i):
return max_prime_factor(num//i)

View File

@ -33,7 +33,7 @@ def main():
n = 0
j = 1
while 1:
while True:
tmp = i * j
n = n + tmp
j = j + 1

View File

@ -8,15 +8,6 @@
from timeit import default_timer
from projecteuler import is_pandigital, is_prime
def count_digits(n):
i = 0
while n > 0:
i = i + 1
n = n // 10
return i
def main():
start = default_timer()
@ -28,7 +19,7 @@ def main():
i = 7654321
while(i > 0):
if is_pandigital(i, count_digits(i)) and is_prime(i):
if is_pandigital(i, len(str(i))) and is_prime(i):
break
# Skipping the even numbers.
i = i - 2

View File

@ -35,7 +35,7 @@ def main():
i = 1
# Brute force approach, try every integer number until the desired one is found.
while 1:
while True:
if have_same_digits(i, 2*i) and have_same_digits(i, 3*i) and have_same_digits(i, 4*i) and\
have_same_digits(i, 5*i) and have_same_digits(i, 6*i):
break

View File

@ -18,15 +18,6 @@
from timeit import default_timer
def count_digits(n):
count = 0
while n > 0:
n = n // 10
count = count + 1
return count
def main():
start = default_timer()
@ -41,7 +32,7 @@ def main():
d = n + d
n = n + d2
if count_digits(n) > count_digits(d):
if len(str(n)) > len(str(d)):
count = count + 1
end = default_timer()

View File

@ -33,7 +33,7 @@ def main():
count = 0
diag = 5
while 1:
while True:
i = i + step
if is_prime(i):