Improve code
This commit is contained in:
parent
7489196be8
commit
ec1d1fa446
6
C/p051.c
6
C/p051.c
@ -30,7 +30,11 @@ int main(int argc, char **argv)
|
|||||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||||
|
|
||||||
/* N set to 1000000 as a reasonable limit, which turns out to be enough.*/
|
/* 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.*/
|
/* Checking only odd numbers with at least 4 digits.*/
|
||||||
for(i = 1001; i < N; i += 2)
|
for(i = 1001; i < N; i += 2)
|
||||||
|
6
C/p060.c
6
C/p060.c
@ -24,7 +24,11 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
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.*/
|
/* Straightforward brute force approach.*/
|
||||||
for(p1 = 3; p1 < N && !found; p1 += 2)
|
for(p1 = 3; p1 < N && !found; p1 += 2)
|
||||||
|
@ -26,7 +26,7 @@ def max_prime_factor(num):
|
|||||||
|
|
||||||
# If num is divisible by i and i is prime, find largest
|
# If num is divisible by i and i is prime, find largest
|
||||||
# prime factor of num/i.
|
# prime factor of num/i.
|
||||||
while 1:
|
while True:
|
||||||
if num % i == 0:
|
if num % i == 0:
|
||||||
if is_prime(i):
|
if is_prime(i):
|
||||||
return max_prime_factor(num//i)
|
return max_prime_factor(num//i)
|
||||||
|
@ -33,7 +33,7 @@ def main():
|
|||||||
n = 0
|
n = 0
|
||||||
j = 1
|
j = 1
|
||||||
|
|
||||||
while 1:
|
while True:
|
||||||
tmp = i * j
|
tmp = i * j
|
||||||
n = n + tmp
|
n = n + tmp
|
||||||
j = j + 1
|
j = j + 1
|
||||||
|
@ -8,15 +8,6 @@
|
|||||||
from timeit import default_timer
|
from timeit import default_timer
|
||||||
from projecteuler import is_pandigital, is_prime
|
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():
|
def main():
|
||||||
start = default_timer()
|
start = default_timer()
|
||||||
|
|
||||||
@ -28,7 +19,7 @@ def main():
|
|||||||
i = 7654321
|
i = 7654321
|
||||||
|
|
||||||
while(i > 0):
|
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
|
break
|
||||||
# Skipping the even numbers.
|
# Skipping the even numbers.
|
||||||
i = i - 2
|
i = i - 2
|
||||||
|
@ -35,7 +35,7 @@ def main():
|
|||||||
i = 1
|
i = 1
|
||||||
|
|
||||||
# Brute force approach, try every integer number until the desired one is found.
|
# 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\
|
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):
|
have_same_digits(i, 5*i) and have_same_digits(i, 6*i):
|
||||||
break
|
break
|
||||||
|
@ -18,15 +18,6 @@
|
|||||||
|
|
||||||
from timeit import default_timer
|
from timeit import default_timer
|
||||||
|
|
||||||
def count_digits(n):
|
|
||||||
count = 0
|
|
||||||
|
|
||||||
while n > 0:
|
|
||||||
n = n // 10
|
|
||||||
count = count + 1
|
|
||||||
|
|
||||||
return count
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
start = default_timer()
|
start = default_timer()
|
||||||
|
|
||||||
@ -41,7 +32,7 @@ def main():
|
|||||||
d = n + d
|
d = n + d
|
||||||
n = n + d2
|
n = n + d2
|
||||||
|
|
||||||
if count_digits(n) > count_digits(d):
|
if len(str(n)) > len(str(d)):
|
||||||
count = count + 1
|
count = count + 1
|
||||||
|
|
||||||
end = default_timer()
|
end = default_timer()
|
||||||
|
@ -33,7 +33,7 @@ def main():
|
|||||||
count = 0
|
count = 0
|
||||||
diag = 5
|
diag = 5
|
||||||
|
|
||||||
while 1:
|
while True:
|
||||||
i = i + step
|
i = i + step
|
||||||
|
|
||||||
if is_prime(i):
|
if is_prime(i):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user