Skip to content

Commit

Permalink
Convert to unity testing
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Jun 24, 2024
1 parent 97b0bee commit 4b2ccef
Show file tree
Hide file tree
Showing 21 changed files with 223 additions and 69 deletions.
20 changes: 20 additions & 0 deletions c/include/bcd.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
Expand Down Expand Up @@ -538,6 +539,25 @@ inline BCD_int shift_bcd_right(BCD_int a, uintmax_t tens) {
return div_bcd_pow_10(a, tens);
}

uintmax_t bcd_to_unsigned(BCD_int a) {
if (a.zero)
return 0;
uintmax_t answer = 0;
for (size_t i = 0; i < a.bcd_digits; ++i) {
size_t digit = a.bcd_digits - i - 1;
answer = answer * 100 + ((a.digits[digit] & 0xF0) >> 4) * 10 + (a.digits[digit] & 0xF);
}
return answer;
}

uintmax_t bcd_to_signed(BCD_int a);
inline uintmax_t bcd_to_signed(BCD_int a) {
uintmax_t answer = bcd_to_unsigned(a);
if (a.negative)
return -answer;
return answer;
}

void print_bcd(BCD_int x) {
if (unlikely(x.zero)) {
printf("0");
Expand Down
1 change: 1 addition & 0 deletions c/p0000_template.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ critque.
This paragraph should be replaced by the problem description, excluding images.
*/
#pragma once
#include <stdio.h>

unsigned long long p0000() {
Expand Down
6 changes: 5 additions & 1 deletion c/p0001.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
*/

#pragma once
#include <stdio.h>
#include "include/iterator.h"

unsigned long long p0001() {
unsigned long long answer = 0;
counter c = counter3(0, 1000, 3);
while (!c.exhausted) {
answer += next(c);
Expand All @@ -29,10 +30,13 @@ unsigned long long p0001() {
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);
return 0;
}
#endif
10 changes: 9 additions & 1 deletion c/p0002.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,25 @@ By considering the terms in the Fibonacci sequence whose values do not exceed
four million, find the sum of the even-valued terms.
*/

#pragma once
#include <stdio.h>
#include "include/fibonacci.h"

int main(int argc, char const *argv[]) {
unsigned long long p0002() {
unsigned long long answer = 0;
fibonacci fib = fibonacci1(3999999);
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, ...)
}
return answer;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
unsigned long long answer = p0002();
printf("%llu\n", answer);
return 0;
}
#endif
10 changes: 9 additions & 1 deletion c/p0003.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,24 @@ The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
*/

#pragma once
#include <stdio.h>
#include "include/primes.h"

int main(int argc, char const *argv[]) {
unsigned long long p0003() {
unsigned long long answer = 0;
prime_factor_counter pfc = prime_factors(600851475143);
while (!pfc.exhausted) {
answer = next(pfc);
}
free_prime_factor_counter(pfc);
return answer;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
unsigned long long answer = p0003();
printf("%llu\n", answer);
return 0;
}
#endif
10 changes: 8 additions & 2 deletions c/p0004.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
*/
#pragma once
#include <stdio.h>

#include "include/digits.h"


int main(int argc, char const *argv[]) {
unsigned int p0004() {
unsigned int answer = 0, i, j, a, z, prod;
bool broken;
digit_counter dc;
Expand All @@ -36,6 +36,12 @@ int main(int argc, char const *argv[]) {
free_digit_counter(dc);
}
}
return answer;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
printf("%u", answer);
return 0;
}
#endif
10 changes: 9 additions & 1 deletion c/p0005.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ number infrastructure a lot easier to set up.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
*/

#pragma once
#include <stdio.h>
#include "include/macros.h"
#include "include/primes.h"

int main(int argc, char const *argv[]) {
unsigned long long p0005() {
unsigned long long answer = 1;
unsigned char factor_tracker[20] = {0}, local_factor_tracker[20] = {0};
prime_factor_counter pfc;
Expand All @@ -36,6 +37,13 @@ int main(int argc, char const *argv[]) {
answer *= i;
}
}
return answer;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
unsigned long long answer = p0005();
printf("%llu", answer);
return 0;
}
#endif
13 changes: 10 additions & 3 deletions c/p0006.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,21 @@ numbers and the square of the sum is 3025 − 385 = 2640.
Find the difference between the sum of the squares of the first one hundred
natural numbers and the square of the sum.
*/
#pragma once
#include <stdio.h>


int main(int argc, char const *argv[]) {
unsigned long long p0006() {
unsigned long long sum = 100 * 101 / 2, sum_of_squares = 0;
for (unsigned long long i = 1; i < 101; i++) {
sum_of_squares += i * i;
}
printf("%llu", sum * sum - sum_of_squares);
return sum * sum - sum_of_squares;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
unsigned long long answer = p0006();
printf("%llu", answer);
return 0;
}
#endif
16 changes: 11 additions & 5 deletions c/p0007.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,26 @@ By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that
What is the 10 001st prime number?
*/
#pragma once
#include <stdio.h>
#include "include/primes.h"


int main(int argc, char const *argv[]) {
unsigned long long p0007() {
unsigned int answer, count = 0;
prime_sieve ps = prime_sieve0();
while (!ps.exhausted) {
answer = next(ps);
if (++count == 10001) {
printf("%u", answer);
if (++count == 10001)
break;
}
}
free_prime_sieve(ps);
return answer;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
unsigned long long answer = p0007();
printf("%llu", answer);
return 0;
}
#endif
11 changes: 9 additions & 2 deletions c/p0008.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ The four adjacent digits in the 1000-digit number that have the greatest product
Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
*/
#pragma once
#include <stdio.h>
#include "include/macros.h"


int main(int argc, char const *argv[]) {
unsigned long long p0008() {
size_t i, j;
unsigned long long answer = 0, tmp;
const char *plain_digits = ("73167176531330624919225119674426574742355349194934"
Expand Down Expand Up @@ -68,6 +68,13 @@ int main(int argc, char const *argv[]) {
}
answer = max(answer, tmp);
}
return answer;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
unsigned long long answer = p0008();
printf("%llu", answer);
return 0;
}
#endif
11 changes: 9 additions & 2 deletions c/p0009.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
*/
#pragma once
#include <stdio.h>


int main(int argc, char const *argv[]) {
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++) {
Expand All @@ -27,6 +27,13 @@ int main(int argc, char const *argv[]) {
}
}
}
return answer;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
unsigned long long answer = p0009();
printf("%llu", answer);
return 0;
}
#endif
13 changes: 10 additions & 3 deletions c/p0010.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,24 @@ The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
*/
#pragma once
#include <stdio.h>
#include "include/primes.h"


int main(int argc, char const *argv[]) {
unsigned long long p0010() {
unsigned long long tmp, answer = 0;
prime_sieve ps = prime_sieve0();
while ((tmp = next(ps)) < 2000000) {
answer += tmp;
}
free_prime_sieve(ps);
printf("%llu", answer); // this is because of a bug
return answer;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
unsigned long long answer = p0010();
printf("%llu", answer);
return 0;
}
#endif
11 changes: 9 additions & 2 deletions c/p0011.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ The product of these numbers is 26 × 63 × 78 × 14 = 1788696.
What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in
the 20×20 grid?
*/
#pragma once
#include <stdio.h>
#include "include/macros.h"

Expand All @@ -57,8 +58,7 @@ static const unsigned char grid[20][20] = {
{ 1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48}
};


int main(int argc, char const *argv[]) {
unsigned long long p0011() {
unsigned long answer = 0, tmp;
unsigned char i, j;
for (i = 0; i < 20; i++) {
Expand All @@ -81,6 +81,13 @@ int main(int argc, char const *argv[]) {
answer = max(answer, tmp);
}
}
return answer;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
unsigned long long answer = p0011();
printf("%lu", answer);
return 0;
}
#endif
18 changes: 12 additions & 6 deletions c/p0012.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ We can see that 28 is the first triangle number to have over five divisors.
What is the value of the first triangle number to have over five hundred
divisors?
*/
#pragma once
#include <stdio.h>
#include "include/factors.h"

Expand All @@ -51,16 +52,21 @@ triangle_iterator triangle_iterator0() {
return ret;
}

int main(int argc, char const *argv[]) {
unsigned long long p0012() {
triangle_iterator ti = triangle_iterator0();
unsigned long long current;
while (true) {
current = next(ti);
// printf("%llu\n", current);
if (proper_divisor_count(current) > 499) {
printf("%llu", current);
return 0;
}
if (proper_divisor_count(current) > 499)
return current;
}
return -1;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
unsigned long long answer = p0012();
printf("%llu", answer);
return 0;
}
#endif
Loading

0 comments on commit 4b2ccef

Please sign in to comment.