diff --git a/c/src/include/macros.h b/c/src/include/macros.h index 455e1013..eee58ab3 100644 --- a/c/src/include/macros.h +++ b/c/src/include/macros.h @@ -101,3 +101,11 @@ #define POW_OF_MAX_POW_10_64 19 #define MAX_POW_10_128 ((uintmax_t) MAX_POW_10_64 * (uintmax_t) MAX_POW_10_64) #define POW_OF_MAX_POW_10_128 38 + +#define PROGRAM_TAIL(type, prob) \ +#ifndef UNITY_END \ +int main(int argc, char const *argv[]) { \ + printf("%" type "\n", prob()); + return 0; \ +} \ +#endif diff --git a/c/src/include/math.h b/c/src/include/math.h index ad136f08..6a3dcbb6 100644 --- a/c/src/include/math.h +++ b/c/src/include/math.h @@ -38,16 +38,14 @@ uintmax_t n_choose_r(uint32_t n, uint32_t r) { for (i = 2; i <= n - r; i++) factors[i] -= 1; // this loop reduces to prime factors only - for (i = n; i > 1; i--) { - for (j = 2; j < i; j++) { + for (i = n; i > 1; i--) + for (j = 2; j < i; j++) if (i % j == 0) { factors[j] += factors[i]; factors[i / j] += factors[i]; factors[i] = 0; break; } - } - } i = j = 2; answer = 1; while (i <= n) { diff --git a/c/src/include/primes.h b/c/src/include/primes.h index 8d6d5896..24a45b90 100644 --- a/c/src/include/primes.h +++ b/c/src/include/primes.h @@ -65,12 +65,11 @@ uintmax_t advance_prime_counter(prime_counter *pc) { } for (uintmax_t p = prime_cache[pc->idx - 1] + 2; p < pc->stop; p += 2) { bool broken = false; - for (size_t idx = 1; idx < prime_cache_idx; idx++) { + for (size_t idx = 1; idx < prime_cache_idx; idx++) if (p % prime_cache[idx] == 0) { // is not prime broken = true; break; } - } if (!broken) { // primeness not determined, exceeded cache uintmax_t root_p = ceil(sqrt(p)); for (uintmax_t c = prime_cache_max; c <= root_p; c += 2) { @@ -81,7 +80,7 @@ uintmax_t advance_prime_counter(prime_counter *pc) { } } if (!broken) { // is prime - if (pc->idx == prime_cache_idx) { + if (pc->idx == prime_cache_idx) #ifdef PRIME_CACHE_SIZE_LIMIT if (prime_cache_size == prime_cache_idx && prime_cache_size < PRIME_CACHE_SIZE_LIMIT) #else @@ -102,7 +101,6 @@ uintmax_t advance_prime_counter(prime_counter *pc) { } else prime_cache[prime_cache_idx++] = p; - } pc->idx++; if ((pc->exhausted = (p >= pc->stop))) return 0; @@ -180,14 +178,13 @@ uintmax_t advance_prime_sieve(prime_sieve *ps) { uintmax_t step; bool candidate_in_sieve = false; size_t candidate_index = -1; - for (size_t i = 0; i < ps->sieve_len * 2; i += 2) { + for (size_t i = 0; i < ps->sieve_len * 2; i += 2) if (ps->sieve[i] == ps->candidate) { step = ps->sieve[i + 1]; candidate_in_sieve = true; candidate_index = i; break; } - } if (!candidate_in_sieve) { if (ps->candidate < ps->prime_squared) { // prime uintmax_t ret = ps->candidate; @@ -204,12 +201,11 @@ uintmax_t advance_prime_sieve(prime_sieve *ps) { do { candidate += step; candidate_in_sieve = false; - for (size_t i = 0; i < ps->sieve_len * 2; i += 2) { + for (size_t i = 0; i < ps->sieve_len * 2; i += 2) if (ps->sieve[i] == candidate) { candidate_in_sieve = true; break; } - } } while (candidate_in_sieve); if (candidate_index != -1) ps->sieve[candidate_index] = candidate; diff --git a/c/src/include/utils.h b/c/src/include/utils.h index e333e6d9..e4a5cc34 100644 --- a/c/src/include/utils.h +++ b/c/src/include/utils.h @@ -53,7 +53,7 @@ char *get_data_file(const char *name) { return NULL; } #else - char* absolute_path = realpath(start, NULL); + char* absolute_path = realpath(start, NULL); if (!absolute_path) { perror("realpath"); return NULL; @@ -90,12 +90,11 @@ char *get_data_file(const char *name) { } const size_t ret_code = fread(buffer, 1, length, file); - if (ret_code != length) { + if (ret_code != length) if (feof(file)) printf("Error reading %s: unexpected end of file, read %" PRIu64 " of %"PRIu64" bytes expected\n", name, (uint64_t)ret_code, (uint64_t)length); else if (ferror(file)) perror("Error reading data file"); - } buffer[length] = 0; fclose(file); diff --git a/c/src/p0000_template.c b/c/src/p0000_template.c index 9afa326c..13243136 100644 --- a/c/src/p0000_template.c +++ b/c/src/p0000_template.c @@ -18,10 +18,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0000() { return 0; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu64 "\n", p0000()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu64, p0000) #endif diff --git a/c/src/p0001.c b/c/src/p0001.c index d46247aa..a363dc9a 100644 --- a/c/src/p0001.c +++ b/c/src/p0001.c @@ -34,10 +34,5 @@ uint32_t EMSCRIPTEN_KEEPALIVE p0001() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu32 "\n", p0001()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu32, p0001) #endif diff --git a/c/src/p0002.c b/c/src/p0002.c index 0b9da30e..b3f1af8d 100644 --- a/c/src/p0002.c +++ b/c/src/p0002.c @@ -33,10 +33,5 @@ uint32_t EMSCRIPTEN_KEEPALIVE p0002() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu32 "\n", p0002()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu32, p0002) #endif diff --git a/c/src/p0003.c b/c/src/p0003.c index f2dd994f..247213d1 100644 --- a/c/src/p0003.c +++ b/c/src/p0003.c @@ -27,10 +27,5 @@ uint16_t EMSCRIPTEN_KEEPALIVE p0003() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu16 "\n", p0003()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu16, p0003) #endif diff --git a/c/src/p0004.c b/c/src/p0004.c index 10e95780..25fffdb5 100644 --- a/c/src/p0004.c +++ b/c/src/p0004.c @@ -22,29 +22,22 @@ uint32_t EMSCRIPTEN_KEEPALIVE p0004() { uint32_t answer = 0, i, j, a, z, prod; bool broken; digit_counter dc; - for (i = 100; i < 1000; i++) { + for (i = 100; i < 1000; i++) for (j = 100; j < 1000; j++) { prod = i * j; dc = digits(prod); broken = false; - for (a = 0, z = dc.idx; a < z; a++, z--) { + for (a = 0, z = dc.idx; a < z; a++, z--) if (dc.digits[a] != dc.digits[z]) { broken = true; break; } - } if (!broken) answer = max(answer, prod); free_digit_counter(dc); } - } return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu32 "\n", p0004()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu32, p0004) #endif diff --git a/c/src/p0005.c b/c/src/p0005.c index 8f3f9782..772982fc 100644 --- a/c/src/p0005.c +++ b/c/src/p0005.c @@ -39,10 +39,5 @@ uint32_t EMSCRIPTEN_KEEPALIVE p0005() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu32 "\n", p0005()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu32, p0005) #endif diff --git a/c/src/p0006.c b/c/src/p0006.c index 8f21f6a4..9c432c0e 100644 --- a/c/src/p0006.c +++ b/c/src/p0006.c @@ -33,10 +33,5 @@ uint32_t EMSCRIPTEN_KEEPALIVE p0006() { return sum * sum - sum_of_squares; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu32 "\n", p0006()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu32, p0006) #endif diff --git a/c/src/p0007.c b/c/src/p0007.c index c06402cc..5238b9fe 100644 --- a/c/src/p0007.c +++ b/c/src/p0007.c @@ -29,10 +29,5 @@ uint32_t EMSCRIPTEN_KEEPALIVE p0007() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu32 "\n", p0007()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu32, p0007) #endif diff --git a/c/src/p0008.c b/c/src/p0008.c index ba2fd6ae..9507a449 100644 --- a/c/src/p0008.c +++ b/c/src/p0008.c @@ -72,10 +72,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0008() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu64 "\n", p0008()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu64, p0008) #endif diff --git a/c/src/p0009.c b/c/src/p0009.c index c0e4fedc..28acd171 100644 --- a/c/src/p0009.c +++ b/c/src/p0009.c @@ -22,7 +22,7 @@ Find the product abc. uint32_t EMSCRIPTEN_KEEPALIVE p0009() { uint32_t answer = 0; - for (uint32_t c = 3; !answer && c < 1000; c++) { + for (uint32_t c = 3; !answer && c < 1000; c++) for (uint32_t b = 2; b < c; b++) { uint32_t a = 1000 - c - b; if (a < b && a*a + b*b == c*c) { @@ -30,14 +30,8 @@ uint32_t EMSCRIPTEN_KEEPALIVE p0009() { break; } } - } return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu32 "\n", p0009()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu32, p0009) #endif diff --git a/c/src/p0010.c b/c/src/p0010.c index 763241b5..90a1da0b 100644 --- a/c/src/p0010.c +++ b/c/src/p0010.c @@ -26,10 +26,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0010() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu64 "\n", p0010()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu64, p0010) #endif diff --git a/c/src/p0011.c b/c/src/p0011.c index afdb99e3..4b55bcb7 100644 --- a/c/src/p0011.c +++ b/c/src/p0011.c @@ -64,7 +64,7 @@ static const uint8_t grid[20][20] = { uint64_t EMSCRIPTEN_KEEPALIVE p0011() { uint64_t answer = 0, tmp; uint8_t i, j; - for (i = 0; i < 20; i++) { + for (i = 0; i < 20; i++) for (j = 0; j < 17; j++) { // horizontal section tmp = grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3]; @@ -73,8 +73,7 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0011() { tmp = grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i]; answer = max(answer, tmp); } - } - for (i = 0; i < 17; i++) { + for (i = 0; i < 17; i++) for (j = 0; j < 17; j++) { // right diagonal section tmp = grid[i][j] * grid[i + 1][j + 1] * grid[i + 2][j + 2] * grid[i + 3][j + 3]; @@ -83,14 +82,8 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0011() { tmp = grid[i][j + 3] * grid[i + 1][j + 2] * grid[i + 2][j + 1] * grid[i + 3][j]; answer = max(answer, tmp); } - } return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu64 "\n", p0011()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu64, p0011) #endif diff --git a/c/src/p0012.c b/c/src/p0012.c index 044b74c5..5cc6ed97 100644 --- a/c/src/p0012.c +++ b/c/src/p0012.c @@ -54,10 +54,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0012() { return -1; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu64 "\n", p0012()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu64, p0012) #endif diff --git a/c/src/p0013.c b/c/src/p0013.c index a0b91fbf..8135c0d8 100644 --- a/c/src/p0013.c +++ b/c/src/p0013.c @@ -237,10 +237,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0013() { return ret; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu64 "\n", p0013()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu64, p0013) #endif diff --git a/c/src/p0014.c b/c/src/p0014.c index 5b47c18d..3ed36f0c 100644 --- a/c/src/p0014.c +++ b/c/src/p0014.c @@ -57,10 +57,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0014() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu64 "\n", p0014()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu64, p0014) #endif diff --git a/c/src/p0015.c b/c/src/p0015.c index 2527a4cc..7de4e749 100644 --- a/c/src/p0015.c +++ b/c/src/p0015.c @@ -28,10 +28,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0015() { return lattice_paths(20, 20); } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu64 "\n", p0015()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu64, p0015) #endif diff --git a/c/src/p0016.c b/c/src/p0016.c index 63f9b836..37c208de 100644 --- a/c/src/p0016.c +++ b/c/src/p0016.c @@ -21,17 +21,10 @@ What is the sum of the digits of the number 21000? uint64_t EMSCRIPTEN_KEEPALIVE p0016() { uint64_t answer = 0; BCD_int power = pow_cuint_cuint(256, 125); - for (size_t i = 0; i < power.bcd_digits; i++) { - answer += power.digits[i] & 0x0F; - answer += power.digits[i] >> 4; - } + for (size_t i = 0; i < power.bcd_digits; i++) + answer += (power.digits[i] & 0x0F) + (power.digits[i] >> 4); return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu64 "\n", p0016()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu64, p0016) #endif diff --git a/c/src/p0017.c b/c/src/p0017.c index f238e710..6e8e6c03 100644 --- a/c/src/p0017.c +++ b/c/src/p0017.c @@ -88,10 +88,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0017() { } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu64 "\n", p0017()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu64, p0017) #endif diff --git a/c/src/p0019.c b/c/src/p0019.c index 1dd6666a..7a6f2b78 100644 --- a/c/src/p0019.c +++ b/c/src/p0019.c @@ -54,16 +54,13 @@ uint16_t EMSCRIPTEN_KEEPALIVE p0019() { systemTime.wMonth = month + 1; systemTime.wDay = 1; - if (!SystemTimeToFileTime(&systemTime, &fileTime)) { + if (!SystemTimeToFileTime(&systemTime, &fileTime)) return -1; - } - if (!FileTimeToSystemTime(&fileTime, &normalizedTime)) { + if (!FileTimeToSystemTime(&fileTime, &normalizedTime)) return -1; - } - if (normalizedTime.wDayOfWeek == 0) { + if (normalizedTime.wDayOfWeek == 0) ++answer; - } #else date.tm_year = year - 1900; date.tm_mon = month; @@ -74,9 +71,8 @@ uint16_t EMSCRIPTEN_KEEPALIVE p0019() { return -1; } - if (date.tm_wday == 0) { + if (date.tm_wday == 0) ++answer; - } #endif } } @@ -84,10 +80,5 @@ uint16_t EMSCRIPTEN_KEEPALIVE p0019() { } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu16 "\n", p0019()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu16, p0019) #endif diff --git a/c/src/p0020.c b/c/src/p0020.c index 563b610b..f9385551 100644 --- a/c/src/p0020.c +++ b/c/src/p0020.c @@ -24,12 +24,11 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0020() { for (uint8_t i = 2; i <= 100; i++) { for (uint8_t j = 0; j < 10; j++) numbers[j] *= i; - for (uint8_t j = 0; j < 9; j++) { + for (uint8_t j = 0; j < 9; j++) if (numbers[j] > ten17) { numbers[j + 1] += numbers[j] / ten17; numbers[j] %= ten17; } - } } uint64_t answer = 0; uint64_t power = 1; @@ -41,10 +40,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0020() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu64 "\n", p0020()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu64, p0020) #endif diff --git a/c/src/p0022.c b/c/src/p0022.c index 97d23fc8..8e5174e3 100644 --- a/c/src/p0022.c +++ b/c/src/p0022.c @@ -51,10 +51,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0022() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu64 "\n", p0022()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu64, p0022) #endif diff --git a/c/src/p0025.c b/c/src/p0025.c index e50c8f90..db966655 100644 --- a/c/src/p0025.c +++ b/c/src/p0025.c @@ -47,10 +47,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0025() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu64 "\n", p0025()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu64, p0025) #endif diff --git a/c/src/p0030.c b/c/src/p0030.c index 4baae34e..a0b7c11a 100644 --- a/c/src/p0030.c +++ b/c/src/p0030.c @@ -39,10 +39,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0030() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu64 "\n", p0030()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu64, p0030) #endif diff --git a/c/src/p0034.c b/c/src/p0034.c index 2ea637cd..868b4418 100644 --- a/c/src/p0034.c +++ b/c/src/p0034.c @@ -34,10 +34,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0034() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu64 "\n", p0034()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu64, p0034) #endif diff --git a/c/src/p0076.c b/c/src/p0076.c index e9ba5549..8491e0a1 100644 --- a/c/src/p0076.c +++ b/c/src/p0076.c @@ -70,10 +70,5 @@ uint32_t EMSCRIPTEN_KEEPALIVE p0076() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%" PRIu32 "\n", p0076()); - return 0; -} -#endif +PROGRAM_TAIL(PRIu32, p0076) #endif diff --git a/c/src/p0836.c b/c/src/p0836.c index a7650ee1..3f0aaaa7 100644 --- a/c/src/p0836.c +++ b/c/src/p0836.c @@ -25,10 +25,5 @@ const char *EMSCRIPTEN_KEEPALIVE p0836() { return "aprilfoolsjoke"; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - printf("%s", p0836()); - return 0; -} -#endif +PROGRAM_TAIL("%s", p0836) #endif diff --git a/c/test.c b/c/test.c index b17568ee..3d7edfe0 100644 --- a/c/test.c +++ b/c/test.c @@ -119,6 +119,9 @@ void test_euler_answer() { break; case STRINGT: sresult = ((char *(*)()) key.func)(); + for (size_t i = 0; sresult[i]; ++i) + if (sresult[i] == '\n') + sresult[i] = 0; snprintf(msg, 256, "Euler problem %u should have an answer of %s, but we actually got %s", key.id, answer.value.string, sresult); TEST_ASSERT_EQUAL_STRING_MESSAGE(answer.value.string, sresult, msg); } diff --git a/cplusplus/src/include/macros.hpp b/cplusplus/src/include/macros.hpp index 4c636635..cca9edbd 100644 --- a/cplusplus/src/include/macros.hpp +++ b/cplusplus/src/include/macros.hpp @@ -73,3 +73,11 @@ #define POW_OF_MAX_POW_10_64 19 #define MAX_POW_10_128 ((uintmax_t) MAX_POW_10_64 * (uintmax_t) MAX_POW_10_64) #define POW_OF_MAX_POW_10_128 38 + +#define PROGRAM_TAIL(prob) \ +#ifndef UNITY_END \ +int main(int argc, char const *argv[]) { \ + std::cout << prob() << std::endl; \ + return 0; \ +} \ +#endif diff --git a/cplusplus/src/include/math.hpp b/cplusplus/src/include/math.hpp index 8d4b4ed3..ac55d34f 100644 --- a/cplusplus/src/include/math.hpp +++ b/cplusplus/src/include/math.hpp @@ -37,16 +37,14 @@ uintmax_t n_choose_r(uint32_t n, uint32_t r) { for (i = 2; i <= n - r; i++) factors[i] -= 1; // this loop reduces to prime factors only - for (i = n; i > 1; i--) { - for (j = 2; j < i; j++) { + for (i = n; i > 1; i--) + for (j = 2; j < i; j++) if (i % j == 0) { factors[j] += factors[i]; factors[i / j] += factors[i]; factors[i] = 0; break; } - } - } i = j = 2; answer = 1; while (i <= n) { diff --git a/cplusplus/src/p0000_template.cpp b/cplusplus/src/p0000_template.cpp index d7abc297..3e834e05 100644 --- a/cplusplus/src/p0000_template.cpp +++ b/cplusplus/src/p0000_template.cpp @@ -17,10 +17,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0000() { return 0; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - std::cout << p0000() << std::endl; - return 0; -} -#endif +PROGRAM_TAIL(p0000) #endif diff --git a/cplusplus/src/p0001.cpp b/cplusplus/src/p0001.cpp index b48a5f6b..8529e8f9 100644 --- a/cplusplus/src/p0001.cpp +++ b/cplusplus/src/p0001.cpp @@ -29,10 +29,6 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0001() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - std::cout << p0001() << std::endl; - return 0; -} -#endif + +PROGRAM_TAIL(p0001) #endif diff --git a/cplusplus/src/p0002.cpp b/cplusplus/src/p0002.cpp index 9877063e..b375dcf3 100644 --- a/cplusplus/src/p0002.cpp +++ b/cplusplus/src/p0002.cpp @@ -37,10 +37,6 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0002() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - std::cout << p0002() << std::endl; - return 0; -} -#endif + +PROGRAM_TAIL(p0002) #endif diff --git a/cplusplus/src/p0004.cpp b/cplusplus/src/p0004.cpp index 66a2b5f6..10bd271a 100644 --- a/cplusplus/src/p0004.cpp +++ b/cplusplus/src/p0004.cpp @@ -20,7 +20,7 @@ Find the largest palindrome made from the product of two 3-digit numbers. uint32_t EMSCRIPTEN_KEEPALIVE p0004() { uint32_t answer = 0, i, j, prod; - for (i = 100; i < 1000; i++) { + for (i = 100; i < 1000; i++) for (j = 100; j < 1000; j++) { prod = i * j; char buf[8] = {}; @@ -33,14 +33,8 @@ uint32_t EMSCRIPTEN_KEEPALIVE p0004() { if (forward == reverse) answer = std::max(answer, prod); } - } return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - std::cout << p0004() << std::endl; - return 0; -} -#endif +PROGRAM_TAIL(p0004) #endif diff --git a/cplusplus/src/p0006.cpp b/cplusplus/src/p0006.cpp index a3f9a72e..a963af01 100644 --- a/cplusplus/src/p0006.cpp +++ b/cplusplus/src/p0006.cpp @@ -32,10 +32,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0006() { return sum * sum - sum_of_squares; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - std::cout << p0006() << std::endl; - return 0; -} -#endif +PROGRAM_TAIL(p0006) #endif diff --git a/cplusplus/src/p0007.cpp b/cplusplus/src/p0007.cpp index 70aaa390..488e89dd 100644 --- a/cplusplus/src/p0007.cpp +++ b/cplusplus/src/p0007.cpp @@ -28,10 +28,5 @@ uint32_t EMSCRIPTEN_KEEPALIVE p0007() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - std::cout << p0007() << std::endl; - return 0; -} -#endif +PROGRAM_TAIL(p0007) #endif diff --git a/cplusplus/src/p0008.cpp b/cplusplus/src/p0008.cpp index 35fd3d83..d027262d 100644 --- a/cplusplus/src/p0008.cpp +++ b/cplusplus/src/p0008.cpp @@ -71,10 +71,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0008() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - std::cout << p0008() << std::endl; - return 0; -} -#endif +PROGRAM_TAIL(p0008) #endif diff --git a/cplusplus/src/p0009.cpp b/cplusplus/src/p0009.cpp index bf57311e..42c7b6c4 100644 --- a/cplusplus/src/p0009.cpp +++ b/cplusplus/src/p0009.cpp @@ -21,22 +21,15 @@ Find the product abc. uint64_t EMSCRIPTEN_KEEPALIVE p0009() { uint64_t answer = 0; - for (uint32_t c = 3; !answer && c < 1000; c++) { - for (uint32_t b = 2; b < c; b++) { + for (uint32_t c = 3; !answer && c < 1000; c++) + for (uint32_t b = 2; b < c; b++) uint32_t a = 1000 - c - b; if (a < b && a*a + b*b == c*c) { answer = (uint64_t) a * b * c; break; } - } - } return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - std::cout << p0009() << std::endl; - return 0; -} -#endif +PROGRAM_TAIL(p0009) #endif diff --git a/cplusplus/src/p0011.cpp b/cplusplus/src/p0011.cpp index 6617f3ea..e6b6ac1a 100644 --- a/cplusplus/src/p0011.cpp +++ b/cplusplus/src/p0011.cpp @@ -63,7 +63,7 @@ static const uint8_t grid[20][20] = { uint64_t EMSCRIPTEN_KEEPALIVE p0011() { uint64_t answer = 0, tmp; uint8_t i, j; - for (i = 0; i < 20; i++) { + for (i = 0; i < 20; i++) for (j = 0; j < 17; j++) { // horizontal section tmp = grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3]; @@ -72,8 +72,7 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0011() { tmp = grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i]; answer = std::max(answer, tmp); } - } - for (i = 0; i < 17; i++) { + for (i = 0; i < 17; i++) for (j = 0; j < 17; j++) { // right diagonal section tmp = grid[i][j] * grid[i + 1][j + 1] * grid[i + 2][j + 2] * grid[i + 3][j + 3]; @@ -82,14 +81,8 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0011() { tmp = grid[i][j + 3] * grid[i + 1][j + 2] * grid[i + 2][j + 1] * grid[i + 3][j]; answer = std::max(answer, tmp); } - } return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - std::cout << p0011() << std::endl; - return 0; -} -#endif +PROGRAM_TAIL(p0011) #endif diff --git a/cplusplus/src/p0013.cpp b/cplusplus/src/p0013.cpp index c93b82d1..aa27e153 100644 --- a/cplusplus/src/p0013.cpp +++ b/cplusplus/src/p0013.cpp @@ -236,10 +236,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0013() { return high; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - std::cout << p0013() << std::endl; - return 0; -} -#endif +PROGRAM_TAIL(p0013) #endif diff --git a/cplusplus/src/p0014.cpp b/cplusplus/src/p0014.cpp index e3647eef..80495ce4 100644 --- a/cplusplus/src/p0014.cpp +++ b/cplusplus/src/p0014.cpp @@ -55,10 +55,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0014() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - std::cout << p0014() << std::endl; - return 0; -} -#endif +PROGRAM_TAIL(p0014) #endif diff --git a/cplusplus/src/p0015.cpp b/cplusplus/src/p0015.cpp index 4b9dceed..2640221e 100644 --- a/cplusplus/src/p0015.cpp +++ b/cplusplus/src/p0015.cpp @@ -27,10 +27,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0015() { return lattice_paths(20, 20); } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - std::cout << p0015() << std::endl; - return 0; -} -#endif +PROGRAM_TAIL(p0015) #endif diff --git a/cplusplus/src/p0016.cpp b/cplusplus/src/p0016.cpp index c5e6c6fc..d2e3b8c3 100644 --- a/cplusplus/src/p0016.cpp +++ b/cplusplus/src/p0016.cpp @@ -22,12 +22,11 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0016() { for (uint16_t i = 0; i < 1000; i++) { for (size_t j = 0; j < numbers.size(); j++) numbers[j] *= 2; - for (size_t j = 0; j < numbers.size() - 1; j++) { + for (size_t j = 0; j < numbers.size() - 1; j++) if (numbers[j] > ten17) { numbers[j + 1] += numbers[j] / ten17; numbers[j] %= ten17; } - } } uint64_t answer = 0; uint64_t power = 1; @@ -39,10 +38,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0016() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - std::cout << p0016() << std::endl; - return 0; -} -#endif +PROGRAM_TAIL(p0016) #endif diff --git a/cplusplus/src/p0017.cpp b/cplusplus/src/p0017.cpp index 7ab358d6..87bb6a2d 100644 --- a/cplusplus/src/p0017.cpp +++ b/cplusplus/src/p0017.cpp @@ -27,9 +27,8 @@ British usage. std::string ToString(uint64_t n); std::string ToString(uint64_t n) { - if (n >= 1000) { + if (n >= 1000) return ToString(n / 1000 % 100) + " thousand"; - } else if (n >= 100) { std::string hundreds = ToString(n / 100 % 10) + " hundred"; if (n % 100) @@ -113,10 +112,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0017() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - std::cout << p0017() << std::endl; - return 0; -} -#endif +PROGRAM_TAIL(p0017) #endif diff --git a/cplusplus/src/p0019.cpp b/cplusplus/src/p0019.cpp index 6571afad..6f00e0b3 100644 --- a/cplusplus/src/p0019.cpp +++ b/cplusplus/src/p0019.cpp @@ -54,27 +54,22 @@ uint16_t EMSCRIPTEN_KEEPALIVE p0019() { systemTime.wMonth = month + 1; systemTime.wDay = 1; - if (!SystemTimeToFileTime(&systemTime, &fileTime)) { + if (!SystemTimeToFileTime(&systemTime, &fileTime)) throw std::runtime_error("SystemTimeToFileTime failed."); - } - if (!FileTimeToSystemTime(&fileTime, &normalizedTime)) { + if (!FileTimeToSystemTime(&fileTime, &normalizedTime)) throw std::runtime_error("FileTimeToSystemTime failed."); - } - if (normalizedTime.wDayOfWeek == 0) { + if (normalizedTime.wDayOfWeek == 0) ++answer; - } #else date.tm_year = year - 1900; date.tm_mon = month; date.tm_mday = 1; - if (std::mktime(&date) == -1) { + if (std::mktime(&date) == -1) throw std::runtime_error("mktime failed to normalize the date."); - } - if (date.tm_wday == 0) { + if (date.tm_wday == 0) ++answer; - } #endif } } @@ -82,10 +77,5 @@ uint16_t EMSCRIPTEN_KEEPALIVE p0019() { } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - std::cout << p0019() << std::endl; - return 0; -} -#endif +PROGRAM_TAIL(p0019) #endif diff --git a/cplusplus/src/p0020.cpp b/cplusplus/src/p0020.cpp index 0edf8451..63d6703f 100644 --- a/cplusplus/src/p0020.cpp +++ b/cplusplus/src/p0020.cpp @@ -42,10 +42,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0020() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - std::cout << p0020() << std::endl; - return 0; -} -#endif +PROGRAM_TAIL(p0020) #endif diff --git a/cplusplus/src/p0022.cpp b/cplusplus/src/p0022.cpp index 22841fa1..0e4665e1 100644 --- a/cplusplus/src/p0022.cpp +++ b/cplusplus/src/p0022.cpp @@ -44,10 +44,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0022() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - std::cout << p0022() << std::endl; - return 0; -} -#endif +PROGRAM_TAIL(p0022) #endif diff --git a/cplusplus/src/p0034.cpp b/cplusplus/src/p0034.cpp index 1632fcd1..261b0d5d 100644 --- a/cplusplus/src/p0034.cpp +++ b/cplusplus/src/p0034.cpp @@ -34,10 +34,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0034() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - std::cout << p0034() << std::endl; - return 0; -} -#endif +PROGRAM_TAIL(p0034) #endif diff --git a/cplusplus/src/p0076.cpp b/cplusplus/src/p0076.cpp index bb568985..2dec759b 100644 --- a/cplusplus/src/p0076.cpp +++ b/cplusplus/src/p0076.cpp @@ -69,10 +69,5 @@ uint32_t EMSCRIPTEN_KEEPALIVE p0076() { return answer; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - std::cout << p0076() << std::endl; - return 0; -} -#endif +PROGRAM_TAIL(p0076) #endif diff --git a/cplusplus/src/p0836.cpp b/cplusplus/src/p0836.cpp index ad2b02b7..4066f880 100644 --- a/cplusplus/src/p0836.cpp +++ b/cplusplus/src/p0836.cpp @@ -23,10 +23,5 @@ std::string EMSCRIPTEN_KEEPALIVE p0836() { return "aprilfoolsjoke"; } -#ifndef UNITY_END -int main(int argc, char const *argv[]) { - std::cout << p0836() << std::endl; - return 0; -} -#endif +PROGRAM_TAIL(p0836) #endif diff --git a/csharp/Euler.Test/Usings.cs b/csharp/Euler.Test/Usings.cs deleted file mode 100644 index cf827197..00000000 --- a/csharp/Euler.Test/Usings.cs +++ /dev/null @@ -1,2 +0,0 @@ -global using Euler; -global using Xunit; \ No newline at end of file diff --git a/csharp/Euler.Test/test.cs b/csharp/Euler.Test/test.cs index 0e8e803b..5d17f948 100644 --- a/csharp/Euler.Test/test.cs +++ b/csharp/Euler.Test/test.cs @@ -1,5 +1,9 @@ using System.Diagnostics; +using Xunit; + +using Euler; + namespace Tests { public class EulerTest @@ -55,10 +59,7 @@ public void EulerTest_Mathematics_Factorial() 2432902008176640000 }; for (byte i = 0; i < results.Count; i += 1) - { - ulong expected = results[i]; - Assert.Equal(expected, Mathematics.Factorial(i)); - } + Assert.Equal(results[i], Mathematics.Factorial(i)); } [Fact] @@ -70,9 +71,8 @@ public void EulerTest_Prime_Primes() var comparison = Prime.Primes().GetEnumerator(); for (byte i = 0; i < results.Count; i += 1) { - long expected = results[i]; comparison.MoveNext(); - Assert.Equal(expected, comparison.Current); + Assert.Equal(results[i], comparison.Current); } } @@ -83,14 +83,12 @@ public void EulerTest_Prime_PrimeFactors() 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 }; foreach (long x in candidates) - { foreach (long y in candidates) { List result = new(Prime.PrimeFactors(x * y)); Assert.Contains(x, result); Assert.Contains(y, result); } - } } [Fact] @@ -118,13 +116,8 @@ public async Task EulerTest_Mathematics_NChooseR() }; List tasks = new(); for (byte i = 0; i < results.Count; i += 1) - { for (byte j = 0; j < results[i].Count; j += 1) - { - ulong expected = results[i][j]; - tasks.Append(Task.Run(() => Assert.Equal(expected, Mathematics.NChooseR(i, j)))); - } - } + tasks.Append(Task.Run(() => Assert.Equal(results[i][j], Mathematics.NChooseR(i, j)))); foreach (Task task in tasks) await task; } diff --git a/csharp/Euler/include/math.cs b/csharp/Euler/include/math.cs index 225fbddb..e461f587 100644 --- a/csharp/Euler/include/math.cs +++ b/csharp/Euler/include/math.cs @@ -31,9 +31,7 @@ public static ulong NChooseR(ulong n, ulong r) // this loop reduces to prime factors only for (i = (uint)n; i > 1; i -= 1) - { for (j = 2; j < i; j += 1) - { if (i % j == 0) { factors[j] += factors[i]; @@ -41,8 +39,6 @@ public static ulong NChooseR(ulong n, ulong r) factors[i] = 0; break; } - } - } i = j = 2; answer = 1; diff --git a/csharp/Euler/include/prime.cs b/csharp/Euler/include/prime.cs index 59f12e51..1f653ad5 100644 --- a/csharp/Euler/include/prime.cs +++ b/csharp/Euler/include/prime.cs @@ -10,9 +10,7 @@ public static class Prime public static IEnumerable Primes(T? stop = null) where T : struct { foreach (dynamic p in _Primes(stop)) - { yield return (T)p; - } } private static IEnumerable _Primes(dynamic? stop = null) @@ -27,22 +25,14 @@ private static IEnumerable _Primes(dynamic? stop = null) // Yield cached values if (stop == null) - { foreach (dynamic p in cache) - { yield return p; - } - } else - { foreach (dynamic p in cache) - { if (p < stop) yield return p; else break; - } - } // Generate new primes if (stop != null && lastCached > stop) @@ -108,9 +98,7 @@ private static IEnumerable ModifiedEratosthenes() public static IEnumerable PrimeFactors(T n) where T : struct { foreach (dynamic f in _PrimeFactors(n)) - { yield return (T)f; - } } private static IEnumerable _PrimeFactors(dynamic n) @@ -121,9 +109,7 @@ private static IEnumerable _PrimeFactors(dynamic n) n = -n; } if (n == 0) - { yield return 0; - } else { dynamic root = (dynamic)Math.Ceiling(Math.Sqrt((double)n)); diff --git a/csharp/Euler/p0004.cs b/csharp/Euler/p0004.cs index 0a127c58..f09b2a37 100644 --- a/csharp/Euler/p0004.cs +++ b/csharp/Euler/p0004.cs @@ -35,14 +35,12 @@ public object Answer() { int answer = 0; for (int v = 101; v < 1000; v++) - { for (int u = 100; u < v; u++) { int p = u * v; if (IsPalindrome(p) && p > answer) answer = p; } - } return answer; } diff --git a/csharp/Euler/p0007.cs b/csharp/Euler/p0007.cs index c3d35d74..fdfd0a88 100644 --- a/csharp/Euler/p0007.cs +++ b/csharp/Euler/p0007.cs @@ -18,11 +18,8 @@ public object Answer() { int i = 0; foreach (long p in Prime.Primes()) - { - if (i == 10000) + if (i++ == 10000) return (int)p; - i++; - } return -1; } } diff --git a/csharp/Euler/p0008.cs b/csharp/Euler/p0008.cs index 64ca2b22..e2c57e0d 100644 --- a/csharp/Euler/p0008.cs +++ b/csharp/Euler/p0008.cs @@ -66,9 +66,8 @@ public object Answer() long answer = 0; for (int i = 0; i < str.Length - 13; i++) { - String slice = str.Substring(i, 13); long prod = 1; - foreach (char c in slice) + foreach (char c in str.Substring(i, 13)) prod *= (long)c - '0'; if (prod > answer) diff --git a/csharp/Euler/p0011.cs b/csharp/Euler/p0011.cs index 24026f26..cd64a38b 100644 --- a/csharp/Euler/p0011.cs +++ b/csharp/Euler/p0011.cs @@ -43,7 +43,6 @@ public object Answer() { int answer = 0, tmp; for (byte i = 0; i < 20; i++) - { for (byte j = 0; j < 17; j++) { // horizontal section @@ -53,9 +52,7 @@ public object Answer() tmp = (int)grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i]; answer = Math.Max(answer, tmp); } - } for (byte i = 0; i < 17; i++) - { for (byte j = 0; j < 17; j++) { // right diagonal section @@ -65,7 +62,6 @@ public object Answer() tmp = (int)grid[i][j + 3] * grid[i + 1][j + 2] * grid[i + 2][j + 1] * grid[i + 3][j]; answer = Math.Max(answer, tmp); } - } return answer; } diff --git a/csharp/Euler/p0016.cs b/csharp/Euler/p0016.cs index b7a25422..d419fa10 100644 --- a/csharp/Euler/p0016.cs +++ b/csharp/Euler/p0016.cs @@ -21,27 +21,20 @@ public object Answer() for (ushort i = 0; i < 1000; i++) { for (byte j = 0; j < 18; j++) - { numbers[j] *= 2; - } for (byte j = 0; j < 17; j++) - { if (numbers[j] > ten17) { numbers[j + 1] += numbers[j] / ten17; numbers[j] %= ten17; } - } } ulong answer = 0; ulong power = 1; for (byte i = 0; i < 19; i++) { for (byte j = 0; j < 18; j++) - { - ulong value = numbers[j] / power; - answer += value % 10; - } + answer += (numbers[j] / power) % 10; power *= 10; } return (short)answer; diff --git a/csharp/Euler/p0017.cs b/csharp/Euler/p0017.cs index 55114b1f..50b8d298 100644 --- a/csharp/Euler/p0017.cs +++ b/csharp/Euler/p0017.cs @@ -27,19 +27,14 @@ public object Answer() { int answer = 0; for (int x = 1; x < 1001; x += 1) - { - string str = to_string(x); - answer += str.Replace(" ", "").Replace("-", "").Length; - } + answer += to_string(x).Replace(" ", "").Replace("-", "").Length; return (short)answer; } static String to_string(int n) { if (n >= 1000) - { return to_string(n / 1000 % 100) + " thousand"; - } else if (n >= 100) { string hundreds = to_string(n / 100 % 10) + " hundred"; diff --git a/csharp/Euler/p0019.cs b/csharp/Euler/p0019.cs index 70d39842..2063bbe6 100644 --- a/csharp/Euler/p0019.cs +++ b/csharp/Euler/p0019.cs @@ -30,16 +30,10 @@ public class p0019 : IEuler public object Answer() { byte answer = 0; - for (ushort x = 1901; x < 2001; x += 1) - { - for (byte y = 1; y < 13; y += 1) - { - if (new DateTime(x, y, 1).DayOfWeek == DayOfWeek.Sunday) - { + for (ushort x = 1901; x < 2001; x += 1) + for (byte y = 1; y < 13; y += 1) + if (new DateTime(x, y, 1).DayOfWeek == DayOfWeek.Sunday) answer += 1; - } - } - } return answer; } } diff --git a/csharp/Euler/p0020.cs b/csharp/Euler/p0020.cs index 4d024613..f3d09b9b 100644 --- a/csharp/Euler/p0020.cs +++ b/csharp/Euler/p0020.cs @@ -24,17 +24,13 @@ public object Answer() for (byte i = 2; i <= 100; i++) { for (byte j = 0; j < 10; j++) - { numbers[j] *= i; - } for (byte j = 0; j < 9; j++) - { if (numbers[j] > ten17) { numbers[j + 1] += numbers[j] / ten17; numbers[j] %= ten17; } - } } ulong answer = 0; ulong power = 1; diff --git a/csharp/Euler/p0022.cs b/csharp/Euler/p0022.cs index 26f0610b..c3f77e9b 100644 --- a/csharp/Euler/p0022.cs +++ b/csharp/Euler/p0022.cs @@ -33,9 +33,7 @@ public object Answer() { int sum = 0; foreach (char c in names[i]) - { sum += c & 0x3F; - } answer += sum * (i + 1); } return answer; diff --git a/csharp/Euler/p0034.cs b/csharp/Euler/p0034.cs index cbaeb357..e446b12f 100644 --- a/csharp/Euler/p0034.cs +++ b/csharp/Euler/p0034.cs @@ -28,13 +28,9 @@ public object Answer() string xs = x.ToString(); uint sum = 0; for (byte i = 0; i < xs.Length; i += 1) - { sum += (uint)Mathematics.Factorial((ulong)(xs[i] - '0')); - } if (sum == x) - { answer += x; - } } return (ushort)answer; } diff --git a/csharp/Euler/p0076.cs b/csharp/Euler/p0076.cs index f9d99ef0..8d38fad4 100644 --- a/csharp/Euler/p0076.cs +++ b/csharp/Euler/p0076.cs @@ -40,8 +40,7 @@ public object Answer() idx = 2; do { - counts[idx] = 0; - idx += 1; + counts[idx++] = 0; counts[idx] += idx; sum = 0; for (byte i = (byte)(idx - 1); i < 101; i += 1) diff --git a/docs/src/c/lib/macros.rst b/docs/src/c/lib/macros.rst index 9bec649d..d995021b 100644 --- a/docs/src/c/lib/macros.rst +++ b/docs/src/c/lib/macros.rst @@ -54,6 +54,12 @@ View source code :source:`c/src/include/macros.h` .. c:macro:: MAX_POW_10_128 POW_OF_MAX_POW_10_128 +.. c:macro:: PROGRAM_TAIL(type, prob) + + Conditionally generates a ``main()`` function if running under the Python test runner or compiling as a standalone + program. Takes in as an argument ``printf()`` formatting argument and the name of the function which implements + this Project Euler solution. + .. c:namespace-pop:: .. literalinclude:: ../../../../c/src/include/macros.h diff --git a/docs/src/cplusplus/lib/macros.rst b/docs/src/cplusplus/lib/macros.rst index 2c6e560f..24167d73 100644 --- a/docs/src/cplusplus/lib/macros.rst +++ b/docs/src/cplusplus/lib/macros.rst @@ -42,6 +42,11 @@ View source code :source:`cplusplus/src/include/macros.hpp` .. c:macro:: MAX_POW_10_128 POW_OF_MAX_POW_10_128 +.. c:macro:: PROGRAM_TAIL(prob) + + Conditionally generates a ``main()`` function if running under the Python test runner or compiling as a standalone + program. Takes in as an argument the name of the function which implements this Project Euler solution. + .. c:namespace-pop:: .. literalinclude:: ../../../../cplusplus/src/include/macros.hpp diff --git a/java/src/main/java/euler/p0004.java b/java/src/main/java/euler/p0004.java index 4ace1bb9..8e644b8c 100644 --- a/java/src/main/java/euler/p0004.java +++ b/java/src/main/java/euler/p0004.java @@ -18,23 +18,21 @@ public class p0004 implements IEuler { private boolean IsPalindrome(int x) { String rep = Integer.toString(x); int length = rep.length(); - for (int i = 0; i < length; i++) { + for (int i = 0; i < length; i++) if (rep.charAt(i) != rep.charAt(length - i - 1)) return false; - } return true; } @Override public Object answer() { int answer = 0; - for (int v = 101; v < 1000; v++) { + for (int v = 101; v < 1000; v++) for (int u = 100; u < v; u++) { int p = u * v; if (IsPalindrome(p) && p > answer) answer = p; } - } return answer; } diff --git a/java/src/main/java/euler/p0008.java b/java/src/main/java/euler/p0008.java index 59ecc748..56593f48 100644 --- a/java/src/main/java/euler/p0008.java +++ b/java/src/main/java/euler/p0008.java @@ -39,25 +39,25 @@ public class p0008 implements IEuler { @Override public Object answer() { String str = ("73167176531330624919225119674426574742355349194934" - + "96983520312774506326239578318016984801869478851843" - + "85861560789112949495459501737958331952853208805511" - + "12540698747158523863050715693290963295227443043557" - + "66896648950445244523161731856403098711121722383113" - + "62229893423380308135336276614282806444486645238749" - + "30358907296290491560440772390713810515859307960866" - + "70172427121883998797908792274921901699720888093776" - + "65727333001053367881220235421809751254540594752243" - + "52584907711670556013604839586446706324415722155397" - + "53697817977846174064955149290862569321978468622482" - + "83972241375657056057490261407972968652414535100474" - + "82166370484403199890008895243450658541227588666881" - + "16427171479924442928230863465674813919123162824586" - + "17866458359124566529476545682848912883142607690042" - + "24219022671055626321111109370544217506941658960408" - + "07198403850962455444362981230987879927244284909188" - + "84580156166097919133875499200524063689912560717606" - + "05886116467109405077541002256983155200055935729725" - + "71636269561882670428252483600823257530420752963450"); + + "96983520312774506326239578318016984801869478851843" + + "85861560789112949495459501737958331952853208805511" + + "12540698747158523863050715693290963295227443043557" + + "66896648950445244523161731856403098711121722383113" + + "62229893423380308135336276614282806444486645238749" + + "30358907296290491560440772390713810515859307960866" + + "70172427121883998797908792274921901699720888093776" + + "65727333001053367881220235421809751254540594752243" + + "52584907711670556013604839586446706324415722155397" + + "53697817977846174064955149290862569321978468622482" + + "83972241375657056057490261407972968652414535100474" + + "82166370484403199890008895243450658541227588666881" + + "16427171479924442928230863465674813919123162824586" + + "17866458359124566529476545682848912883142607690042" + + "24219022671055626321111109370544217506941658960408" + + "07198403850962455444362981230987879927244284909188" + + "84580156166097919133875499200524063689912560717606" + + "05886116467109405077541002256983155200055935729725" + + "71636269561882670428252483600823257530420752963450"); long answer = 0; for (int i = 0; i < str.length() - 13; i++) { String slice = str.substring(i, i + 13); diff --git a/java/src/main/java/euler/p0011.java b/java/src/main/java/euler/p0011.java index 4a59a7cf..f1a200a0 100644 --- a/java/src/main/java/euler/p0011.java +++ b/java/src/main/java/euler/p0011.java @@ -40,7 +40,7 @@ public class p0011 implements IEuler { public Object answer() { int answer = 0, tmp; byte i, j; - for (i = 0; i < 20; i++) { + for (i = 0; i < 20; i++) for (j = 0; j < 17; j++) { // horizontal section tmp = grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3]; @@ -49,8 +49,7 @@ public Object answer() { tmp = grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i]; answer = Math.max(answer, tmp); } - } - for (i = 0; i < 17; i++) { + for (i = 0; i < 17; i++) for (j = 0; j < 17; j++) { // right diagonal section tmp = grid[i][j] * grid[i + 1][j + 1] * grid[i + 2][j + 2] * grid[i + 3][j + 3]; @@ -59,7 +58,6 @@ public Object answer() { tmp = grid[i][j + 3] * grid[i + 1][j + 2] * grid[i + 2][j + 1] * grid[i + 3][j]; answer = Math.max(answer, tmp); } - } return answer; } diff --git a/java/src/main/java/euler/p0016.java b/java/src/main/java/euler/p0016.java index 86c9df04..4c6b4eec 100644 --- a/java/src/main/java/euler/p0016.java +++ b/java/src/main/java/euler/p0016.java @@ -16,23 +16,19 @@ public Object answer() { long ten17 = 100000000000000000L; numbers[0] = 1; for (short i = 0; i < 1000; i++) { - for (byte j = 0; j < 18; j++) { + for (byte j = 0; j < 18; j++) numbers[j] *= 2; - } - for (byte j = 0; j < 17; j++) { + for (byte j = 0; j < 17; j++) if (numbers[j] > ten17) { numbers[j + 1] += numbers[j] / ten17; numbers[j] %= ten17; } - } } long answer = 0; long power = 1; for (byte i = 0; i < 19; i++) { - for (byte j = 0; j < 18; j++) { - long value = numbers[j] / power; - answer += value % 10; - } + for (byte j = 0; j < 18; j++) + answer += (numbers[j] / power) % 10; power *= 10; } return (short) answer; diff --git a/java/src/main/java/euler/p0017.java b/java/src/main/java/euler/p0017.java index 65e0ac28..e92e3169 100644 --- a/java/src/main/java/euler/p0017.java +++ b/java/src/main/java/euler/p0017.java @@ -31,9 +31,9 @@ public Object answer() { } String to_string(int n) { - if (n >= 1000) { + if (n >= 1000) return to_string(n / 1000 % 100) + " thousand"; - } else if (n >= 100) { + else if (n >= 100) { String hundreds = to_string(n / 100 % 10) + " hundred"; if (n % 100 != 0) return hundreds + " and " + to_string(n % 100); diff --git a/java/src/main/java/euler/p0019.java b/java/src/main/java/euler/p0019.java index 19e96ba7..d33c7c7a 100644 --- a/java/src/main/java/euler/p0019.java +++ b/java/src/main/java/euler/p0019.java @@ -30,13 +30,10 @@ public class p0019 implements IEuler { @Override public Object answer() { byte answer = 0; - for (short x = 1901; x < 2001; x++) { - for (byte y = 1; y < 13; y++) { - if (LocalDate.of(x, y, 1).getDayOfWeek() == DayOfWeek.SUNDAY) { + for (short x = 1901; x < 2001; x++) + for (byte y = 1; y < 13; y++) + if (LocalDate.of(x, y, 1).getDayOfWeek() == DayOfWeek.SUNDAY) answer++; - } - } - } return answer; } } \ No newline at end of file diff --git a/java/src/main/java/euler/p0020.java b/java/src/main/java/euler/p0020.java index 2cb55f7c..fb537ba5 100644 --- a/java/src/main/java/euler/p0020.java +++ b/java/src/main/java/euler/p0020.java @@ -23,9 +23,8 @@ public Object answer() { long ten16 = 10000000000000000L; numbers[0] = 1; for (byte i = 2; i <= 100; i++) { - for (byte j = 0; j < 10; j++) { + for (byte j = 0; j < 10; j++) numbers[j] *= i; - } for (byte j = 0; j < 9; j++) { if (numbers[j] > ten16) { numbers[j + 1] += numbers[j] / ten16; @@ -36,10 +35,8 @@ public Object answer() { long answer = 0; long power = 1; for (byte i = 0; i < 19; i++) { - for (byte j = 0; j < 10; j++) { - long value = numbers[j] / power; - answer += value % 10; - } + for (byte j = 0; j < 10; j++) + answer += (numbers[j] / power) % 10; power *= 10; } return (short) answer; diff --git a/java/src/main/java/euler/p0022.java b/java/src/main/java/euler/p0022.java index a7496333..afe2e614 100644 --- a/java/src/main/java/euler/p0022.java +++ b/java/src/main/java/euler/p0022.java @@ -39,9 +39,8 @@ public Object answer() { Arrays.sort(names); for (int i = 0; i < names.length; i += 1) { int sum = 0; - for (int j = 0; j < names[i].length(); j += 1) { + for (int j = 0; j < names[i].length(); j += 1) sum += names[i].charAt(j) & 0x3F; - } answer += sum * (i + 1); } return answer; diff --git a/java/src/test/java/EulerTest.java b/java/src/test/java/EulerTest.java index eb2d73f6..0733fef6 100644 --- a/java/src/test/java/EulerTest.java +++ b/java/src/test/java/EulerTest.java @@ -46,9 +46,8 @@ void eulerTestProblem(Class problemClass, boolean isSlow, Object expected) th Object result = answerMethod.invoke(instance); long elapsedTime = System.nanoTime() - startTime; Assertions.assertEquals(expected, result); - if (!isSlow && !isSemeru()) { + if (!isSlow && !isSemeru()) Assertions.assertTrue(elapsedTime <= ONE_MINUTE_NS, "Test took too long"); - } } public static boolean isSemeru() { diff --git a/javascript/src/p0034.js b/javascript/src/p0034.js index a8874a13..ffd87ccc 100644 --- a/javascript/src/p0034.js +++ b/javascript/src/p0034.js @@ -22,7 +22,7 @@ exports.p0034 = function() { const xs = x.toString(); let sum = 0; for (let i = 0; i < xs.length; i += 1) { - sum += Mathematics.factorial(parseInt(xs[i])); + sum += Mathematics.factorial(Number(xs[i])); } if (sum == x) { answer += x; diff --git a/javascript/src/p0035.js b/javascript/src/p0035.js index 09f3fc89..d1271c4e 100644 --- a/javascript/src/p0035.js +++ b/javascript/src/p0035.js @@ -43,7 +43,7 @@ function* rotations(x) { yield x; const xs = x.toString(); for (let i = 1; i < xs.length; i++) { - yield parseInt(xs.substring(i) + xs.substring(0, i)); + yield Number(xs.substring(i) + xs.substring(0, i)); } } diff --git a/javascript/src/p0076.js b/javascript/src/p0076.js index 297a7e05..ca9db5a2 100644 --- a/javascript/src/p0076.js +++ b/javascript/src/p0076.js @@ -31,8 +31,7 @@ exports.p0076 = function() { answer += 0 | ((100 + counts[2] - sum) / 2); let idx = 2; while (true) { - counts[idx] = 0; - idx += 1; + counts[idx++] = 0; counts[idx] += idx; sum = 0; for (let i = idx - 1; i < 101; i += 1) {