Skip to content

Commit

Permalink
Port Javascript solution to rust, c#
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Jul 3, 2024
1 parent cac466f commit e5ab1d8
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 46 deletions.
18 changes: 0 additions & 18 deletions .github/workflows/javascript.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,6 @@ on:
- .github/workflows/javascript.yml

jobs:
javascript-lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Use Node.js 22
uses: actions/setup-node@v3
with:
node-version: 22
cache: 'npm'
cache-dependency-path: 'javascript/package-lock.json'

- name: Fetch ESLint
run: make jsdependencies; npm install eslint-config-google@latest eslint@>=5.16.0

- name: Lint
run: make jslint

javascript:
strategy:
fail-fast: false
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/rust-clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# More details at https://github.com/rust-lang/rust-clippy
# and https://rust-lang.github.io/rust-clippy/

name: rust-clippy analyze
name: Rust-Clippy

on:
push:
Expand Down
4 changes: 4 additions & 0 deletions .mailmap
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Olivia Appleton <[email protected]> Gabe Appleton <[email protected]>
Olivia Appleton <[email protected]> Gabe Appleton <[email protected]>
Olivia Appleton <[email protected]> Olivia Appleton <[email protected]>
Olivia Appleton <[email protected]> Olivia Appleton <[email protected]>
54 changes: 30 additions & 24 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,41 @@ LivInTheLookingGlass’s Project Euler solutions
:target: https://github.com/LivInTheLookingGlass/Euler/actions/workflows/rust.yml
.. |CodeQL| image:: https://github.com/LivInTheLookingGlass/Euler/actions/workflows/codeql.yml/badge.svg
:target: https://github.com/LivInTheLookingGlass/Euler/actions/workflows/codeql.yml
.. |ESLint| image:: https://github.com/LivInTheLookingGlass/Euler/actions/workflows/eslint.yml/badge.svg
:target: https://github.com/LivInTheLookingGlass/Euler/actions/workflows/eslint.yml
.. |RustClippy| image:: https://github.com/LivInTheLookingGlass/Euler/actions/workflows/rust-clippy.yml/badge.svg
:target: https://github.com/LivInTheLookingGlass/Euler/actions/workflows/rust-clippy.yml
.. |Pages| image:: https://github.com/LivInTheLookingGlass/Euler/actions/workflows/pages.yml/badge.svg
:target: https://github.com/LivInTheLookingGlass/Euler/actions/workflows/pages.yml
.. |br| raw:: html

<br/>
.. |total| replace:: 642
.. |total| replace:: 897

+------------+---------------------+--------------+--------------+
| Language | Version | Solved | Status |
+============+=====================+==============+==============+
| C | C11+ in: ``gcc``, | 17 / |total| | |C| |
| | |br| ``clang``, | | |
| | ``msvc``, |br| | | |
| | ``pcc``, ``tcc`` | | |
+------------+---------------------+--------------+--------------+
| C# | .NET 2+ | 1 / |total| | |C#| |
+------------+---------------------+--------------+--------------+
| JavaScript | Node 12+ | 2 / |total| | |JavaScript| |
+------------+---------------------+--------------+--------------+
| Python | CPython 3.6+ |br| | 70 / |total| | |Python| |
| | Pypy 3.8+ |br| | | |
| | GraalPy 23.1+ | | |
+------------+---------------------+--------------+--------------+
| Rust | 1.1+ | 1 / |total| | |Rust| |
+------------+---------------------+--------------+--------------+
| CodeQL Scanning | |CodeQL| |
+-------------------------------------------------+--------------+
| Documentation (in progress) | |Pages| |
+-------------------------------------------------+--------------+
+------------+---------------------+--------------+---------------+
| Language | Version | Solved | Status |
+============+=====================+==============+===============+
| C | C11+ in: ``gcc``, | 17 / |total| | |C| |
| | |br| ``clang``, | | |
| | ``msvc``, |br| | | |
| | ``pcc``, ``tcc`` | | |
+------------+---------------------+--------------+---------------+
| C# | .NET 2+ | 2 / |total| | |C#| |
+------------+---------------------+--------------+---------------+
| JavaScript | Node 12+ | 2 / |total| | |JavaScript| |
+------------+---------------------+--------------+---------------+
| Python | CPython 3.6+ |br| | 70 / |total| | |Python| |
| | Pypy 3.8+ |br| | | |
| | GraalPy 23.1+ | | |
+------------+---------------------+--------------+---------------+
| Rust | 1.1+ | 2 / |total| | |Rust| |
+------------+---------------------+--------------+---------------+
| Documentation (in progress) | |Pages| |
+-------------------------------------------------+---------------+
| Code Scanning | |CodeQL| |br| |
| | |ESLint| |br| |
| | |RustClippy| |
+-------------------------------------------------+---------------+

This is the repository I keep for prospective employers to look at
project Euler code that I have written.
Expand All @@ -64,8 +70,8 @@ Makefile will direct recipes using prefixes

- ``make c*`` will go to the c Makefile
- ``make cs*`` will go to the csharp Makefile
- ``make py*`` will go to the python Makefile
- ``make js*`` will go to the javascript Makefile
- ``make py*`` will go to the python Makefile
- ``make rs*`` will go to the rust Makefile
- Any make recipes supported by Sphinx will be redirected to the docs Makefile
- All other recipes will be run for all languages
Expand Down
1 change: 1 addition & 0 deletions csharp/Euler.Test/test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static IEnumerable<object[]> Data()
{
yield return new object[] { typeof(p0000), 0 };
yield return new object[] { typeof(p0001), 233168 };
yield return new object[] { typeof(p0002), 4613732 };
}

[Theory]
Expand Down
39 changes: 39 additions & 0 deletions csharp/Euler/p0002.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Project Euler Problem 2
This is a port of the optimized version found in python. For a proof of why this
works, see that implementation
Problem:
Each new term in the Fibonacci sequence is generated by adding the previous two
terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed
four million, find the sum of the even-valued terms.
*/
using System;

namespace Euler
{
public class p0002 : IEuler
{
public Task<Int64> Answer()
{
Int64 answer = 0,
i = 2,
j = 8,
tmp = 0;
while (i < 4000000)
{
answer += i;
tmp = 4 * j + i;
i = j;
j = tmp;
}
return Task.FromResult<Int64>(answer);
}
}
}
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
'sphinx_csharp.csharp',
]
if 'TERMUX_VERSION' not in os.environ:
extensions.append('hawkmoth')
# extensions.append('hawkmoth')
extensions.append('sphinx_js')
extensions.append('sphinx_rust')

Expand Down
2 changes: 1 addition & 1 deletion docs/csharp/p0001.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
C# Implementation of Problem 1
==============================

View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler/blob/master/c/p0001.c>`_
View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler/blob/master/csharp/Euler/p0001.cs>`_

.. csharp:namespace:: Euler
Expand Down
16 changes: 16 additions & 0 deletions docs/csharp/p0002.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
C# Implementation of Problem 2
==============================

View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler/blob/master/csharp/Euler/p0002.cs>`_

.. csharp:namespace:: Euler
.. csharp:class:: p0002
.. csharp:inherits:: Euler.IEuler
.. csharp:method:: Task<Int64> Answer()
.. literalinclude:: ../../csharp/Euler/p0002.cs
:language: csharp
:linenos:
2 changes: 1 addition & 1 deletion docs/rust/p0001.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Rust Implementation of Problem 1
================================

View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler/blob/master/rust/p0001.rs>`_
View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler/blob/master/rust/src/p0001.rs>`_

.. rust:function:: rust::p0001::p0001
Expand Down
10 changes: 10 additions & 0 deletions docs/rust/p0002.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Rust Implementation of Problem 2
================================

View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler/blob/master/rust/src/p0002.rs>`_

.. rust:function:: rust::p0002::p0002
.. literalinclude:: ../../rust/src/p0002.rs
:language: rust
:linenos:
2 changes: 2 additions & 0 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ use seq_macro::seq;
use rstest::rstest;

mod p0001;
mod p0002;

const ANSWERS: [(&str, fn() -> u64, u64); 1] = [
("p0001", p0001::p0001, 233168)
("p0002", p0002::p0002, 4613732)
];

fn main() {
Expand Down
13 changes: 13 additions & 0 deletions rust/src/p0001.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/*
Project Euler Problem 1
I did this in the traditional way, as I'm mostly using this folder as a way
to learn Rust
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.
*/
pub fn p0001() -> u64 {
let mut answer: u64 = 0;
for i in (0..1000).step_by(3) {
Expand Down
29 changes: 29 additions & 0 deletions rust/src/p0002.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Project Euler Problem 2
This is a port of the optimized version found in python. For a proof of why this
works, see that implementation
Problem:
Each new term in the Fibonacci sequence is generated by adding the previous two
terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed
four million, find the sum of the even-valued terms.
*/
pub fn p0002() -> u64 {
let mut answer = 0;
let mut i = 2;
let mut j = 8;
let mut tmp = 0;
while i < 4000000 {
answer += i;
tmp = 4 * j + i;
i = j;
j = tmp;
}
return answer;
}

0 comments on commit e5ab1d8

Please sign in to comment.