Skip to content

Commit

Permalink
Add combination iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
ishanpranav committed Feb 15, 2024
1 parent 42ff06a commit 14bf321
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Licensed under the MIT License.

a.out
a.py
*.exe
*.a
*.lib
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ all: \
id0068$(E) id0069$(E) id0070$(E) id0071$(E) id0072$(E) id0073$(E) \
id0074$(E) id0075$(E) id0076$(E) id0077$(E) id0078$(E) id0079$(E) \
id0080$(E) id0081$(E) id0082$(E) id0083$(E) id0084$(E) id0085$(E) \
id0086$(E) id0087$(E) id0089$(E) id0091$(E) id0092$(E)
id0086$(E) id0087$(E) id0089$(E) id0091$(E) id0092$(E) id0093$(E) \

libeuler$(A): $(call RECURSE,lib,*.c)
$(RM) *.o
Expand Down Expand Up @@ -312,5 +312,8 @@ id0091$(E): src/id0091.c libeuler$(A)
id0092$(E): src/id0092.c libeuler$(A)
$(CC) $(CFLAGS) $< -o $@ $(LEULER) $(LM)

id0093$(E): src/id0093.c libeuler$(A)
$(CC) $(CFLAGS) $< -o $@ $(LEULER) $(LM)

clean:
$(RM) *$(A) *.o
2 changes: 1 addition & 1 deletion cstyle.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ is impractical, use numeric suffixes instead.
| `b` | blue color component, boundary points in Pick\'s Theorem |
| `cb` | cube |
| `d` | divisor |
| `dx` | change in an arbitrary dimension |
| `e` | arbitrary edge |
| `ef` | even Fibonacci number |
| `f` | Fibonacci number |
| `dx` | change in an arbitrary dimension |
| `g` | green color component, arbitrary graph |
| `h` | row offset, horizontal offset |
| `hi` | larger pointer, point above, upper buffer |
Expand Down
49 changes: 49 additions & 0 deletions lib/combination_iterator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Licensed under the MIT License.

#include <stdint.h>
#include <string.h>
#include "combination_iterator.h"

void combination_begin(
CombinationIterator iterator,
size_t subset[],
size_t n,
size_t k)
{
iterator->subset = subset;
iterator->i = 0;
iterator->k = k;
iterator->n = n;
iterator->end = false;

for (size_t i = 0; i < iterator->k; i++)
{
iterator->subset[i] = 0;
}

combination_next(iterator);
}

void combination_next(CombinationIterator iterator)
{
while (iterator->i != SIZE_MAX)
{
iterator->subset[iterator->i]++;

if (iterator->subset[iterator->i] > iterator->n)
{
iterator->i--;
}
else if (iterator->i == iterator->k - 1)
{
return;
}
else
{
iterator->i++;
iterator->subset[iterator->i] = iterator->subset[iterator->i - 1];
}
}

iterator->end = true;
}
24 changes: 24 additions & 0 deletions lib/combination_iterator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Licensed under the MIT License.

#include "object.h"
#include <stdbool.h>
#include <stddef.h>

struct CombinationIterator
{
size_t* subset;
size_t i;
size_t k;
size_t n;
bool end;
};

typedef struct CombinationIterator* CombinationIterator;

void combination_begin(
CombinationIterator iterator,
size_t subset[],
size_t n,
size_t k);

void combination_next(CombinationIterator iterator);
4 changes: 2 additions & 2 deletions src/id0092.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int main(void)
int results[568] = { 0 };
clock_t start = clock();

for (long m = 1; m < 568; m++)
for (int m = 1; m < 568; m++)
{
results[m] = math_square_digit_chain(m, results);

Expand All @@ -51,7 +51,7 @@ int main(void)
}
}

for (long m = 1; m < 10000000l; m++)
for (long m = 568; m < 10000000l; m++)
{
long n = math_square_digit_chain(m, results);

Expand Down
22 changes: 22 additions & 0 deletions src/id0093.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "../lib/combination_iterator.h"
#include "../lib/euler.h"

int main(void)
{
struct CombinationIterator it;
size_t subset[2];

for (combination_begin(&it, subset, 4, 2); !it.end; combination_next(&it))
{
printf("=> ");

for (int i = 0; i < 2; i++)
{
printf("%zu ", subset[i]);
}

printf("\n");
}

return 0;
}
7 changes: 7 additions & 0 deletions tools/test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,10 @@ for ($i = 84; $i -le 88; $i++)
{
Invoke-Expression "./../id00${i}.exe"
}

./../id0089.exe

for ($i = 91; $i -le 92; $i++)
{
Invoke-Expression "./../id00${i}.exe"
}
7 changes: 7 additions & 0 deletions tools/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,10 @@ for i in {0084..0088};
do
$run ./../id${i}
done

$run ./../id0089

for i in {0091..0092};
do
$run ./../id${i}
done

0 comments on commit 14bf321

Please sign in to comment.