Improve solution for problem 47

This commit is contained in:
daniele 2019-09-28 11:31:06 +02:00
parent ed7031df4d
commit 7489196be8
Signed by: fuxino
GPG Key ID: 6FE25B4A3EE16FDA
2 changed files with 64 additions and 90 deletions

View File

@ -15,88 +15,81 @@
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
#include <time.h> #include <time.h>
#include "projecteuler.h"
#define N 150000 #define N 150000
int count_distinct_factors(int n); int *count_factors(int n);
int *primes;
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int i, found = 0; int i, count, res;
int *factors;
double elapsed; double elapsed;
struct timespec start, end; struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start); clock_gettime(CLOCK_MONOTONIC, &start);
if((primes = sieve(N)) == NULL) if((factors = count_factors(N)) == NULL)
{ {
fprintf(stderr, "Error! Sieve function returned NULL\n"); fprintf(stderr, "Error! Count_factors function returned NULL\n");
return 1; return 1;
} }
/* Starting from 647, count the distinct prime factors of n, n+1, n+2 and n+3. count = 0;
* If they all have 4, the solution is found.*/
for(i = 647; !found && i < N - 3; i++) for(i = 0; i < N; i++)
{ {
if(!primes[i] && !primes[i+1] && !primes[i+2] && !primes[i+3]) if(factors[i] == 4)
{ {
if(count_distinct_factors(i) == 4 && count_distinct_factors(i+1) == 4 && count++;
count_distinct_factors(i+2) == 4 && count_distinct_factors(i+3) == 4) }
{ else
found = 1; {
} count = 0;
}
if(count == 4)
{
res = i - 3;
break;
} }
} }
free(primes);
clock_gettime(CLOCK_MONOTONIC, &end); clock_gettime(CLOCK_MONOTONIC, &end);
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000; elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
printf("Project Euler, Problem 47\n"); printf("Project Euler, Problem 47\n");
printf("Answer: %d\n", i-1); printf("Answer: %d\n", res);
printf("Elapsed time: %.9lf seconds\n", elapsed); printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0; return 0;
} }
int count_distinct_factors(int n) /* Function using a modified sieve of Eratosthenes to count
* the distinct prime factors of each number.*/
int *count_factors(int n)
{ {
int i, count=0; int i = 2, j;
int *factors;
/* Start checking if 2 is a prime factor of n. Then remove if((factors = (int *)calloc(n, sizeof(int))) == NULL)
* all 2s factore.*/
if(n % 2 == 0)
{ {
count++; return NULL;
do
{
n /= 2;
}while(n % 2 == 0);
} }
/* Check all odd numbers i, if they're prime and they're a factor while(i < n / 2)
* of n, count them and then divide n for by i until all factors i
* are eliminated. Stop the loop when n=1, i.e. all factors have
* been found.*/
for(i = 3; n > 1; i += 2)
{ {
if(primes[i] && n % i == 0) if(factors[i] == 0)
{ {
count++; for(j = i; j < n; j += i)
do
{ {
n /= i; factors[j]++;
}while(n % i == 0); }
} }
i++;
} }
return count; return factors;
} }

View File

@ -14,65 +14,46 @@
# Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers? # Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers?
from timeit import default_timer from timeit import default_timer
from projecteuler import sieve
def count_distinct_factors(n): # Function using a modified sieve of Eratosthenes to count
global primes # the prime factors of each number.
count = 0 def count_factors(n):
factors = [0] * n
i = 2
# Start checking if 2 is a prime factor of n. Then remove while i < n // 2:
# all 2s factore. if factors[i] == 0:
if n % 2 == 0: for j in range(i, n, i):
count = count + 1 factors[j] = factors[j] + 1
i = i + 1
while True: return factors
n = n // 2
if n % 2 != 0:
break
i = 3
# Check all odd numbers i, if they're prime and they're a factor
# of n, count them and then divide n for by i until all factors i
# are eliminated. Stop the loop when n=1, i.e. all factors have
# been found.
while n > 1:
if primes[i] == 1 and n % i == 0:
count = count + 1
while True:
n = n // i
if n % i != 0:
break
i = i + 2
return count
def main(): def main():
start = default_timer() start = default_timer()
global primes
N = 150000 N = 150000
primes = sieve(N) factors = count_factors(N)
found = 0
i = 647
# Starting from 647, count the distinct prime factors of n, n+1, n+2 and n+3. count = 0
# If they all have 4, the solution is found.
while not found and i < N - 3: # Find the first instance of four consecutive numbers
if primes[i] == 0 and primes[i+1] == 0 and primes[i+2] == 0 and primes[i+3] == 0: # having four distinct prime factors.
if count_distinct_factors(i) == 4 and count_distinct_factors(i+1) == 4 and count_distinct_factors(i+2) == 4 and count_distinct_factors(i+3) == 4: for i in range(N):
found = 1 if factors[i] == 4:
i = i + 1 count = count + 1
else:
count = 0
if count == 4:
res = i - 3
break
end = default_timer() end = default_timer()
print('Project Euler, Problem 47') print('Project Euler, Problem 47')
print('Answer: {}'.format(i-1)) print('Answer: {}'.format(res))
print('Elapsed time: {:.9f} seconds'.format(end - start)) print('Elapsed time: {:.9f} seconds'.format(end - start))