/* Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5: * * 2^2=4, 2^3=8, 2^4=16, 2^5=32 * 3^2=9, 3^3=27, 3^4=81, 3^5=243 * 4^2=16, 4^3=64, 4^4=256, 4^5=1024 * 5^2=25, 5^3=125, 5^4=625, 5^5=3125 * * If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms: * * 4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125 * * How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?*/ #define _POSIX_C_SOURCE 199309L #include #include #include #include #include "projecteuler.h" int compare(void *a, void *b); int main(int argc, char **argv) { mpz_t a; mpz_t **powers; int i, j, count; double elapsed; struct timespec start, end; clock_gettime(CLOCK_MONOTONIC, &start); if((powers = (mpz_t **)malloc(9801*sizeof(mpz_t *))) == NULL) { fprintf(stderr, "Error while allocating memory\n"); return 1; } for(i = 0; i < 9801; i++) { if((powers[i] = (mpz_t *)malloc(sizeof(mpz_t))) == NULL) { fprintf(stderr, "Error while allocating memory\n"); return 1; } } mpz_init(a); for(i = 0; i < 9801; i++) { mpz_init(*powers[i]); } /* Using the GMP Library to calculate all the powers.*/ for(i = 2; i <= 100; i++) { mpz_set_ui(a, i); for(j = 2; j <= 100; j++) { mpz_pow_ui(*powers[(i-2)*99+j-2], a, j); } } mpz_clear(a); /* Sort the values and count the different values.*/ quick_sort((void **)powers, 0, 9800, compare); count = 1; for(i = 1; i < 9801; i++) { if(mpz_cmp(*powers[i], *powers[i-1])) { count++; } } for(i = 0; i < 9801; i++) { mpz_clear(*powers[i]); free(powers[i]); } free(powers); clock_gettime(CLOCK_MONOTONIC, &end); elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000; printf("Project Euler, Problem 29\n"); printf("Answer: %d\n", count); printf("Elapsed time: %.9lf seconds\n", elapsed); return 0; } int compare(void *a, void *b) { mpz_t *n1, *n2; n1 = (mpz_t *)a; n2 = (mpz_t *)b; return mpz_cmp(*n1, *n2); }