Skip to content

Commit

Permalink
Port p25, p30 from dev (which is now mostly not needed)
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Aug 15, 2024
1 parent 6e6b391 commit 50a7625
Show file tree
Hide file tree
Showing 13 changed files with 194 additions and 25 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ csharp/*/obj
csharp/*/bin
**/TestResults
docs/_build
rust/target
*/target
**/.coverage
**/.coverage.*
**/coverage
**/dist/*
docs/_static/test-js.html
javascript/src/lib/fallbacks
java/target
docs/languages*.svg
12 changes: 6 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ Olivia's Project Euler Solutions
+------------+----------------------------+--------+-------------------+
| JavaScript | Node 12+ |br| | 25 | |JavaScript| |br| |
| | Bun 1.0+ |br| | | |Js-Cov| |br| |
| | Browser [2]_ | | |CodeQL| |br| |
| | Browser [#]_ | | |CodeQL| |br| |
| | | | |ESLint| |
+------------+----------------------------+--------+-------------------+
| Python | CPython 3.6+ |br| | 79 | |Python| |br| |
| | Pypy 3.6+ |br| | | |Py-Cov| |br| |
| | GraalPy 23.1+ |br| | | |CodeQL| |br| |
| | Browser [3]_ | | |PythonLint| |
| | Browser [#]_ | | |PythonLint| |
+------------+----------------------------+--------+-------------------+
| Rust | 1.69+ |br| | 28 | |Rust| |br| |
| | Browser [4]_ | | |Rs-Cov| |br| |
| | Browser [#]_ | | |Rs-Cov| |br| |
| | | | |RustClippy| |
+------------+----------------------------+--------+-------------------+

Expand All @@ -106,11 +106,11 @@ Olivia's Project Euler Solutions
<a href="https://euler.oliviaappleton.com/_static/test-rs.html" target="_blank">click here!</a>

.. [1] This is the earliest standard the MSVC explicitly supports.
.. [2] While these solutions do run in most browsers, they need to be bundled with WebPack 5.93+ & Babel 7.25+ first,
.. [#] While these solutions do run in most browsers, they need to be bundled with WebPack 5.93+ & Babel 7.25+ first,
and these tests not yet automated as in Nodejs and Bun. To run these tests yourself, |test-js-link|
.. [3] While these solutions do run in most browsers, they need to be bundled with Pyodide 0.26.2+ first, and these
.. [#] While these solutions do run in most browsers, they need to be bundled with Pyodide 0.26.2+ first, and these
tests are not yet automated as in CPython and pypy. To run these tests yourself, |test-py-link|
.. [4] While these solutions will run in most browsers, they need to be bundled with wasm-pack 0.2+ first, and these
.. [#] While these solutions will run in most browsers, they need to be bundled with wasm-pack 0.2+ first, and these
tests are not yet automated as on non-web platforms. To run these tests yourself, |test-rs-link|
Coverage
Expand Down
6 changes: 6 additions & 0 deletions c/src/include/bcd.h
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,12 @@ inline BCD_int shift_bcd_right(BCD_int a, uintmax_t tens) {
return div_bcd_pow_10(a, tens);
}

void iadd_bcd(BCD_int *const x, const BCD_int y) {
BCD_int ret = add_bcd(*x, y);
free_BCD_int(x);
*x = ret;
}

uintmax_t bcd_to_unsigned(BCD_int a) {
if (a.zero)
return 0;
Expand Down
4 changes: 4 additions & 0 deletions c/src/include/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
#define min(a, b) (((a) < (b)) ? (a) : (b))
#endif

#ifndef swap
#define swap(x, y, T) do { T SWAP = x; x = y; y = SWAP; } while (0)
#endif

#if !(CL_COMPILER || TCC_COMPILER)
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
Expand Down
28 changes: 14 additions & 14 deletions c/src/p0022.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/**
* Project Euler Problem 22
*
* I know that this could be done faster with a traditional for loop, but I wanted
* to see if iterators were reasonably possible in C, since it makes the prime
* number infrastructure a lot easier to set up.
*
* Problem:
*
* If we list all the natural numbers below 10 that are multiples of 3 or 5, we
* 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.
*/
/*
Project Euler Problem 22
I know that this could be done faster with a traditional for loop, but I wanted
to see if iterators were reasonably possible in C, since it makes the prime
number infrastructure a lot easier to set up.
Problem:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we
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.
*/
#ifndef EULER_P0022
#define EULER_P0022
#include <stdio.h>
Expand Down
54 changes: 54 additions & 0 deletions c/src/p0025.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Project Euler Problem 25
Problem:
The Fibonacci sequence is defined by the recurrence relation:
Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.
Hence the first 12 terms will be:
F1 = 1
F2 = 1
F3 = 2
F4 = 3
F5 = 5
F6 = 8
F7 = 13
F8 = 21
F9 = 34
F10 = 55
F11 = 89
F12 = 144
The 12th term, F12, is the first term to contain three digits.
What is the index of the first term in the Fibonacci sequence to contain 1000 digits?
*/
#ifndef EULER_P0025
#define EULER_P0025
#include <stdio.h>
#include "include/bcd.h"

unsigned long long p0025() {
unsigned long long answer = 2;
BCD_int a = BCD_one, b = BCD_one;
while (b.decimal_digits < 1000) {
iadd_bcd(&a, b);
swap(a, b, BCD_int);
answer++;
}
free_BCD_int(&a);
free_BCD_int(&b);
return answer;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
unsigned long long answer = p0025();
printf("%llu\n", answer);
return 0;
}
#endif
#endif
47 changes: 47 additions & 0 deletions c/src/p0030.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Project Euler Problem 30
Problem:
Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:
1634 = 1^4 + 6^4 + 3^4 + 4^4
8208 = 8^4 + 2^4 + 0^4 + 8^4
9474 = 9^4 + 4^4 + 7^4 + 4^4
As 1 = 1^4 is not a sum it is not included.
The sum of these numbers is 1634 + 8208 + 9474 = 19316.
Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
*/
#ifndef EULER_P0030
#define EULER_P0030
#include <stdio.h>
#include "include/digits.h"

unsigned long long p0030() {
unsigned long long answer = 0, sum, tmp;
for (unsigned long long i = 2; i < 1000000; i++) {
digit_counter dc = digits(i);
sum = 0;
while (!dc.exhausted) {
tmp = next(dc);
sum += tmp * tmp * tmp * tmp * tmp;
}
if (sum == i) {
answer += i;
}
free_iterator(dc);
}
return answer;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
unsigned long long answer = p0030();
printf("%llu\n", answer);
return 0;
}
#endif
#endif
3 changes: 3 additions & 0 deletions c/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "src/p0017.c"
#include "src/p0020.c"
#include "src/p0022.c"
#include "src/p0025.c"
#include "src/p0034.c"
#include "src/p0076.c"
#include "src/p0836.c"
Expand Down Expand Up @@ -48,6 +49,8 @@ const Answer answers[] = {
{17, 21124, p0017},
{20, 648, p0020},
{22, 871198282, p0022},
{25, 4782, p0025},
{30, 443839, p0030},
{34, 40730, p0034},
{76, 190569291, (unsigned long long (*)()) p0076},
};
Expand Down
2 changes: 2 additions & 0 deletions c/test_euler.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
*range(1, 18),
20,
22,
25,
30,
34,
76,
836,
Expand Down
4 changes: 2 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ Problems Solved
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`24` | | | | | |:py-d:`0024`|:rs-d:`0024`|
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`25` | | | | | |:py-d:`0025`| |
|:prob:`25` |:c-d:`0025` | | | | |:py-d:`0025`| |
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`27` | | | | |:js-d:`0027`|:py-d:`0027`|:rs-d:`0027`|
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`28` | | | | | |:py-d:`0028`| |
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`29` | | | | | |:py-d:`0029`| |
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`30` | | | | | |:py-d:`0030`| |
|:prob:`30` |:c-d:`0030` | | | | |:py-d:`0030`| |
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`31` | | | | | |:py-d:`0031`| |
+-----------+------------+------------+------------+------------+------------+------------+------------+
Expand Down
4 changes: 4 additions & 0 deletions docs/src/c/lib/bcd.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ It was also a good exercise in x86 assembly, as several portions are accellerate
Returns ``x // 10**tens``.
.. c:function:: void iadd_bcd(BCD_int *const x, const BCD_int y)
Transforms ``x`` to be ``x + y`` without needing to make a new assignment.
.. c:function:: signed char cmp_bcd(BCD_int x, BCD_int y)
Returns 1 if ``x > y``, -1 if ``y > x``, and otherwise 0.
Expand Down
25 changes: 25 additions & 0 deletions docs/src/c/p0025.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
C Implementation of Problem 25
==============================

View source code :source:`c/src/p0025.c`

Includes
--------

- `bcd.h <./lib/bcd.html>`__

Solution
--------

.. c:function:: unsigned long long p0025()
.. c:function:: int main(int argc, char const *argv[])
.. note::
This function is only present in the Python test runner, or when compiling as a standalone program.
It is not present when compiling for the Unity test runner.
.. literalinclude:: ../../../c/src/p0025.c
:language: C
:linenos:
25 changes: 25 additions & 0 deletions docs/src/c/p0030.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
C Implementation of Problem 30
==============================

View source code :source:`c/src/p0030.c`

Includes
--------

- `digits.h <./lib/digits.html>`__

Solution
--------

.. c:function:: unsigned long long p0030()
.. c:function:: int main(int argc, char const *argv[])
.. note::
This function is only present in the Python test runner, or when compiling as a standalone program.
It is not present when compiling for the Unity test runner.
.. literalinclude:: ../../../c/src/p0030.c
:language: C
:linenos:

0 comments on commit 50a7625

Please sign in to comment.