-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
60b2d5c
commit 5df74d7
Showing
13 changed files
with
285 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Licensed under the MIT License. | ||
|
||
/** Represents a binary mathematical operator in reverse Polish notation. */ | ||
typedef double (*Operator)(double left, double right); | ||
|
||
/** | ||
* Adds two operands. | ||
* | ||
* @param left the left addend. | ||
* @param right the right addend. | ||
* @return The sum of `left` and `right`. | ||
*/ | ||
double add_operator(double left, double right); | ||
|
||
/** | ||
* Subtracts one operand from an other. | ||
* | ||
* @param left the left addend. | ||
* @param right the right addend. | ||
* @return The difference of `left` and `right`. | ||
*/ | ||
double subtract_operator(double left, double right); | ||
|
||
/** | ||
* Multiplies two operands. | ||
* | ||
* @param left the left factor. | ||
* @param right the right factor. | ||
* @return The product of `left` and `right`. | ||
*/ | ||
double multiply_operator(double left, double right); | ||
|
||
/** | ||
* Performs integer division between two operands. | ||
* | ||
* @param left the dividend. | ||
* @param right the divisor. | ||
* @return The integer quotient of `left div right`. | ||
*/ | ||
double div_operator(double left, double right); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Licensed under the MIT License. | ||
|
||
double add_operator(double left, double right) | ||
{ | ||
return left + right; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Licensed under the MIT License. | ||
|
||
double div_operator(double left, double right) | ||
{ | ||
return (int)left / (int)right; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Licensed under the MIT License. | ||
|
||
double multiply_operator(double left, double right) | ||
{ | ||
return left * right; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Licensed under the MIT License. | ||
|
||
double subtract_operator(double left, double right) | ||
{ | ||
return left - right; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,43 @@ | ||
// Licensed under the MIT License. | ||
|
||
#include <ctype.h> | ||
#include "rpn.h" | ||
#include "rpn_operator.h" | ||
|
||
int rpn_evaluate(String tokens[], size_t length) | ||
static RpnOperator rpn_get_operator(Rpn instance, String operator) | ||
{ | ||
switch (operator[0]) | ||
{ | ||
case '+': return instance->add; | ||
case '-': return instance->subtract; | ||
case '*': return instance->multiply; | ||
case '/': return instance->divide; | ||
} | ||
|
||
return NULL; | ||
} | ||
|
||
double rpn_evaluate(Rpn instance, String tokens[], size_t length) | ||
{ | ||
size_t sp = 0; | ||
int* stack = malloc(length * sizeof * stack); | ||
double* stack = malloc(length * sizeof * stack); | ||
|
||
for (size_t i = 0; i < length; i++) | ||
{ | ||
if (tokens[i][1] != '\0' || isdigit(tokens[i][0])) | ||
{ | ||
stack[sp] = atoi(tokens[i]); | ||
stack[sp] = atof(tokens[i]); | ||
sp++; | ||
|
||
continue; | ||
} | ||
|
||
stack[sp - 2] = rpn_operator_apply( | ||
tokens[i][0], | ||
stack[sp - 2], | ||
stack[sp - 1]); | ||
double left = stack[sp - 2]; | ||
double right = stack[sp - 1]; | ||
|
||
stack[sp - 2] = rpn_get_operator(instance, tokens[i])(left, right); | ||
sp--; | ||
} | ||
|
||
return stack[0]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// Licensed under the MIT License. | ||
|
||
// Arithmetic Expressions | ||
|
||
#include <stdarg.h> | ||
#include <string.h> | ||
#include "../lib/combination_iterator.h" | ||
#include "../lib/euler.h" | ||
#include "../lib/permutation_iterator.h" | ||
#include "../lib/rpn.h" | ||
|
||
void rpn_evaluate_formatted(bool results[], String format, ...) | ||
{ | ||
va_list argl; | ||
char buffer[8]; | ||
char longBuffer[14]; | ||
char* expression[7]; | ||
|
||
va_start(argl, format); | ||
vsprintf(buffer, format, argl); | ||
va_end(argl); | ||
|
||
for (int i = 0; i < 7; i++) | ||
{ | ||
longBuffer[i + i] = buffer[i]; | ||
longBuffer[i + i + 1] = '\0'; | ||
expression[i] = longBuffer + i + i; | ||
} | ||
|
||
int result = rpn_evaluate(expression, 7); | ||
|
||
if (result > 0 && !results[result]) | ||
{ | ||
results[result] = true; | ||
} | ||
} | ||
|
||
int math_arithmetic_expression_length(int digits[]) | ||
{ | ||
bool results[1001] = { 0 }; | ||
struct PermutationIterator it; | ||
|
||
for (permutation_begin( | ||
&it, | ||
digits, | ||
4, | ||
1, | ||
char_comparer); | ||
!it.end; | ||
permutation_next(&it)) | ||
{ | ||
char* operators = "+-*/"; | ||
|
||
for (char* p = operators; *p; p++) | ||
{ | ||
for (char* q = operators; *q; q++) | ||
{ | ||
for (char* r = operators; *r; r++) | ||
{ | ||
rpn_evaluate_formatted( | ||
results, | ||
"%d%d%d%d%c%c%c", | ||
digits[0], digits[1], digits[2], digits[3], *p, *q, *r); | ||
rpn_evaluate_formatted( | ||
results, | ||
"%d%d%d%c%d%c%c", | ||
digits[0], digits[1], digits[2], *p, digits[3], *q, *r); | ||
rpn_evaluate_formatted( | ||
results, | ||
"%d%d%d%c%c%d%c", | ||
digits[0], digits[1], digits[2], *p, *q, digits[3], *r); | ||
rpn_evaluate_formatted( | ||
results, | ||
"%d%d%c%d%d%c%c", | ||
digits[0], digits[1], *p, digits[2], digits[3], *q, *r); | ||
rpn_evaluate_formatted( | ||
results, | ||
"%d%d%c%d%c%d%c", | ||
digits[0], digits[1], *p, digits[2], *q, digits[3], *r); | ||
} | ||
} | ||
} | ||
} | ||
|
||
int result = 0; | ||
|
||
for (int i = 1; i < 1000; i++) | ||
{ | ||
if (!results[i]) | ||
{ | ||
break; | ||
} | ||
|
||
result++; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
int main(void) | ||
{ | ||
int maxLength = 0; | ||
int digits[4]; | ||
int maxDigits[4] = { 0 }; | ||
struct CombinationIterator it; | ||
clock_t start = clock(); | ||
|
||
for (combination_begin(&it, digits, 9, 4); | ||
!it.end; | ||
combination_next(&it)) | ||
{ | ||
int length = math_arithmetic_expression_length(digits); | ||
|
||
if (length > maxLength) | ||
{ | ||
maxLength = length; | ||
|
||
memcpy(maxDigits, digits, sizeof digits); | ||
} | ||
} | ||
|
||
int result = 0; | ||
|
||
for (int i = 0; i < 4; i++) | ||
{ | ||
result = result * 10 + maxDigits[i]; | ||
} | ||
|
||
return euler_submit(92, result, start); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Licensed under the MIT License. | ||
|
||
// Right Triangles with Integer Coordinates | ||
|
||
#include <math.h> | ||
#include "../lib/euler.h" | ||
|
||
int main(void) | ||
{ | ||
long x = 2; | ||
long y = 1; | ||
long sum = -2; | ||
clock_t start = clock(); | ||
|
||
for (;;) | ||
{ | ||
long long p = 2 * (3 * y - x) * (3 * y - x); | ||
|
||
if (p > 100000000) | ||
{ | ||
break; | ||
} | ||
|
||
sum += p; | ||
p = 4 * x * x; | ||
|
||
if (p <= 100000000) | ||
{ | ||
sum += p; | ||
} | ||
|
||
long nextX = x + x + 3 * y; | ||
long nextY = x + y + y; | ||
|
||
x = nextX; | ||
y = nextY; | ||
} | ||
|
||
return euler_submit(94, sum, start); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters