diff --git a/README.md b/README.md index f98a9b1..eb3f09d 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,7 @@ which is licensed under the GNU Lesser General Public License v3.0 (`LGPL-3.0`). | 69 | [Sqrt(x)](src/lc0069.c) | Mathematics | Square root | `sqrt` | | 75 | [Sort Colors](src/lc0075.c) | Sorting | Sort | `qsort` | | 77 | [Combinations](src/lc0077.c) | Backtracking | Matrix | `CombinationIterator` | +| 204 | [Count Primes](src/lc0204.c) | Mathematics | Count | `Sieve`, `binary_search_rank` | | 273 | [Integer to English Words](src/lc0273.c) | Mathematics | String | `words_to_string` | | 367 | [Valid Perfect Square](src/lc0367.c) | Mathematics | Boolean | `math_is_polygonal` | | 744 | [Find Smallest Letter Greater Than Target](src/lc0744.c) | Binary search | Character | `binary_search_min` | diff --git a/lib/binary_search.h b/lib/binary_search.h index 6786f17..604ede5 100644 --- a/lib/binary_search.h +++ b/lib/binary_search.h @@ -9,7 +9,7 @@ * Searches a sorted collection for the least element greater than or equal to * a given value. This function uses the binary search algorithm. * - * @param item the required minimum value. + * @param min the required minimum value. * @param items the sorted collection of items. * @param length the number of items. * @param itemSize the size of each item. @@ -19,7 +19,26 @@ * given minimum. */ Object binary_search_min( - Object item, + Object min, + Array items, + size_t length, + size_t itemSize, + Comparer comparer); + +/** + * Determines the number of elements less than a given value in a sorted + * collection. This function uses the binary search algorithm. + * + * @param max the required maximum value. + * @param items the sorted collection of items. + * @param length the number of items. + * @param itemSize the size of each item. + * @param comparer the item comparer. The `items` argument must be sorted using + * the same comparer. + * @return The number of items less than the given maximum. +*/ +size_t binary_search_rank( + Object max, Array items, size_t length, size_t itemSize, diff --git a/lib/combination_iterator.c b/lib/combination_iterator.c index 872d7e7..8c68e75 100644 --- a/lib/combination_iterator.c +++ b/lib/combination_iterator.c @@ -16,7 +16,7 @@ void combination_begin( iterator->n = n; iterator->end = false; - for (size_t i = 0; i < iterator->k; i++) + for (int i = 0; i < iterator->k; i++) { iterator->subset[i] = 0; } diff --git a/lib/list.c b/lib/list.c index a855211..cb524a7 100644 --- a/lib/list.c +++ b/lib/list.c @@ -93,11 +93,6 @@ bool list_contains(List instance, Object item, EqualityComparer itemComparer) return false; } -void list_clear(List instance) -{ - instance->count = 0; -} - void list_reverse(List instance) { if (!instance->count) @@ -148,12 +143,17 @@ bool list_sequence_equal(List left, List right, EqualityComparer itemComparer) return true; } +void list_clear(List instance) +{ + instance->count = 0; +} + void finalize_list(List instance) { + list_clear(instance); free(instance->items); instance->items = NULL; instance->itemSize = 0; - instance->count = 0; instance->capacity = 0; } diff --git a/lib/list.h b/lib/list.h index 2b6458d..a6f7faa 100644 --- a/lib/list.h +++ b/lib/list.h @@ -63,13 +63,6 @@ Exception list_ensure_capacity(List instance, size_t capacity); */ bool list_contains(List instance, Object item, EqualityComparer itemComparer); -/** - * Removes all elements from the list. - * - * @param instance the `List` instance. -*/ -void list_clear(List instance); - /** * Reverses the order of the elements in the list. * @@ -88,6 +81,13 @@ void list_reverse(List instance); */ bool list_sequence_equal(List left, List right, EqualityComparer itemComparer); +/** + * Removes all elements from the list. + * + * @param instance the `List` instance. +*/ +void list_clear(List instance); + /** * Frees all resources. * diff --git a/lib/priority_queue.c b/lib/priority_queue.c index e808930..805637d 100644 --- a/lib/priority_queue.c +++ b/lib/priority_queue.c @@ -193,8 +193,14 @@ bool priority_queue_try_dequeue( return true; } +void priority_queue_clear(PriorityQueue instance) +{ + instance->count = 0; +} + void finalize_priority_queue(PriorityQueue instance) { + priority_queue_clear(instance); free(instance->items); free(instance->priorities); @@ -202,7 +208,6 @@ void finalize_priority_queue(PriorityQueue instance) instance->itemSize = 0; instance->priorities = NULL; instance->prioritySize = 0; - instance->count = 0; instance->capacity = 0; instance->priorityComparer = NULL; } diff --git a/lib/priority_queue.h b/lib/priority_queue.h index 419938d..bbef4ca 100644 --- a/lib/priority_queue.h +++ b/lib/priority_queue.h @@ -1,5 +1,6 @@ // Licensed under the MIT License. +#include #include "comparer.h" #include "exception.h" #include "object.h" @@ -90,6 +91,13 @@ bool priority_queue_try_dequeue( Object item, Object priority); +/** + * Removes all elements from the priority queue. + * + * @param instance the `PriorityQueue` instance. +*/ +void priority_queue_clear(PriorityQueue instance); + /** * Frees all resources. * diff --git a/src/lc0023.c b/src/lc0023.c index d0b0d46..3de8616 100644 --- a/src/lc0023.c +++ b/src/lc0023.c @@ -75,7 +75,7 @@ struct ListNode* mergeKLists(struct ListNode** lists, int listsSize) return head.next; } -void finalize() +void finalizeMergeKLists() { finalize_priority_queue(&priorityQueue); } diff --git a/src/lc0204.c b/src/lc0204.c new file mode 100644 index 0000000..a71d7d4 --- /dev/null +++ b/src/lc0204.c @@ -0,0 +1,39 @@ +// Licensed under the MIT License. + +// Count Primes + +#include "../lib/binary_search.h" +#include "../lib/euler.h" +#include "../lib/sieve.h" + +bool lazy; +struct Sieve primes; + +int countPrimes(int n) +{ + if (n <= 2) + { + return 0; + } + + if (!lazy) + { + euler_ok(sieve(&primes, 5000000)); + + lazy = true; + } + + long long m = n; + + return binary_search_rank( + &m, + primes.primes.items, + primes.primes.count, + sizeof(long long), + long_long_comparer); +} + +void finalizeCountPrimes() +{ + finalize_sieve(&primes); +} diff --git a/tools/test.ps1 b/tools/test.ps1 index ca0d048..e2d54a5 100644 --- a/tools/test.ps1 +++ b/tools/test.ps1 @@ -65,14 +65,14 @@ Get-Content '../data/id0081.txt' | ./../id0081.exe Get-Content '../data/id0081.txt' | ./../id0082.exe Get-Content '../data/id0081.txt' | ./../id0083.exe -for ($i = 84; $i -le 88; $i++) +for ($i = 84; $i -le 87; $i++) { Invoke-Expression "./../id00${i}.exe" } ./../id0089.exe -for ($i = 91; $i -le 92; $i++) +for ($i = 91; $i -le 93; $i++) { Invoke-Expression "./../id00${i}.exe" } diff --git a/tools/test.sh b/tools/test.sh index 6d1c92e..6f1aadf 100644 --- a/tools/test.sh +++ b/tools/test.sh @@ -72,14 +72,14 @@ cat ../data/id0081.txt | $run ./../id0081 cat ../data/id0081.txt | $run ./../id0082 cat ../data/id0081.txt | $run ./../id0083 -for i in {0084..0088}; +for i in {0084..0087}; do $run ./../id${i} done $run ./../id0089 -for i in {0091..0092}; +for i in {0091..0093}; do $run ./../id${i} done