Skip to content

Commit

Permalink
Add id0068 (c and python)
Browse files Browse the repository at this point in the history
  • Loading branch information
ishanpranav committed Jan 27, 2024
1 parent 3d3d015 commit 5bcaebf
Show file tree
Hide file tree
Showing 19 changed files with 152 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Licensed under the MIT License.

[*]
cpp_indent_braces=false
cpp_indent_multi_line_relative_to=innermost_parenthesis
Expand Down
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# Licensed under the MIT License.

*.sh text eol=lf
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Licensed under the MIT License.

a.out
*.exe
*.o
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Licensed under the MIT License.

CC = gcc
CFLAGS = -O3 -pedantic -std=c99 -Wall -Wextra

Expand Down Expand Up @@ -263,5 +265,8 @@ id0065.o: src/id0065.c euler.o lp_string.o
id0066.o: src/id0066.c euler.o math.o
$(CC) $(CFLAGS) $< -o $@ euler.o math.o -lgmp -lm

id0068.o: src/id0068.c euler.o list.o math.o permutation_iterator.o
$(CC) $(CFLAGS) $< -o $@ euler.o list.o math.o permutation_iterator.o -lm

clean:
rm -rf *.o
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<!-- Licensed under the MIT License. -->

# Project Euler

Efficient Project Euler solutions in C language
Expand All @@ -17,7 +19,8 @@ languages. The most difficult problems are implemented in 10 languages.

| Difficulty | Language |
| :--------: | -------------------------- |
| | [C](tools/test.sh) |
| (Trivial) | [Shell](tools/test.sh) |
| \> 0% | [C](tools/test.sh) |
| \> 10% | [Python](tools/test.py.sh) |
| \> 20% | [C\#](tools/test.cs.ps1) |

Expand Down Expand Up @@ -113,4 +116,4 @@ which is licensed under the GNU Lesser General Public License v3.0 (`LGPL-3.0`).
| 63 | [Powerful Digit Counts](src/id0063.c) | Number theory | Count | |
| 64 | [Odd Period Square Roots](src/id0064.c) | Algebra | Count | [Continued fraction expansion](https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Continued_fraction_expansion) |
| 65 | [Convergents of e](src/id0065.c) | Arbitrary-precision arithmetic | Count | |
| 66 | [Diophantine Equation](src/id0066.c) | Arbitrary-precision arithmetic | Maximum | [Pell\'s equation](https://mathworld.wolfram.com/PellEquation.html) |
| 66 | [Diophantine Equation](src/id0066.c) | Arbitrary-precision arithmetic | Maximum | [Pell\'s equation](https://mathworld.wolfram.com/PellEquation.html) |
12 changes: 11 additions & 1 deletion lib/euler.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,17 @@ long math_gcd(long a, long b);
* @param a the exponent.
* @return The number of digits in the expanded expression `b^a`.
*/
int math_length(int b, int a);
long long math_length(long long b, long long a);

/**
* Concatenates two integers.
*
* @param left the left digits.
* @param right the right digits.
* @return An integer formed by writing out the digits of `left` immediately
* followed by the digits of `right`.
*/
long long math_concat(long long left, long long right);

/**
* Reverses the digits of a given value.
Expand Down
10 changes: 5 additions & 5 deletions lib/factor_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void factor_next(FactorIterator iterator)
{
iterator->current = *iterator->iterator.current;
iterator->remainder = 0;
iterator->currentCount = 1;
iterator->exponent = 1;

return;
}
Expand All @@ -34,12 +34,12 @@ void factor_next(FactorIterator iterator)

iterator->current = *iterator->iterator.current;
iterator->remainder /= iterator->current;
iterator->currentCount = 1;
iterator->exponent = 1;

while (iterator->remainder % iterator->current == 0)
{
iterator->remainder /= iterator->current;
iterator->currentCount++;
iterator->exponent++;
}
}

Expand All @@ -50,7 +50,7 @@ int factor_divisor_count(long long n, Sieve primes)

for (factor_begin(&it, n, primes); !factor_end(&it); factor_next(&it))
{
result *= it.currentCount + 1;
result *= it.exponent + 1;
}

return result;
Expand All @@ -63,7 +63,7 @@ int factor_divisor_sum(long long n, Sieve primes)

for (factor_begin(&it, n, primes); !factor_end(&it); factor_next(&it))
{
result *= (pow(it.current, it.currentCount + 1) - 1) / (it.current - 1);
result *= (pow(it.current, it.exponent + 1) - 1) / (it.current - 1);
}

return result;
Expand Down
2 changes: 1 addition & 1 deletion lib/factor_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct FactorIterator
struct SieveIterator iterator;
long long current;
long long remainder;
int currentCount;
int exponent;
};

/** Iterates over the prime factors of a positive integer. */
Expand Down
9 changes: 7 additions & 2 deletions lib/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ bool math_is_polygonal(int s, long x, long* approxN)
return n == localApproxN;
}

int math_length(int b, int a)
long long math_length(long long b, long long a)
{
return 1 + (int)(a * log10(b));
return 1 + a * log10(b);
}

long long math_concat(long long left, long long right)
{
return left * pow(10, math_length(right, 1)) + right;
}
6 changes: 3 additions & 3 deletions src/id0018.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
#define BUFFER_SIZE 512
#define MAX_HEIGHT 100

int main(int count, LPString args[])
int main(int argc, LPString argv[])
{
int id;

if (count == 2)
if (argc == 2)
{
id = strtol(args[1], NULL, 10);
id = strtol(argv[1], NULL, 10);
}
else
{
Expand Down
5 changes: 0 additions & 5 deletions src/id0060.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
#include "../lib/sieve.h"
#define MAX_SEARCH 10000

long math_concat(int a, int b)
{
return a * pow(10, math_length(b, 1)) + b;
}

bool math_is_prime_pair(Sieve primes, int a, int b)
{
long concat = math_concat(a, b);
Expand Down
4 changes: 1 addition & 3 deletions src/id0060.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
from time import time

MAX_SEARCH = 10000

def math_concat(a: int, b: int) -> int: return a * 10 ** (int(log10(b)) + 1) + b

def math_is_prime_pair(a: int, b: int) -> int:
return isprime(math_concat(a, b)) and isprime(math_concat(b, a))
return isprime(int(str(a) + str(b))) and isprime(int(str(b) + str(a)))

start = time()
primes = list(primerange(2, MAX_SEARCH))
Expand Down
5 changes: 5 additions & 0 deletions src/id0067.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Licensed under the MIT License.

# Maximum Path Sum II

./../id0018.o 67
62 changes: 62 additions & 0 deletions src/id0068.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Licensed under the MIT License.

// Magic 5-gon Ring

#include <math.h>
#include "../lib/euler.h"
#include "../lib/permutation_iterator.h"

int main(void)
{
long long max = 0;
long long r[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
struct List l;
struct PermutationIterator it;
clock_t start = clock();

list_from_array(&l, r, 10);

for (permutation_begin(&it, &l); !it.end; permutation_next(&it))
{
if (r[0] > r[3] || r[0] > r[5] || r[0] > r[7] || r[0] > r[9] ||
(r[3] != 10 && r[5] != 10 && r[7] != 10 && r[9] != 10))
{
continue;
}

int sum = r[0] + r[1] + r[2];

if (r[2] + r[3] + r[4] != sum || r[4] + r[5] + r[6] != sum ||
r[6] + r[7] + r[8] != sum || r[8] + r[9] + r[1] != sum)
{
continue;
}

long long value = math_concat(r[0], r[1]);

value = math_concat(value, r[2]);

value = math_concat(value, r[3]);
value = math_concat(value, r[2]);
value = math_concat(value, r[4]);

value = math_concat(value, r[5]);
value = math_concat(value, r[4]);
value = math_concat(value, r[6]);

value = math_concat(value, r[7]);
value = math_concat(value, r[6]);
value = math_concat(value, r[8]);

value = math_concat(value, r[9]);
value = math_concat(value, r[8]);
value = math_concat(value, r[1]);

if (value > max)
{
max = value;
}
}

return euler_submit(68, max, start);
}
Empty file added src/id0068.csx
Empty file.
31 changes: 31 additions & 0 deletions src/id0068.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Licensed under the MIT License.

# Magic 5-gon Ring

from itertools import permutations
from time import time

maxString = ""
start = time()

for l in permutations([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]):
if l[0] > l[3] or l[0] > l[5] or l[0] > l[7] or l[0] > l[9]: continue
if l[3] != 10 and l[5] != 10 and l[7] != 10 and l[9] != 10: continue

sum = l[0] + l[1] + l[2]

if l[2] + l[3] + l[4] != sum or l[4] + l[5] + l[6] != sum: continue
if l[6] + l[7] + l[8] != sum or l[8] + l[9] + l[1] != sum: continue

string = "".join(map(str, [
l[0], l[1], l[2],
l[3], l[2], l[4],
l[5], l[4], l[6],
l[7], l[6], l[8],
l[9], l[8], l[1]
]))

if string > maxString:
maxString = string

print(f"0068{maxString:>64} {time() - start:.6f}")
2 changes: 0 additions & 2 deletions tools/test.c.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,3 @@ for i in {0060..0066};
do
./../id${i}.o
done

cat ./../data/id0067.txt | ./../id0018.o 67
9 changes: 8 additions & 1 deletion tools/test.py.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

python3 ../src/id0051.py

for i in {0060..0066};
for i in {0060..0062};
do
python3 ../src/id${i}.py
done

for i in {0064..0066};
do
python3 ../src/id${i}.py
done

python3 ../src/id0068.py
3 changes: 2 additions & 1 deletion tools/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ done

python3 ../src/id0065.py
./../id0066.o
cat ./../data/id0067.txt | ./../id0018.o 67
cat ./../data/id0067.txt | ./../src/id0067.sh
./../id0068.o

0 comments on commit 5bcaebf

Please sign in to comment.