diff --git a/c/src/assertions/x64_assert.c b/c/src/assertions/x64_assert.c index ae3ddf10..e66b8a04 100644 --- a/c/src/assertions/x64_assert.c +++ b/c/src/assertions/x64_assert.c @@ -1,7 +1,7 @@ #include "../include/macros.h" // this file should trigger compilation errors on non-x86 targets -int main(int argc, char const *argv[]) { +int main(int argc, char const *argv[]) { #if CL_COMPILER __asm { aaa; diff --git a/c/src/include/bcd.h b/c/src/include/bcd.h index 2bdfec29..f488cf5d 100644 --- a/c/src/include/bcd.h +++ b/c/src/include/bcd.h @@ -57,7 +57,7 @@ inline void free_BCD_int(BCD_int x) { } } -BCD_int new_BCD_int(uintmax_t a, bool negative) { +BCD_int new_BCD_int(uintmax_t a, bool negative) { BCD_int c; #if !PCC_COMPILER c.decimal_digits = ceil(log10(a + 1)); @@ -68,7 +68,7 @@ BCD_int new_BCD_int(uintmax_t a, bool negative) { c.digits = (packed_BCD_pair *) malloc(sizeof(packed_BCD_pair) * c.bcd_digits); c.negative = negative; c.zero = !a; - for (size_t i = 0; i < c.bcd_digits; i++) { + for (size_t i = 0; i < c.bcd_digits; i++) { c.digits[i] = (((a % 100) / 10) << 4) | (a % 10); a /= 100; } @@ -76,17 +76,17 @@ BCD_int new_BCD_int(uintmax_t a, bool negative) { } BCD_int copy_BCD_int(BCD_int a); -inline BCD_int copy_BCD_int(BCD_int a) { +inline BCD_int copy_BCD_int(BCD_int a) { BCD_int b = a; b.digits = (packed_BCD_pair *) malloc(sizeof(packed_BCD_pair) * b.bcd_digits); memcpy(b.digits, a.digits, b.bcd_digits); return b; } -BCD_int BCD_from_bytes(const unsigned char *str, size_t chars, bool negative, bool little_endian) { +BCD_int BCD_from_bytes(const unsigned char *str, size_t chars, bool negative, bool little_endian) { // converts a bytestring to a little-endian BCD int BCD_int c; - if (!chars || str == NULL) { + if (!chars || str == NULL) { c.zero = true; c.bcd_digits = c.decimal_digits = c.negative = 0; c.digits = NULL; @@ -96,7 +96,7 @@ BCD_int BCD_from_bytes(const unsigned char *str, size_t chars, bool negative, bo c.zero = false; c.negative = negative; c.digits = (packed_BCD_pair *) malloc(sizeof(packed_BCD_pair) * chars); - if (little_endian) { + if (little_endian) { for (i = 0; i < chars; i++) { c.digits[i] = str[i]; } @@ -107,7 +107,7 @@ BCD_int BCD_from_bytes(const unsigned char *str, size_t chars, bool negative, bo c.digits[i] = str[j]; } } - for (i = chars - 1; i != -1; i--) { + for (i = chars - 1; i != -1; i--) { if (c.digits[i] & 0xF0) { c.decimal_digits = i * 2 + 1; c.bcd_digits = i + 1; @@ -119,7 +119,7 @@ BCD_int BCD_from_bytes(const unsigned char *str, size_t chars, bool negative, bo break; } } - if (unlikely(i == -1)) { + if (unlikely(i == -1)) { c.zero = true; c.bcd_digits = c.decimal_digits = c.negative = 0; free(c.digits); @@ -129,12 +129,12 @@ BCD_int BCD_from_bytes(const unsigned char *str, size_t chars, bool negative, bo } BCD_int BCD_from_ascii(const char *str, size_t digits, bool negative); -inline BCD_int BCD_from_ascii(const char *str, size_t digits, bool negative) { +inline BCD_int BCD_from_ascii(const char *str, size_t digits, bool negative) { // packs an ASCII digit string into big-endian bytes, then runs through BCD_from_bytes() size_t length = (digits + 1) / 2, i, j; unsigned char *bytes = (unsigned char *) malloc(sizeof(unsigned char) * length); j = i = digits % 2; - if (i) { + if (i) { bytes[0] = str[0] - '0'; } for (; i < length; i++, j += 2) { @@ -147,15 +147,15 @@ inline BCD_int BCD_from_ascii(const char *str, size_t digits, bool negative) BCD_int sub_bcd(BCD_int x, BCD_int y); -BCD_int add_bcd(BCD_int x, BCD_int y) { +BCD_int add_bcd(BCD_int x, BCD_int y) { // performing this on two n-digit numbers will take O(n) time - if (unlikely(x.zero)) { + if (unlikely(x.zero)) { return copy_BCD_int(y); } - if (unlikely(y.zero)) { + if (unlikely(y.zero)) { return copy_BCD_int(x); } - if (x.negative != y.negative) { + if (x.negative != y.negative) { // if signs don't match, absolute value would go down. // that means we need to flip y's sign and move through sub_bcd() y.negative = !y.negative; @@ -171,11 +171,11 @@ BCD_int add_bcd(BCD_int x, BCD_int y) { for (i = 0; i < min_digits; i++) { a = x.digits[i]; b = y.digits[i]; - if (!(overflow || a)) { + if (!(overflow || a)) { c = b; overflow = false; } - else if (!(overflow || b)) { + else if (!(overflow || b)) { c = a; overflow = false; } @@ -213,17 +213,17 @@ BCD_int add_bcd(BCD_int x, BCD_int y) { #else // otherwise fall back to doing it in C c = a + b; // set c to be the result of (a + b) % 0x100 - if ((overflow = (a > 0x99 - b))) { // if c would overflow the decimal range + if ((overflow = (a > 0x99 - b))) { // if c would overflow the decimal range c += 0x60; // and add 0x60 to make a decimal digit } - if (((a & 0xF) + (b & 0xF)) > 9) { // if the lower nibble be bigger than 9 + if (((a & 0xF) + (b & 0xF)) > 9) { // if the lower nibble be bigger than 9 c += 0x06; // add 6 to make a decimal digit } #endif } z.digits[i] = c; } - if (x.bcd_digits < y.bcd_digits) { + if (x.bcd_digits < y.bcd_digits) { x = y; } for (; overflow && i < max_digits; i++) { // while there's overflow and digits, continue adding @@ -231,7 +231,7 @@ BCD_int add_bcd(BCD_int x, BCD_int y) { if ((a & 0x0F) == 0x0A) { // since all that's left is overflow, we don't need to check ranges a += 0x06; } - if ((overflow = ((a & 0xF0) == 0xA0))) { + if ((overflow = ((a & 0xF0) == 0xA0))) { a += 0x60; } z.digits[i] = a; @@ -241,10 +241,10 @@ BCD_int add_bcd(BCD_int x, BCD_int y) { } z.digits[max_digits] = overflow; z.bcd_digits = max_digits + overflow; - if (overflow) { + if (overflow) { z.decimal_digits = max_digits * 2 + 1; } - else if (z.digits[max_digits - 1] & 0xF0) { + else if (z.digits[max_digits - 1] & 0xF0) { z.decimal_digits = max_digits * 2; } else { @@ -253,11 +253,11 @@ BCD_int add_bcd(BCD_int x, BCD_int y) { return z; } -BCD_int mul_bcd_pow_10(BCD_int x, uintmax_t tens) { +BCD_int mul_bcd_pow_10(BCD_int x, uintmax_t tens) { // this takes O(log_100(x)) time. Note that it's significantly faster if tens is even // returns x * 10^tens BCD_int ret; - if (unlikely(x.zero)) { + if (unlikely(x.zero)) { return new_BCD_int(0, false); } ret.zero = false; @@ -265,7 +265,7 @@ BCD_int mul_bcd_pow_10(BCD_int x, uintmax_t tens) { ret.decimal_digits = x.decimal_digits + tens; ret.bcd_digits = (ret.decimal_digits + 1) / 2; ret.digits = (packed_BCD_pair *) calloc(ret.bcd_digits, sizeof(packed_BCD_pair)); - if (tens % 2 == 0) { + if (tens % 2 == 0) { // +--+--+ +--+--+--+ // |23|01| -> ...|23|01| // +--+--+ +--+--+--+ @@ -282,7 +282,7 @@ BCD_int mul_bcd_pow_10(BCD_int x, uintmax_t tens) { const size_t digit_diff = ret.bcd_digits - x.bcd_digits - ret.decimal_digits % 2; // note that digit_diff needs to be adjusted on this branch, so it can't be common ret.digits[digit_diff] = x.digits[0] << 4; - for (size_t i = 1; i < x.bcd_digits; i++) { + for (size_t i = 1; i < x.bcd_digits; i++) { ret.digits[i + digit_diff] = x.digits[i] << 4; ret.digits[i + digit_diff] |= x.digits[i - 1] >> 4; } @@ -292,11 +292,11 @@ BCD_int mul_bcd_pow_10(BCD_int x, uintmax_t tens) { } BCD_int shift_bcd_left(BCD_int x, uintmax_t tens); -inline BCD_int shift_bcd_left(BCD_int x, uintmax_t tens) { +inline BCD_int shift_bcd_left(BCD_int x, uintmax_t tens) { return mul_bcd_pow_10(x, tens); } -BCD_int mul_bcd_cuint(BCD_int x, uintmax_t y) { +BCD_int mul_bcd_cuint(BCD_int x, uintmax_t y) { // this takes roughly O(log(y) * log(x)) time, and is nearly linear if y is a multiple of 10 // it works by breaking the multiplication down into groups of addition // full formula for performance is something like: @@ -314,7 +314,7 @@ BCD_int mul_bcd_cuint(BCD_int x, uintmax_t y) { y /= 10; ++tens; } - if (tens) { + if (tens) { ret = mul_bcd_pow_10(x, tens); } else { @@ -323,8 +323,8 @@ BCD_int mul_bcd_cuint(BCD_int x, uintmax_t y) { // then for decreasing powers of ten, batch additions uintmax_t p = (sizeof(uintmax_t) == 16) ? MAX_POW_10_128 : MAX_POW_10_64; tens = (sizeof(uintmax_t) == 16) ? POW_OF_MAX_POW_10_128 : POW_OF_MAX_POW_10_64; - for (; p > 1; p /= 10, --tens) { - while (y >= p) { + for (; p > 1; p /= 10, --tens) { + while (y >= p) { mul_by_power_10 = mul_bcd_pow_10(x, tens); sum = add_bcd(ret, mul_by_power_10); free_BCD_int(mul_by_power_10); @@ -343,7 +343,7 @@ BCD_int mul_bcd_cuint(BCD_int x, uintmax_t y) { } BCD_int pow_cuint_cuint(uintmax_t x, uintmax_t y); -inline BCD_int pow_cuint_cuint(uintmax_t x, uintmax_t y) { +inline BCD_int pow_cuint_cuint(uintmax_t x, uintmax_t y) { // this takes roughly O(xylog_100(xy)) time BCD_int answer = new_BCD_int(1, false), tmp; while (y--) { @@ -355,7 +355,7 @@ inline BCD_int pow_cuint_cuint(uintmax_t x, uintmax_t y) { } unsigned short mul_dig_pair(packed_BCD_pair ab, packed_BCD_pair cd); -inline unsigned short mul_dig_pair(packed_BCD_pair ab, packed_BCD_pair cd) { +inline unsigned short mul_dig_pair(packed_BCD_pair ab, packed_BCD_pair cd) { // multiplies two digits pairs and returns an unsigned C short. valid range is 0 thru 9801 unsigned char a, b, c, d; // first unpack the digits @@ -367,7 +367,7 @@ inline unsigned short mul_dig_pair(packed_BCD_pair ab, packed_BCD_pair cd) { return 100 * a * c + 10 * (a * d + b * c) + b * d; } -BCD_int mul_bcd(BCD_int x, BCD_int y) { +BCD_int mul_bcd(BCD_int x, BCD_int y) { // multiplies two BCD ints by breaking them down into their component bytes and adding the results // this takes O(log_100(x) * log_100(y) * log_100(xy)) time BCD_int answer = new_BCD_int(0, false), addend, tmp; @@ -377,10 +377,10 @@ BCD_int mul_bcd(BCD_int x, BCD_int y) { size_t i, j; unsigned short staging; uintmax_t ipow_10 = 0, pow_10; - for (i = 0; i < x.bcd_digits; i++, ipow_10 += 2) { + for (i = 0; i < x.bcd_digits; i++, ipow_10 += 2) { for (j = 0, pow_10 = ipow_10; j < y.bcd_digits; j++, pow_10 += 2) { staging = mul_dig_pair(x.digits[i], y.digits[j]); - if (staging == 0) { + if (staging == 0) { continue; } if (likely(pow_10)) { @@ -401,7 +401,7 @@ BCD_int mul_bcd(BCD_int x, BCD_int y) { return answer; } -BCD_int pow_bcd(BCD_int x, BCD_int y) { +BCD_int pow_bcd(BCD_int x, BCD_int y) { // this takes O(y * 2log_100(x) * log_100(x)^2) time BCD_int answer = new_BCD_int(1, false), tmp, one = new_BCD_int(1, false); while (!y.zero) { @@ -416,16 +416,16 @@ BCD_int pow_bcd(BCD_int x, BCD_int y) { return answer; } -signed char cmp_bcd(BCD_int x, BCD_int y) { +signed char cmp_bcd(BCD_int x, BCD_int y) { // returns: // 1 if x > y // -1 if y > x // else 0 - if (x.negative != y.negative) { + if (x.negative != y.negative) { return (x.negative) ? -1 : 1; } - if (x.decimal_digits != y.decimal_digits) { - if (x.decimal_digits > y.decimal_digits) { + if (x.decimal_digits != y.decimal_digits) { + if (x.decimal_digits > y.decimal_digits) { return (x.negative) ? -1 : 1; } return (x.negative) ? 1 : -1; @@ -439,16 +439,16 @@ signed char cmp_bcd(BCD_int x, BCD_int y) { return 0; } -BCD_int sub_bcd(BCD_int x, BCD_int y) { - if (unlikely(x.zero)) { +BCD_int sub_bcd(BCD_int x, BCD_int y) { + if (unlikely(x.zero)) { BCD_int ret = copy_BCD_int(y); ret.negative = !ret.negative; return ret; } - if (unlikely(y.zero)) { + if (unlikely(y.zero)) { return copy_BCD_int(x); } - if (x.negative != y.negative) { + if (x.negative != y.negative) { // if signs don't match, absolute value would go up. // that means we need to flip y's sign and move through add_bcd() y.negative = !y.negative; @@ -456,7 +456,7 @@ BCD_int sub_bcd(BCD_int x, BCD_int y) { } signed char cmp = cmp_bcd(x, y); BCD_int z; - if ((z.zero = !cmp)) { + if ((z.zero = !cmp)) { return new_BCD_int(0, false); } z.negative = (cmp == -1); @@ -467,11 +467,11 @@ BCD_int sub_bcd(BCD_int x, BCD_int y) { for (i = 0; i < min_digits; i++) { a = x.digits[i]; b = y.digits[i]; - if (!(carry || a)) { + if (!(carry || a)) { c = b; carry = false; } - else if (!(carry || b)) { + else if (!(carry || b)) { c = a; carry = false; } @@ -509,17 +509,17 @@ BCD_int sub_bcd(BCD_int x, BCD_int y) { #else // otherwise fall back to doing it in C c = a - b; // set c to be the result of (a - b) % 0x100 - if ((carry = (c & 0xF0) > 0x99)) { // if c would overflow the decimal range + if ((carry = (c & 0xF0) > 0x99)) { // if c would overflow the decimal range c -= 0x60; // and subtract 0x60 to make a decimal digit } - if ((c & 0x0F) > 9) { // if the lower nibble be bigger than 9 + if ((c & 0x0F) > 9) { // if the lower nibble be bigger than 9 c -= 0x06; // subtract 6 to make a decimal digit } #endif } z.digits[i] = c; } - if (x.bcd_digits < y.bcd_digits) { + if (x.bcd_digits < y.bcd_digits) { x = y; } for (; carry && i < max_digits; i++) { // while there's carry and digits, continue adding @@ -527,7 +527,7 @@ BCD_int sub_bcd(BCD_int x, BCD_int y) { if ((a & 0x0F) == 0x0F) { // since all that's left is carry, we don't need to check ranges a -= 0x06; } - if ((carry = ((a & 0xF0) == 0xF0))) { + if ((carry = ((a & 0xF0) == 0xF0))) { a -= 0x60; } z.digits[i] = a; @@ -537,21 +537,21 @@ BCD_int sub_bcd(BCD_int x, BCD_int y) { } z.bcd_digits = i; z.decimal_digits = z.bcd_digits / 2; - if (!(z.digits[i - 1] & 0xF0)) { + if (!(z.digits[i - 1] & 0xF0)) { z.decimal_digits--; } return z; } -BCD_int div_bcd_pow_10(BCD_int a, uintmax_t tens) { - if (unlikely(a.zero)) { +BCD_int div_bcd_pow_10(BCD_int a, uintmax_t tens) { + if (unlikely(a.zero)) { return copy_BCD_int(a); } BCD_int ret; ret.negative = a.negative; ret.zero = false; ret.decimal_digits = a.decimal_digits - tens; - if (tens % 2 == 0) { + if (tens % 2 == 0) { ret.bcd_digits = a.bcd_digits - tens / 2; ret.digits = (packed_BCD_pair *) malloc(sizeof(packed_BCD_pair) * ret.bcd_digits); memcpy(ret.digits, a.digits + tens / 2, ret.bcd_digits); @@ -563,7 +563,7 @@ BCD_int div_bcd_pow_10(BCD_int a, uintmax_t tens) { } BCD_int shift_bcd_right(BCD_int a, uintmax_t tens); -inline BCD_int shift_bcd_right(BCD_int a, uintmax_t tens) { +inline BCD_int shift_bcd_right(BCD_int a, uintmax_t tens) { return div_bcd_pow_10(a, tens); } @@ -592,8 +592,8 @@ inline intmax_t bcd_to_signed(BCD_int a) { return answer; } -void print_bcd(BCD_int x) { - if (unlikely(x.zero)) { +void print_bcd(BCD_int x) { + if (unlikely(x.zero)) { printf("0"); return; } @@ -602,8 +602,8 @@ void print_bcd(BCD_int x) { } size_t i = x.bcd_digits - 1; printf("%x", x.digits[i]); - if (i--) { - for (; i != -1; i--) { + if (i--) { + for (; i != -1; i--) { printf("%02x", x.digits[i]); } } diff --git a/c/src/include/digits.h b/c/src/include/digits.h index 589df025..5f9209c5 100644 --- a/c/src/include/digits.h +++ b/c/src/include/digits.h @@ -18,14 +18,14 @@ struct digit_counter { size_t idx; }; -unsigned char advance_digit_counter(digit_counter *dc) { +unsigned char advance_digit_counter(digit_counter *dc) { IterationHead(dc); unsigned char ret = dc->digits[dc->idx--]; dc->exhausted = (dc->idx == -1); return ret; } -digit_counter digits(uintmax_t n) { +digit_counter digits(uintmax_t n) { digit_counter ret; IteratorInitHead(ret, advance_digit_counter); #if !PCC_COMPILER @@ -34,7 +34,7 @@ digit_counter digits(uintmax_t n) { size_t digit_len = imprecise_log10(n + 1); #endif ret.digits = (unsigned char *) malloc(digit_len * sizeof(unsigned char)); - for (size_t i = 0; i < digit_len; i++) { + for (size_t i = 0; i < digit_len; i++) { ret.digits[i] = n % 10; n /= 10; } @@ -42,8 +42,8 @@ digit_counter digits(uintmax_t n) { return ret; } -void free_digit_counter(digit_counter dc) { - if (dc.digits != NULL) { +void free_digit_counter(digit_counter dc) { + if (dc.digits != NULL) { free(dc.digits); } } diff --git a/c/src/include/factors.h b/c/src/include/factors.h index b17159e2..e9c6339d 100644 --- a/c/src/include/factors.h +++ b/c/src/include/factors.h @@ -13,9 +13,9 @@ struct factor_counter { uintmax_t advance_factor_counter(factor_counter *fc); inline uintmax_t advance_factor_counter(factor_counter *fc) { IterationHead(fc); - while (fc->target > fc->current) { + while (fc->target > fc->current) { fc->current++; - if (fc->target % fc->current == 0) { + if (fc->target % fc->current == 0) { fc->exhausted = (fc->target == fc->current); return fc->current; } @@ -36,7 +36,7 @@ uintmax_t proper_divisor_count(uintmax_t target); inline uintmax_t proper_divisor_count(uintmax_t target) { uintmax_t ret = 0; factor_counter fc = proper_divisors(target); - while (!fc.exhausted) { + while (!fc.exhausted) { next(fc); ret++; } diff --git a/c/src/include/fibonacci.h b/c/src/include/fibonacci.h index ae763fc5..fb775891 100644 --- a/c/src/include/fibonacci.h +++ b/c/src/include/fibonacci.h @@ -11,7 +11,7 @@ struct fibonacci { uintmax_t limit; }; -uintmax_t advance_fibonacci(fibonacci *fib) { +uintmax_t advance_fibonacci(fibonacci *fib) { if (fib->exhausted) { return 0; } @@ -23,7 +23,7 @@ uintmax_t advance_fibonacci(fibonacci *fib) { return fib->a; } -fibonacci fibonacci1(uintmax_t limit) { +fibonacci fibonacci1(uintmax_t limit) { fibonacci ret; IteratorInitHead(ret, advance_fibonacci); ret.a = 0; @@ -33,7 +33,7 @@ fibonacci fibonacci1(uintmax_t limit) { } fibonacci fibonacci0(); -inline fibonacci fibonacci0() { +inline fibonacci fibonacci0() { return fibonacci1(-1); } diff --git a/c/src/include/iterator.h b/c/src/include/iterator.h index f5f89535..3651b7e9 100644 --- a/c/src/include/iterator.h +++ b/c/src/include/iterator.h @@ -83,7 +83,7 @@ struct counter { }; uintmax_t iterate_counter(counter *i); -inline uintmax_t iterate_counter(counter *i) { +inline uintmax_t iterate_counter(counter *i) { /** * The function to advance a counter * @i the counter you want to advance @@ -94,7 +94,7 @@ inline uintmax_t iterate_counter(counter *i) { uintmax_t ret = i->idx; intmax_t step = i->step; i->idx += step; - if ((step > 0 && i->idx >= i->stop) || (step < 0 && i->idx <= i->stop)) { + if ((step > 0 && i->idx >= i->stop) || (step < 0 && i->idx <= i->stop)) { i->exhausted = 1; } return ret; @@ -119,7 +119,7 @@ inline counter counter3(uintmax_t start, uintmax_t stop, intmax_t step) { } counter counter2(uintmax_t start, uintmax_t stop); -inline counter counter2(uintmax_t start, uintmax_t stop) { +inline counter counter2(uintmax_t start, uintmax_t stop) { /** * The simpler constructor for the counter iterator * @start: The beginning position of the counter @@ -131,7 +131,7 @@ inline counter counter2(uintmax_t start, uintmax_t stop) { } counter counter1(uintmax_t stop); -inline counter counter1(uintmax_t stop) { +inline counter counter1(uintmax_t stop) { /** * The simplest constructor for the counter iterator * @stop: The point where the counter is exhausted diff --git a/c/src/include/math.h b/c/src/include/math.h index 9279829f..8190a967 100644 --- a/c/src/include/math.h +++ b/c/src/include/math.h @@ -10,7 +10,7 @@ #include uintmax_t factorial(unsigned int n); -inline uintmax_t factorial(unsigned int n) { +inline uintmax_t factorial(unsigned int n) { // note that this function only works for numbers smaller than MAX_FACTORIAL_64 if ((sizeof(uintmax_t) == 8 && n > MAX_FACTORIAL_64) || (sizeof(uintmax_t) == 16 && n > MAX_FACTORIAL_128)) return -1; @@ -21,9 +21,9 @@ inline uintmax_t factorial(unsigned int n) { return ret; } -uintmax_t n_choose_r(unsigned int n, unsigned int r) { +uintmax_t n_choose_r(unsigned int n, unsigned int r) { // function returns -1 if it overflows - if ((sizeof(uintmax_t) == 8 && n <= MAX_FACTORIAL_64) || (sizeof(uintmax_t) == 16 && n <= MAX_FACTORIAL_128)) { + if ((sizeof(uintmax_t) == 8 && n <= MAX_FACTORIAL_64) || (sizeof(uintmax_t) == 16 && n <= MAX_FACTORIAL_128)) { // fast path if small enough return factorial(n) / factorial(r) / factorial(n-r); } @@ -56,27 +56,27 @@ uintmax_t n_choose_r(unsigned int n, unsigned int r) { } i = j = 2; answer = 1; - while (i <= n) { - while (factors[i] > 0) { + while (i <= n) { + while (factors[i] > 0) { tmp = answer; answer *= i; - while (answer < tmp && j <= n) { - while (factors[j] < 0) { + while (answer < tmp && j <= n) { + while (factors[j] < 0) { tmp /= j; factors[j]++; } j++; answer = tmp * i; } - if (answer < tmp) { + if (answer < tmp) { return -1; // this indicates an overflow } factors[i]--; } i++; } - while (j <= n) { - while (factors[j] < 0) { + while (j <= n) { + while (factors[j] < 0) { answer /= j; factors[j]++; } @@ -88,9 +88,9 @@ uintmax_t n_choose_r(unsigned int n, unsigned int r) { #if PCC_COMPILER unsigned char imprecise_log10(uintmax_t x); - inline unsigned char imprecise_log10(uintmax_t x) { + inline unsigned char imprecise_log10(uintmax_t x) { unsigned char answer = 0; - while (x) { + while (x) { x /= 10; ++answer; } @@ -98,7 +98,7 @@ uintmax_t n_choose_r(unsigned int n, unsigned int r) { } double sqrt(double S); - inline double sqrt(double S) { + inline double sqrt(double S) { // implements the Bakhshali method of square root computation to fix a PCC error double a, x = S / 2; unsigned int i; @@ -110,9 +110,9 @@ uintmax_t n_choose_r(unsigned int n, unsigned int r) { } uintmax_t ceil(double x); - inline uintmax_t ceil(double x) { + inline uintmax_t ceil(double x) { uintmax_t ret = (uintmax_t) x; - if (x == (double) ret) { + if (x == (double) ret) { return ret; } return ret + 1; diff --git a/c/src/include/primes.h b/c/src/include/primes.h index 312d1534..036137ce 100644 --- a/c/src/include/primes.h +++ b/c/src/include/primes.h @@ -49,7 +49,7 @@ uintmax_t advance_prime_counter(prime_counter *pc) { * Returns the next number in the iteration */ IterationHead(pc); - if (!prime_cache_size) { // if not already done, initialize the prime cache + if (!prime_cache_size) { // if not already done, initialize the prime cache prime_cache = (uintmax_t *) malloc(sizeof(uintmax_t) * 4); prime_cache[0] = 2; prime_cache[1] = 3; @@ -58,45 +58,45 @@ uintmax_t advance_prime_counter(prime_counter *pc) { prime_cache_size = 4; prime_cache_idx = 4; } - if (pc->idx < prime_cache_idx) { + if (pc->idx < prime_cache_idx) { uintmax_t p = prime_cache[pc->idx++]; - if ((pc->exhausted = (p >= pc->stop))) { + if ((pc->exhausted = (p >= pc->stop))) { return 0; } return p; } 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++) { - if (p % prime_cache[idx] == 0) { // is not prime + 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 + 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) { + for (uintmax_t c = prime_cache_max; c <= root_p; c += 2) { if (p % c == 0) { // is not prime broken = true; break; } } } - if (!broken) { // is prime + if (!broken) { // is prime 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) { + if (prime_cache_size == prime_cache_idx && prime_cache_size < PRIME_CACHE_SIZE_LIMIT) { #else - if (prime_cache_size == prime_cache_idx) { + if (prime_cache_size == prime_cache_idx) { #endif size_t new_size = prime_cache_size * 2; #ifdef PRIME_CACHE_SIZE_LIMIT - if (new_size > PRIME_CACHE_SIZE_LIMIT) { + if (new_size > PRIME_CACHE_SIZE_LIMIT) { new_size = PRIME_CACHE_SIZE_LIMIT; } #endif void *tmp = realloc(prime_cache, new_size * sizeof(uintmax_t)); - if (tmp != NULL) { + if (tmp != NULL) { prime_cache = (uintmax_t *) tmp; prime_cache_size = new_size; prime_cache[prime_cache_idx++] = prime_cache_max = p; @@ -106,7 +106,7 @@ uintmax_t advance_prime_counter(prime_counter *pc) { } } pc->idx++; - if ((pc->exhausted = (p >= pc->stop))) { + if ((pc->exhausted = (p >= pc->stop))) { return 0; } return p; @@ -116,7 +116,7 @@ uintmax_t advance_prime_counter(prime_counter *pc) { return 0; } -prime_counter prime_counter1(uintmax_t stop) { +prime_counter prime_counter1(uintmax_t stop) { /** * The base constructor for the prime number generator * @stop: The point where the counter is exhausted @@ -132,7 +132,7 @@ prime_counter prime_counter1(uintmax_t stop) { } prime_counter prime_counter0(); -inline prime_counter prime_counter0() { +inline prime_counter prime_counter0() { /** * The simplest constructor for the prime number generator * @@ -179,20 +179,20 @@ uintmax_t advance_prime_sieve(prime_sieve *ps) { return 2; } // if candidate in sieve - while (true) { + while (true) { 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) { - if (ps->sieve[i] == ps->candidate) { + 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 + if (!candidate_in_sieve) { + if (ps->candidate < ps->prime_squared) { // prime uintmax_t ret = ps->candidate; ps->candidate += 2; return ret; @@ -207,14 +207,14 @@ 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) { - if (ps->sieve[i] == candidate) { + 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) { + if (candidate_index != -1) { ps->sieve[candidate_index] = candidate; } else { ps->sieve_len++; @@ -225,7 +225,7 @@ uintmax_t advance_prime_sieve(prime_sieve *ps) { } } -prime_sieve prime_sieve0() { +prime_sieve prime_sieve0() { /** * The constructor for the prime number sieve * @@ -245,15 +245,15 @@ prime_sieve prime_sieve0() { } void free_prime_counter(prime_counter pc); -void free_prime_sieve(prime_sieve ps) { +void free_prime_sieve(prime_sieve ps) { free_prime_counter(ps.source); - if (ps.sieve != NULL) { + if (ps.sieve != NULL) { free(ps.sieve); } } -void free_prime_counter(prime_counter pc) { - if (pc.ps != NULL) { +void free_prime_counter(prime_counter pc) { + if (pc.ps != NULL) { free_prime_sieve(*pc.ps); free(pc.ps); } @@ -279,7 +279,7 @@ struct prime_factor_counter { prime_counter pc; }; -uintmax_t advance_prime_factor_counter(prime_factor_counter *pfc) { +uintmax_t advance_prime_factor_counter(prime_factor_counter *pfc) { /** * The function to advance a prime factor iterator * @i the counter you want to advance @@ -287,7 +287,7 @@ uintmax_t advance_prime_factor_counter(prime_factor_counter *pfc) { * Returns the next number in the iteration */ while (pfc->target != 0 && pfc->target != 1 && !pfc->pc.exhausted) { - if (pfc->target % pfc->current == 0) { + if (pfc->target % pfc->current == 0) { pfc->target /= pfc->current; pfc->exhausted = (pfc->target == 1); return pfc->current; @@ -298,7 +298,7 @@ uintmax_t advance_prime_factor_counter(prime_factor_counter *pfc) { return -1; } -prime_factor_counter prime_factors(uintmax_t n) { +prime_factor_counter prime_factors(uintmax_t n) { /** * The base constructor for the prime factors iterator * @n: The non-zero number you wish to factor @@ -318,26 +318,26 @@ prime_factor_counter prime_factors(uintmax_t n) { #define free_prime_factor_counter(pfc) free_prime_counter(pfc.pc) -uintmax_t is_composite(uintmax_t n) { +uintmax_t is_composite(uintmax_t n) { /** * Tells you if a number is composite, and if so, its smallest prime factor * @n: The number you wish to test * * See prime_factor_counter */ - if (!n || n == 1) { + if (!n || n == 1) { return 0; } prime_factor_counter iter = prime_factors(n); uintmax_t ret = next(iter); - if (ret == n) { + if (ret == n) { return 0; } return ret; } bool is_prime(uintmax_t n); -inline bool is_prime(uintmax_t n) { +inline bool is_prime(uintmax_t n) { /** * Tells you if a number is prime * @n: The number you wish to test diff --git a/c/src/p0000_template.c b/c/src/p0000_template.c index 8e08e86a..d41d3739 100644 --- a/c/src/p0000_template.c +++ b/c/src/p0000_template.c @@ -16,9 +16,8 @@ unsigned long long p0000() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { - unsigned long long answer = p0000(); - printf("%llu\n", answer); +int main(int argc, char const *argv[]) { + printf("%llu\n", p0000()); return 0; } #endif diff --git a/c/src/p0001.c b/c/src/p0001.c index bd0f6356..6ee27a1c 100644 --- a/c/src/p0001.c +++ b/c/src/p0001.c @@ -20,24 +20,23 @@ unsigned long long p0001() { unsigned long long answer = 0; counter c = counter3(0, 1000, 3); - while (!c.exhausted) { + while (!c.exhausted) { answer += next(c); } c = counter3(0, 1000, 5); - while (!c.exhausted) { + while (!c.exhausted) { answer += next(c); } c = counter3(0, 1000, 15); - while (!c.exhausted) { + while (!c.exhausted) { answer -= next(c); } return answer; } #ifndef UNITY_END -int main(int argc, char const *argv[]) { - unsigned long long answer = p0001(); - printf("%llu\n", answer); +int main(int argc, char const *argv[]) { + printf("%llu\n", p0001()); return 0; } #endif diff --git a/c/src/p0002.c b/c/src/p0002.c index 985b3d98..922e0412 100644 --- a/c/src/p0002.c +++ b/c/src/p0002.c @@ -22,7 +22,7 @@ four million, find the sum of the even-valued terms. unsigned long long p0002() { unsigned long long answer = 0; fibonacci fib = fibonacci1(3999999); - while (!fib.exhausted) { + while (!fib.exhausted) { next(fib); // odd (1, 3, 13, 55, ...) next(fib); // odd (1, 5, 21, 89, ...) answer += next(fib); // even (2, 8, 34, 144, ...) @@ -31,9 +31,8 @@ unsigned long long p0002() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { - unsigned long long answer = p0002(); - printf("%llu\n", answer); +int main(int argc, char const *argv[]) { + printf("%llu\n", p0002()); return 0; } #endif diff --git a/c/src/p0003.c b/c/src/p0003.c index 2d814e56..4b838a6c 100644 --- a/c/src/p0003.c +++ b/c/src/p0003.c @@ -18,7 +18,7 @@ What is the largest prime factor of the number 600851475143 ? unsigned long long p0003() { unsigned long long answer = 0; prime_factor_counter pfc = prime_factors(600851475143); - while (!pfc.exhausted) { + while (!pfc.exhausted) { answer = next(pfc); } free_prime_factor_counter(pfc); @@ -26,9 +26,8 @@ unsigned long long p0003() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { - unsigned long long answer = p0003(); - printf("%llu\n", answer); +int main(int argc, char const *argv[]) { + printf("%llu\n", p0003()); return 0; } #endif diff --git a/c/src/p0004.c b/c/src/p0004.c index 19da03ca..473badbc 100644 --- a/c/src/p0004.c +++ b/c/src/p0004.c @@ -19,18 +19,18 @@ unsigned int p0004() { unsigned int answer = 0, i, j, a, z, prod; bool broken; digit_counter dc; - for (i = 100; i < 1000; i++) { - for (j = 100; j < 1000; j++) { + 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--) { - if (dc.digits[a] != dc.digits[z]) { + for (a = 0, z = dc.idx; a < z; a++, z--) { + if (dc.digits[a] != dc.digits[z]) { broken = true; break; } } - if (!broken) { + if (!broken) { answer = max(answer, prod); } free_digit_counter(dc); @@ -40,9 +40,8 @@ unsigned int p0004() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { - unsigned int answer = p0004(); - printf("%u", answer); +int main(int argc, char const *argv[]) { + printf("%u", p0004()); return 0; } #endif diff --git a/c/src/p0005.c b/c/src/p0005.c index 3d2dda1e..409b85ec 100644 --- a/c/src/p0005.c +++ b/c/src/p0005.c @@ -21,19 +21,19 @@ unsigned long long p0005() { unsigned long long answer = 1; unsigned char factor_tracker[20] = {0}, local_factor_tracker[20] = {0}; prime_factor_counter pfc; - for (unsigned char i = 2; i < 21; i++) { + for (unsigned char i = 2; i < 21; i++) { pfc = prime_factors(i); - while (!pfc.exhausted) { + while (!pfc.exhausted) { local_factor_tracker[next(pfc)]++; } - for (unsigned char i = 2; i < 20; i++) { + for (unsigned char i = 2; i < 20; i++) { factor_tracker[i] = max(factor_tracker[i], local_factor_tracker[i]); local_factor_tracker[i] = 0; } free_prime_factor_counter(pfc); } - for (unsigned char i = 2; i < 20; i++) { - for (unsigned char j = 0; j < factor_tracker[i]; j++) { + for (unsigned char i = 2; i < 20; i++) { + for (unsigned char j = 0; j < factor_tracker[i]; j++) { answer *= i; } } @@ -41,9 +41,8 @@ unsigned long long p0005() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { - unsigned long long answer = p0005(); - printf("%llu", answer); +int main(int argc, char const *argv[]) { + printf("%llu", p0005()); return 0; } #endif diff --git a/c/src/p0006.c b/c/src/p0006.c index 2880b009..2e93c831 100644 --- a/c/src/p0006.c +++ b/c/src/p0006.c @@ -25,16 +25,15 @@ natural numbers and the square of the sum. unsigned long long p0006() { unsigned long long sum = 100 * 101 / 2, sum_of_squares = 0; - for (unsigned long long i = 1; i < 101; i++) { + for (unsigned long long i = 1; i < 101; i++) { sum_of_squares += i * i; } return sum * sum - sum_of_squares; } #ifndef UNITY_END -int main(int argc, char const *argv[]) { - unsigned long long answer = p0006(); - printf("%llu", answer); +int main(int argc, char const *argv[]) { + printf("%llu", p0006()); return 0; } #endif diff --git a/c/src/p0007.c b/c/src/p0007.c index 6dd575ac..17e58792 100644 --- a/c/src/p0007.c +++ b/c/src/p0007.c @@ -27,9 +27,8 @@ unsigned long long p0007() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { - unsigned long long answer = p0007(); - printf("%llu", answer); +int main(int argc, char const *argv[]) { + printf("%llu", p0007()); return 0; } #endif diff --git a/c/src/p0008.c b/c/src/p0008.c index a8e29a13..5157a017 100644 --- a/c/src/p0008.c +++ b/c/src/p0008.c @@ -59,12 +59,12 @@ unsigned long long p0008() { "05886116467109405077541002256983155200055935729725" "71636269561882670428252483600823257530420752963450"); char digits[1000]; - for (i = 0; i < 1000; i++) { + for (i = 0; i < 1000; i++) { digits[i] = plain_digits[i] - 0x30; } for (i = 0; i < 1000 - 13; i++) { tmp = digits[i]; - for (j = i + 1; j < i + 13; j++) { + for (j = i + 1; j < i + 13; j++) { tmp *= digits[j]; } answer = max(answer, tmp); @@ -73,9 +73,8 @@ unsigned long long p0008() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { - unsigned long long answer = p0008(); - printf("%llu", answer); +int main(int argc, char const *argv[]) { + printf("%llu", p0008()); return 0; } #endif diff --git a/c/src/p0009.c b/c/src/p0009.c index 6b79d5dd..dbc3c98c 100644 --- a/c/src/p0009.c +++ b/c/src/p0009.c @@ -19,10 +19,10 @@ Find the product abc. unsigned long long p0009() { unsigned long long answer = 0; - for (unsigned int c = 3; !answer && c < 1000; c++) { - for (unsigned int b = 2; b < c; b++) { + for (unsigned int c = 3; !answer && c < 1000; c++) { + for (unsigned int b = 2; b < c; b++) { unsigned int a = 1000 - c - b; - if (a < b && a*a + b*b == c*c) { + if (a < b && a*a + b*b == c*c) { answer = (unsigned long long) a * b * c; break; } @@ -32,9 +32,8 @@ unsigned long long p0009() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { - unsigned long long answer = p0009(); - printf("%llu", answer); +int main(int argc, char const *argv[]) { + printf("%llu", p0009()); return 0; } #endif diff --git a/c/src/p0010.c b/c/src/p0010.c index 34793087..1b352856 100644 --- a/c/src/p0010.c +++ b/c/src/p0010.c @@ -17,7 +17,7 @@ Find the sum of all the primes below two million. unsigned long long p0010() { unsigned long long tmp, answer = 0; prime_sieve ps = prime_sieve0(); - while ((tmp = next(ps)) < 2000000) { + while ((tmp = next(ps)) < 2000000) { answer += tmp; } free_prime_sieve(ps); @@ -25,9 +25,8 @@ unsigned long long p0010() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { - unsigned long long answer = p0010(); - printf("%llu", answer); +int main(int argc, char const *argv[]) { + printf("%llu", p0010()); return 0; } #endif diff --git a/c/src/p0011.c b/c/src/p0011.c index b66cf7bd..fcab4d4f 100644 --- a/c/src/p0011.c +++ b/c/src/p0011.c @@ -62,8 +62,8 @@ static const unsigned char grid[20][20] = { unsigned long long p0011() { unsigned long answer = 0, tmp; unsigned char i, j; - for (i = 0; i < 20; i++) { - for (j = 0; j < 17; j++) { + 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]; answer = max(answer, tmp); @@ -72,8 +72,8 @@ unsigned long long p0011() { answer = max(answer, tmp); } } - for (i = 0; i < 17; i++) { - for (j = 0; j < 17; j++) { + 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]; answer = max(answer, tmp); @@ -86,9 +86,8 @@ unsigned long long p0011() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { - unsigned long long answer = p0011(); - printf("%llu", answer); +int main(int argc, char const *argv[]) { + printf("%llu", p0011()); return 0; } #endif diff --git a/c/src/p0012.c b/c/src/p0012.c index d8c13af6..da34f431 100644 --- a/c/src/p0012.c +++ b/c/src/p0012.c @@ -34,7 +34,7 @@ divisors? unsigned long long p0012() { unsigned long long current = 1; unsigned int i = 2; - while (true) { + while (true) { current += i; // 3, 21, ... ++i; current += i; // 6, 28, ... @@ -52,9 +52,8 @@ unsigned long long p0012() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { - unsigned long long answer = p0012(); - printf("%llu", answer); +int main(int argc, char const *argv[]) { + printf("%llu", p0012()); return 0; } #endif diff --git a/c/src/p0013.c b/c/src/p0013.c index e565d285..5b553369 100644 --- a/c/src/p0013.c +++ b/c/src/p0013.c @@ -220,7 +220,7 @@ static const char numbers[100][50] = { unsigned long long p0013() { BCD_int answer, tmp1, tmp2 = new_BCD_int(0, false); uintmax_t ret; - for (size_t i = 0; i < 100; i++) { + for (size_t i = 0; i < 100; i++) { tmp1 = BCD_from_ascii(numbers[i], 50, false); answer = add_bcd(tmp1, tmp2); free_BCD_int(tmp1); @@ -235,9 +235,8 @@ unsigned long long p0013() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { - unsigned long long answer = p0013(); - printf("%llu\n", answer); +int main(int argc, char const *argv[]) { + printf("%llu\n", p0013()); return 0; } #endif diff --git a/c/src/p0014.c b/c/src/p0014.c index b8e9c4d6..bad1f006 100644 --- a/c/src/p0014.c +++ b/c/src/p0014.c @@ -30,12 +30,12 @@ static unsigned int collatz_len_cache[CACHE_SIZE] = {0, 1, 0}; unsigned int collatz_len(unsigned long long n); -unsigned int collatz_len(unsigned long long n) { +unsigned int collatz_len(unsigned long long n) { if (n < CACHE_SIZE && collatz_len_cache[n]) { return collatz_len_cache[n]; } unsigned int ret = 0; - if (n % 2) { + if (n % 2) { ret = 2 + collatz_len((3 * n + 1) / 2); } else { ret = 1 + collatz_len(n / 2); @@ -48,9 +48,9 @@ unsigned int collatz_len(unsigned long long n) { unsigned long long p0014() { unsigned long long answer = 2, length = 2, tmp; - for (unsigned long long test = 3; test < 1000000; test++) { + for (unsigned long long test = 3; test < 1000000; test++) { tmp = collatz_len(test); - if (tmp > length) { + if (tmp > length) { answer = test; length = tmp; } @@ -59,9 +59,8 @@ unsigned long long p0014() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { - unsigned long long answer = p0014(); - printf("%llu", answer); +int main(int argc, char const *argv[]) { + printf("%llu", p0014()); return 0; } #endif diff --git a/c/src/p0015.c b/c/src/p0015.c index 1e12eebd..54dc7bcb 100644 --- a/c/src/p0015.c +++ b/c/src/p0015.c @@ -26,9 +26,8 @@ unsigned long long p0015() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { - unsigned long long answer = p0015(); - printf("%llu", answer); +int main(int argc, char const *argv[]) { + printf("%llu", p0015()); return 0; } #endif diff --git a/c/src/p0016.c b/c/src/p0016.c index 4402768f..83dca2a2 100644 --- a/c/src/p0016.c +++ b/c/src/p0016.c @@ -18,7 +18,7 @@ What is the sum of the digits of the number 21000? unsigned long long p0016() { unsigned long long answer = 0; BCD_int power = pow_cuint_cuint(256, 125); - for (size_t i = 0; i < power.bcd_digits; i++) { + for (size_t i = 0; i < power.bcd_digits; i++) { answer += power.digits[i] & 0x0F; answer += power.digits[i] >> 4; } @@ -26,9 +26,8 @@ unsigned long long p0016() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { - unsigned long long answer = p0016(); - printf("%llu\n", answer); +int main(int argc, char const *argv[]) { + printf("%llu\n", p0016()); return 0; } #endif diff --git a/c/src/p0017.c b/c/src/p0017.c index bca317c7..5895df5b 100644 --- a/c/src/p0017.c +++ b/c/src/p0017.c @@ -88,9 +88,8 @@ unsigned long long p0017() { #ifndef UNITY_END -int main(int argc, char const *argv[]) { - unsigned long long answer = p0017(); - printf("%llu\n", answer); +int main(int argc, char const *argv[]) { + printf("%llu\n", p0017()); return 0; } #endif diff --git a/c/src/p0020.c b/c/src/p0020.c index 6b99d962..1f127412 100644 --- a/c/src/p0020.c +++ b/c/src/p0020.c @@ -41,9 +41,8 @@ unsigned long long p0020() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { - unsigned long long answer = p0020(); - printf("%llu\n", answer); +int main(int argc, char const *argv[]) { + printf("%llu\n", p0020()); return 0; } #endif diff --git a/c/src/p0022.c b/c/src/p0022.c index 02a305e7..0e1dde99 100644 --- a/c/src/p0022.c +++ b/c/src/p0022.c @@ -15,6 +15,7 @@ Find the sum of all the multiples of 3 or 5 below 1000. #ifndef EULER_P0022 #define EULER_P0022 #include +#include "include/macros.h" #include "include/utils.h" int cmpstr(const void* a, const void* b) { @@ -31,6 +32,8 @@ unsigned long long p0022() { while (fstring[i] && fstring[i] != ',') i++; const size_t len = i - pi - 2; + if (unlikely(len == 0)) + continue; names[idx] = (char *)malloc(len); memcpy(names[idx], fstring + pi + 1, len); names[idx++][len] = 0; @@ -48,9 +51,8 @@ unsigned long long p0022() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { - unsigned long long answer = p0022(); - printf("%llu\n", answer); +int main(int argc, char const *argv[]) { + printf("%llu\n", p0022()); return 0; } #endif diff --git a/c/src/p0025.c b/c/src/p0025.c index d26b6eeb..cb82ddaf 100644 --- a/c/src/p0025.c +++ b/c/src/p0025.c @@ -45,9 +45,8 @@ unsigned long long p0025() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { - unsigned long long answer = p0025(); - printf("%llu\n", answer); +int main(int argc, char const *argv[]) { + printf("%llu\n", p0025()); return 0; } #endif diff --git a/c/src/p0030.c b/c/src/p0030.c index ab6313dd..f7fd7eac 100644 --- a/c/src/p0030.c +++ b/c/src/p0030.c @@ -22,14 +22,14 @@ Find the sum of all the numbers that can be written as the sum of fifth powers o unsigned long long p0030() { unsigned long long answer = 0, sum, tmp; - for (unsigned long long i = 2; i < 1000000; i++) { + for (unsigned long long i = 2; i < 1000000; i++) { digit_counter dc = digits(i); sum = 0; - while (!dc.exhausted) { + while (!dc.exhausted) { tmp = next(dc); sum += tmp * tmp * tmp * tmp * tmp; } - if (sum == i) { + if (sum == i) { answer += i; } free_digit_counter(dc); @@ -38,9 +38,8 @@ unsigned long long p0030() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { - unsigned long long answer = p0030(); - printf("%llu\n", answer); +int main(int argc, char const *argv[]) { + printf("%llu\n", p0030()); return 0; } #endif diff --git a/c/src/p0034.c b/c/src/p0034.c index de5faff6..e221c08a 100644 --- a/c/src/p0034.c +++ b/c/src/p0034.c @@ -22,10 +22,10 @@ unsigned long long p0034() { for (unsigned long i = 10; i < 100000; i++) { sum = 0; dc = digits(i); - while (!dc.exhausted) { + while (!dc.exhausted) { sum += factorial(next(dc)); } - if (sum == i) { + if (sum == i) { answer += i; } free_digit_counter(dc); @@ -34,9 +34,8 @@ unsigned long long p0034() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { - unsigned long long answer = p0034(); - printf("%llu", answer); +int main(int argc, char const *argv[]) { + printf("%llu", p0034()); return 0; } #endif diff --git a/c/src/p0076.c b/c/src/p0076.c index a76e9e26..1d3f9d5c 100644 --- a/c/src/p0076.c +++ b/c/src/p0076.c @@ -72,9 +72,8 @@ unsigned int p0076() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { - unsigned int answer = p0076(); - printf("%u", answer); +int main(int argc, char const *argv[]) { + printf("%u", p0076()); return 0; } #endif diff --git a/c/src/p0836.c b/c/src/p0836.c index 350381ae..7e9d9583 100644 --- a/c/src/p0836.c +++ b/c/src/p0836.c @@ -23,9 +23,8 @@ char *p0836() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { - char *answer = p0836(); - printf("%s", answer); +int main(int argc, char const *argv[]) { + printf("%s", p0836()); return 0; } #endif diff --git a/c/src/tests/test_is_prime.c b/c/src/tests/test_is_prime.c index f15dfd5b..d779e589 100644 --- a/c/src/tests/test_is_prime.c +++ b/c/src/tests/test_is_prime.c @@ -11,7 +11,7 @@ int main(int argc, char const *argv[]) { uintmax_t i, p, prev = 0; p = next(pc); while (!pc.exhausted) { - for (i = prev + 1; i < p; i++) { + for (i = prev + 1; i < p; i++) { printf("%" PRIuMAX " %d %" PRIuMAX " -1\n", i, is_prime(i), is_composite(i)); } printf("%" PRIuMAX " %d %" PRIuMAX " %" PRIuMAX "\n", p, is_prime(p), is_composite(p), (uintmax_t) pc.idx - 1); diff --git a/cplusplus/src/include/macros.h b/cplusplus/src/include/macros.h index d56923ac..751f5f03 100644 --- a/cplusplus/src/include/macros.h +++ b/cplusplus/src/include/macros.h @@ -42,10 +42,6 @@ // helper macro function section -#ifndef swap - #define swap(x, y) do { typeof(x) SWAP = x; x = y; y = SWAP; } while (0) -#endif - #if !(CL_COMPILER) #define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0) diff --git a/cplusplus/src/include/math.h b/cplusplus/src/include/math.h index 79a1a330..4125a289 100644 --- a/cplusplus/src/include/math.h +++ b/cplusplus/src/include/math.h @@ -9,7 +9,7 @@ using namespace std; uintmax_t factorial(unsigned int n); -inline uintmax_t factorial(unsigned int n) { +inline uintmax_t factorial(unsigned int n) { // note that this function only works for numbers smaller than MAX_FACTORIAL_64 if ((sizeof(uintmax_t) == 8 && n > MAX_FACTORIAL_64) || (sizeof(uintmax_t) == 16 && n > MAX_FACTORIAL_128)) return -1; @@ -20,9 +20,9 @@ inline uintmax_t factorial(unsigned int n) { return ret; } -uintmax_t n_choose_r(unsigned int n, unsigned int r) { +uintmax_t n_choose_r(unsigned int n, unsigned int r) { // function returns -1 if it overflows - if ((sizeof(uintmax_t) == 8 && n <= MAX_FACTORIAL_64) || (sizeof(uintmax_t) == 16 && n <= MAX_FACTORIAL_128)) { + if ((sizeof(uintmax_t) == 8 && n <= MAX_FACTORIAL_64) || (sizeof(uintmax_t) == 16 && n <= MAX_FACTORIAL_128)) { // fast path if small enough return factorial(n) / factorial(r) / factorial(n-r); } @@ -55,27 +55,27 @@ uintmax_t n_choose_r(unsigned int n, unsigned int r) { } i = j = 2; answer = 1; - while (i <= n) { - while (factors[i] > 0) { + while (i <= n) { + while (factors[i] > 0) { tmp = answer; answer *= i; - while (answer < tmp && j <= n) { - while (factors[j] < 0) { + while (answer < tmp && j <= n) { + while (factors[j] < 0) { tmp /= j; factors[j]++; } j++; answer = tmp * i; } - if (answer < tmp) { + if (answer < tmp) { return -1; // this indicates an overflow } factors[i]--; } i++; } - while (j <= n) { - while (factors[j] < 0) { + while (j <= n) { + while (factors[j] < 0) { answer /= j; factors[j]++; } diff --git a/cplusplus/src/p0000_template.cpp b/cplusplus/src/p0000_template.cpp index 8e08e86a..de3e9e94 100644 --- a/cplusplus/src/p0000_template.cpp +++ b/cplusplus/src/p0000_template.cpp @@ -16,7 +16,7 @@ unsigned long long p0000() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { +int main(int argc, char const *argv[]) { unsigned long long answer = p0000(); printf("%llu\n", answer); return 0; diff --git a/cplusplus/src/p0001.cpp b/cplusplus/src/p0001.cpp index 9fbdeea9..b786b558 100644 --- a/cplusplus/src/p0001.cpp +++ b/cplusplus/src/p0001.cpp @@ -31,7 +31,7 @@ unsigned long long p0001() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { +int main(int argc, char const *argv[]) { std::cout << p0001() << std::endl; return 0; } diff --git a/cplusplus/src/p0002.cpp b/cplusplus/src/p0002.cpp index 765c0b17..9d6c0771 100644 --- a/cplusplus/src/p0002.cpp +++ b/cplusplus/src/p0002.cpp @@ -36,7 +36,7 @@ unsigned long long p0002() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { +int main(int argc, char const *argv[]) { std::cout << p0002() << std::endl; return 0; } diff --git a/cplusplus/src/p0004.cpp b/cplusplus/src/p0004.cpp index ff691c38..ae026741 100644 --- a/cplusplus/src/p0004.cpp +++ b/cplusplus/src/p0004.cpp @@ -18,8 +18,8 @@ Find the largest palindrome made from the product of two 3-digit numbers. unsigned int p0004() { unsigned int answer = 0, i, j, prod; - for (i = 100; i < 1000; i++) { - for (j = 100; j < 1000; j++) { + for (i = 100; i < 1000; i++) { + for (j = 100; j < 1000; j++) { prod = i * j; char buf[8] = {}; // I know snprintf exists, but it isn't defined in C++98, @@ -37,7 +37,7 @@ unsigned int p0004() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { +int main(int argc, char const *argv[]) { std::cout << p0004() << std::endl; return 0; } diff --git a/cplusplus/src/p0006.cpp b/cplusplus/src/p0006.cpp index ca1590bb..841e4f7f 100644 --- a/cplusplus/src/p0006.cpp +++ b/cplusplus/src/p0006.cpp @@ -25,14 +25,14 @@ natural numbers and the square of the sum. unsigned long long p0006() { unsigned long long sum = 100 * 101 / 2, sum_of_squares = 0; - for (unsigned long long i = 1; i < 101; i++) { + for (unsigned long long i = 1; i < 101; i++) { sum_of_squares += i * i; } return sum * sum - sum_of_squares; } #ifndef UNITY_END -int main(int argc, char const *argv[]) { +int main(int argc, char const *argv[]) { std::cout << p0006() << std::endl; return 0; } diff --git a/cplusplus/src/p0008.cpp b/cplusplus/src/p0008.cpp index 8d4d547b..1872ef72 100644 --- a/cplusplus/src/p0008.cpp +++ b/cplusplus/src/p0008.cpp @@ -58,12 +58,12 @@ unsigned long long p0008() { "05886116467109405077541002256983155200055935729725" "71636269561882670428252483600823257530420752963450"); char digits[1000]; - for (i = 0; i < 1000; i++) { + for (i = 0; i < 1000; i++) { digits[i] = plain_digits[i] - 0x30; } for (i = 0; i < 1000 - 13; i++) { tmp = digits[i]; - for (j = i + 1; j < i + 13; j++) { + for (j = i + 1; j < i + 13; j++) { tmp *= digits[j]; } answer = std::max(answer, tmp); @@ -72,7 +72,7 @@ unsigned long long p0008() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { +int main(int argc, char const *argv[]) { std::cout << p0008() << std::endl; return 0; } diff --git a/cplusplus/src/p0009.cpp b/cplusplus/src/p0009.cpp index 2205a02f..0b318ff7 100644 --- a/cplusplus/src/p0009.cpp +++ b/cplusplus/src/p0009.cpp @@ -19,10 +19,10 @@ Find the product abc. unsigned long long p0009() { unsigned long long answer = 0; - for (unsigned int c = 3; !answer && c < 1000; c++) { - for (unsigned int b = 2; b < c; b++) { + for (unsigned int c = 3; !answer && c < 1000; c++) { + for (unsigned int b = 2; b < c; b++) { unsigned int a = 1000 - c - b; - if (a < b && a*a + b*b == c*c) { + if (a < b && a*a + b*b == c*c) { answer = (unsigned long long) a * b * c; break; } @@ -32,7 +32,7 @@ unsigned long long p0009() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { +int main(int argc, char const *argv[]) { std::cout << p0009() << std::endl; return 0; } diff --git a/cplusplus/src/p0011.cpp b/cplusplus/src/p0011.cpp index cf28820c..371fdbb4 100644 --- a/cplusplus/src/p0011.cpp +++ b/cplusplus/src/p0011.cpp @@ -61,8 +61,8 @@ static const unsigned char grid[20][20] = { unsigned long long p0011() { unsigned long answer = 0, tmp; unsigned char i, j; - for (i = 0; i < 20; i++) { - for (j = 0; j < 17; j++) { + 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]; answer = std::max(answer, tmp); @@ -71,8 +71,8 @@ unsigned long long p0011() { answer = std::max(answer, tmp); } } - for (i = 0; i < 17; i++) { - for (j = 0; j < 17; j++) { + 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]; answer = std::max(answer, tmp); @@ -85,7 +85,7 @@ unsigned long long p0011() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { +int main(int argc, char const *argv[]) { std::cout << p0011() << std::endl; return 0; } diff --git a/cplusplus/src/p0013.cpp b/cplusplus/src/p0013.cpp index 2a3fa946..47c8da98 100644 --- a/cplusplus/src/p0013.cpp +++ b/cplusplus/src/p0013.cpp @@ -235,7 +235,7 @@ unsigned long long p0013() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { +int main(int argc, char const *argv[]) { std::cout << p0013() << std::endl; return 0; } diff --git a/cplusplus/src/p0014.cpp b/cplusplus/src/p0014.cpp index fbdf4590..294f37cb 100644 --- a/cplusplus/src/p0014.cpp +++ b/cplusplus/src/p0014.cpp @@ -29,12 +29,12 @@ static unsigned int collatz_len_cache[CACHE_SIZE] = {0, 1, 0}; unsigned int collatz_len(unsigned long long n); -unsigned int collatz_len(unsigned long long n) { +unsigned int collatz_len(unsigned long long n) { if (n < CACHE_SIZE && collatz_len_cache[n]) { return collatz_len_cache[n]; } unsigned int ret = 0; - if (n % 2) { + if (n % 2) { ret = 2 + collatz_len((3 * n + 1) / 2); } else { ret = 1 + collatz_len(n / 2); @@ -47,9 +47,9 @@ unsigned int collatz_len(unsigned long long n) { unsigned long long p0014() { unsigned long long answer = 2, length = 2, tmp; - for (unsigned long long test = 3; test < 1000000; test++) { + for (unsigned long long test = 3; test < 1000000; test++) { tmp = collatz_len(test); - if (tmp > length) { + if (tmp > length) { answer = test; length = tmp; } @@ -58,7 +58,7 @@ unsigned long long p0014() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { +int main(int argc, char const *argv[]) { std::cout << p0014() << std::endl; return 0; } diff --git a/cplusplus/src/p0015.cpp b/cplusplus/src/p0015.cpp index e7397289..b6313631 100644 --- a/cplusplus/src/p0015.cpp +++ b/cplusplus/src/p0015.cpp @@ -26,7 +26,7 @@ unsigned long long p0015() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { +int main(int argc, char const *argv[]) { std::cout << p0015() << std::endl; return 0; } diff --git a/cplusplus/src/p0016.cpp b/cplusplus/src/p0016.cpp index e23bbcf8..14224a4f 100644 --- a/cplusplus/src/p0016.cpp +++ b/cplusplus/src/p0016.cpp @@ -40,7 +40,7 @@ unsigned long long p0016() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { +int main(int argc, char const *argv[]) { std::cout << p0016() << std::endl; return 0; } diff --git a/cplusplus/src/p0017.cpp b/cplusplus/src/p0017.cpp index 6de609f3..312c43fe 100644 --- a/cplusplus/src/p0017.cpp +++ b/cplusplus/src/p0017.cpp @@ -112,7 +112,7 @@ unsigned long long p0017() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { +int main(int argc, char const *argv[]) { std::cout << p0017() << std::endl; return 0; } diff --git a/cplusplus/src/p0020.cpp b/cplusplus/src/p0020.cpp index d93ea1af..94ab7fc7 100644 --- a/cplusplus/src/p0020.cpp +++ b/cplusplus/src/p0020.cpp @@ -43,7 +43,7 @@ unsigned long long p0020() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { +int main(int argc, char const *argv[]) { std::cout << p0020() << std::endl; return 0; } diff --git a/cplusplus/src/p0022.cpp b/cplusplus/src/p0022.cpp index 95e8e343..860fee75 100644 --- a/cplusplus/src/p0022.cpp +++ b/cplusplus/src/p0022.cpp @@ -45,7 +45,7 @@ unsigned long long p0022() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { +int main(int argc, char const *argv[]) { std::cout << p0022() << std::endl; return 0; } diff --git a/cplusplus/src/p0034.cpp b/cplusplus/src/p0034.cpp index 58cc25b5..37a8ee31 100644 --- a/cplusplus/src/p0034.cpp +++ b/cplusplus/src/p0034.cpp @@ -24,10 +24,10 @@ unsigned long long p0034() { // I know snprintf exists, but it isn't defined in C++98, // and this isn't taking in user input sprintf(buf, "%lu", i); - for (unsigned char j = 0; j < 8 && buf[j]; j++) { + for (unsigned char j = 0; j < 8 && buf[j]; j++) { sum += factorial(buf[j] - '0'); } - if (sum == i) { + if (sum == i) { answer += i; } } @@ -35,7 +35,7 @@ unsigned long long p0034() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { +int main(int argc, char const *argv[]) { std::cout << p0034() << std::endl; return 0; } diff --git a/cplusplus/src/p0076.cpp b/cplusplus/src/p0076.cpp index 0575a74c..9ae40e4e 100644 --- a/cplusplus/src/p0076.cpp +++ b/cplusplus/src/p0076.cpp @@ -72,7 +72,7 @@ unsigned int p0076() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { +int main(int argc, char const *argv[]) { std::cout << p0076() << std::endl; return 0; } diff --git a/cplusplus/src/p0836.cpp b/cplusplus/src/p0836.cpp index 8e0fb9c4..a2cb0564 100644 --- a/cplusplus/src/p0836.cpp +++ b/cplusplus/src/p0836.cpp @@ -23,7 +23,7 @@ std::string p0836() { } #ifndef UNITY_END -int main(int argc, char const *argv[]) { +int main(int argc, char const *argv[]) { std::cout << p0836() << std::endl; return 0; } diff --git a/docs/src/c/p0022.rst b/docs/src/c/p0022.rst index a7c27c5f..ae79b207 100644 --- a/docs/src/c/p0022.rst +++ b/docs/src/c/p0022.rst @@ -12,6 +12,7 @@ View source code :source:`c/src/p0022.c` Includes -------- +- `macros.h <./lib/macros.html>`__ - `utils.h <./lib/utils.html>`__ Solution diff --git a/docs/src/cplusplus/lib/macros.rst b/docs/src/cplusplus/lib/macros.rst index 0390947e..805edd3e 100644 --- a/docs/src/cplusplus/lib/macros.rst +++ b/docs/src/cplusplus/lib/macros.rst @@ -25,10 +25,6 @@ View source code :source:`cplusplus/src/include/macros.h` These macros implement the ``likely()`` and ``unlikely()`` flags, as in the `Linux kernel `__ to assist in branch prediction. On ``cl`` it has no effect. -.. c:macro:: swap(x, y) - - Swap the names of two variables of the same type. - .. c:macro:: MAX_FACTORIAL_64 MAX_FACTORIAL_128