Skip to content

Commit

Permalink
Solve p28 in js, rust
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Sep 9, 2024
1 parent 470a8a7 commit b324bf9
Show file tree
Hide file tree
Showing 18 changed files with 324 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Olivia's Project Euler Solutions
| | Microsoft, Oracle, |br| | | |CodeQL| |br| |
| | Semeru, Temurin, & Zulu | | |Java-lint| |
+------------+----------------------------+--------+-------------------+
| JavaScript | Node 12+ |br| | 33 | |JavaScript| |br| |
| JavaScript | Node 12+ |br| | 34 | |JavaScript| |br| |
| | Bun 1.0+ |br| | | |Js-Cov| |br| |
| | Browser [#]_ | | |CodeQL| |br| |
| | | | |ESLint| |
Expand All @@ -98,7 +98,7 @@ Olivia's Project Euler Solutions
| | GraalPy 23.1+ |br| | | |CodeQL| |br| |
| | Browser [#]_ | | |PythonLint| |
+------------+----------------------------+--------+-------------------+
| Rust | Rust 1.69+ |br| | 43 | |Rust| |br| |
| Rust | Rust 1.69+ |br| | 44 | |Rust| |br| |
| | Browser [#]_ | | |Rs-Cov| |br| |
| | | | |RustClippy| |
+------------+----------------------------+--------+-------------------+
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Problems Solved
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`027`| | | | |:js-d:`0027`| |:py-d:`0027`|:rs-s:`0027`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`028`| | | | | |:lu-d:`0028`|:py-d:`0028`| |
|:prob:`028`| | | | |:js-d:`0028`|:lu-d:`0028`|:py-d:`0028`|:rs-s:`0028`|
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`029`| | | | | | |:py-d:`0029`| |
+-----------+-----------+------------+------------+------------+------------+------------+------------+------------+
Expand Down
12 changes: 12 additions & 0 deletions docs/src/javascript/lib/ranges.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ranges.js
=========

View source code :source:`javascript/src/lib/ranges.js`

.. js:autofunction:: ranges.rangeEntry3

.. js:autofunction:: ranges.rangeEntry4

.. literalinclude:: ../../../../javascript/src/lib/ranges.js
:language: javascript
:linenos:
20 changes: 20 additions & 0 deletions docs/src/javascript/p0028.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
JavaScript Implementation of Problem 28
=======================================

View source code :source:`javascript/src/p0028.js`

Includes
--------

- `ranges <./lib/ranges.html>`_

Problem Solution
----------------

.. js:autofunction:: p0028

.. literalinclude:: ../../../javascript/src/p0028.js
:language: javascript
:linenos:

.. tags:: grid-pattern
25 changes: 25 additions & 0 deletions docs/src/rust/lib/ranges.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
ranges.rs
=======

View source code :source:`rust/src/include/ranges.rs`

Includes
--------

- :external:rust:trait:`num_traits::NumAssign`
- :external:rust:fn:`num_traits::one`
- :external:rust:fn:`num_traits::zero`
- :external:rust:trait:`std::cmp::PartialOrd`

.. rust:fn:: pub fn ranges::range_entry3<I>(start: I, idx: I, step: I) -> I where I: NumAssign
Returns the factorial of a given number. Note that it only accepts a ``u8`` because
any number that requires a larger type is *guaranteed* to overflow.

.. rust:fn:: pub fn ranges::range_entry4<I>(start: I, stop: I, idx: I, step: I) -> Option<I> where I: NumAssign + PartialOrd
Returns the number of ways to choose r items from a set of n.

.. literalinclude:: ../../../../rust/src/include/ranges.rs
:language: rust
:linenos:
20 changes: 20 additions & 0 deletions docs/src/rust/p0028.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Rust Implementation of Problem 28
=================================

View source code :source:`rust/src/problems/p0028.rs`

Includes
--------

- `ranges <./lib/ranges.html>`_

Problem Solution
----------------

.. rust:fn:: pub fn problems::p0028::p0028() -> utils::Answer
.. literalinclude:: ../../../rust/src/problems/p0028.rs
:language: rust
:linenos:

.. tags:: grid-pattern
1 change: 1 addition & 0 deletions javascript/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ Problems Solved
- ☒ `22 <./src/p0022.js>`__
- ☒ `23 <./src/p0023.js>`__
- ☒ `27 <./src/p0027.js>`__
- ☒ `28 <./src/p0028.js>`__
- ☒ `34 <./src/p0034.js>`__
- ☒ `35 <./src/p0035.js>`__
- ☒ `37 <./src/p0037.js>`__
Expand Down
1 change: 1 addition & 0 deletions javascript/euler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const answers = {
22: [require('./src/p0022.js'), false],
23: [require('./src/p0023.js'), false],
27: [require('./src/p0027.js'), false],
28: [require('./src/p0028.js'), false],
34: [require('./src/p0034.js'), false],
35: [require('./src/p0035.js'), false],
37: [require('./src/p0037.js'), false],
Expand Down
36 changes: 36 additions & 0 deletions javascript/src/lib/ranges.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Returns the number at position ``idx`` in a range starting at ``start`` and iterating by ``step``.
* @param {number} start
* @param {number} idx
* @param {number} step
* @return {number}
*/
function rangeEntry3(start, idx, step) {
return start + idx * step;
}
exports.rangeEntry3 = rangeEntry3;

/**
* Returns the number at position ``idx`` in the range [``start``, ``stop``) and iterating by ``step``.
* @param {number} start
* @param {number} stop
* @param {number} idx
* @param {number} step
* @return {number | undefined}
*/
function rangeEntry4(start, stop, idx, step) {
let length = 0;
if (step > 0 && start < stop) {
length = Math.floor(1 + (stop - 1 - start) / step);
} else if (step < 0 && start > stop) {
length = Math.floor(1 + (start - 1 - stop) / (-step));
}
if (idx < 0) {
idx = length + idx;
}
if (idx < 0 || idx >= length) {
return undefined;
}
return rangeEntry3(start, idx, step);
}
exports.rangeEntry4 = rangeEntry4;
63 changes: 63 additions & 0 deletions javascript/src/p0028.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Project Euler Problem 28
*
* 1 ends at 1^2
* 2-9 ends at 3^2
* 10-25 ends at 5^2
* 26-49 ends at 7^2
*
* perimeter[0] = (1, )
* perimeter[i] = ((2 * i - 1)^2, (2 * i + 1)^2]
*
* .. code-block::
*
* i = 1
* 2 3 4 5 6 7 8 9
* ^ ^ ^ ^
* 2i - 1, 2 * 2i - 1, 3 * 2i - 1, 4 * 2i - 1
*
* i = 2
* 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
* ^ ^ ^ ^
* 2i - 1, 2 * 2i - 1, 3 * 2i - 1, 4 * 2i - 1
*
* i = 3
* 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
* ^ ^ ^ ^
*
* the corners are:
* perimeter[i][x * 2i - 1 for x in (1, 2, 3, 4)]
*
* Revision 1:
*
* Extracted the code that finds the corners
*
* Problem:
*
* Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:
*
* 21 22 23 24 25
* 20 7 8 9 10
* 19 6 1 2 11
* 18 5 4 3 12
* 17 16 15 14 13
*
* It can be verified that the sum of the numbers on the diagonals is 101.
*
* What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?
*
* @return {number}
*/
exports.p0028 = function() {
let answer = 1;
for (let i = 1; i < (1002 / 2); i++) {
const start = (2 * i - 1)**2 + 1;
answer += rangeEntry(start, 1, (1 * 2 * i - 1)) +
rangeEntry(start, 1, (2 * 2 * i - 1)) +
rangeEntry(start, 1, (3 * 2 * i - 1)) +
rangeEntry(start, 1, (4 * 2 * i - 1));
}
return answer;
};

const rangeEntry = require('./lib/ranges.js').rangeEntry3;
64 changes: 44 additions & 20 deletions lua/src/p0028.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,49 @@
-- Project Euler Problem 28
--
--
-- 1 ends at 1^2
-- 2-9 ends at 3^2
-- 10-25 ends at 5^2
-- 26-49 ends at 7^2
--
-- perimeter[0] = (1, )
-- perimeter[i] = ((2 * i - 1)^2, (2 * i + 1)^2]
--
-- .. code-block::
--
-- i = 1
-- 2 3 4 5 6 7 8 9
-- ^ ^ ^ ^
-- 2i - 1, 2 * 2i - 1, 3 * 2i - 1, 4 * 2i - 1

local function _range_entry(start, stop, step, idx)
local length = 0
if step > 0 and start < stop
then
length = math.floor(1 + (stop - 1 - start) / step)
elseif step < 0 and start > stop
then
length = math.floor(1 + (start - 1 - stop) / (-step))
end
if idx < 0
then
idx = length + idx
end
if idx < 0 or idx >= length
then
return nil, "length is 0, cannot fetch"
end
return start + (step * idx)
end
-- i = 2
-- 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
-- ^ ^ ^ ^
-- 2i - 1, 2 * 2i - 1, 3 * 2i - 1, 4 * 2i - 1

-- i = 3
-- 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
-- ^ ^ ^ ^
--
-- the corners are:
-- perimeter[i][x * 2i - 1 for x in (1, 2, 3, 4)]
--
-- Revision 1:
--
-- Extracted the code that finds the corners
--
-- Problem:
--
-- Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:
--
-- 21 22 23 24 25
-- 20 7 8 9 10
-- 19 6 1 2 11
-- 18 5 4 3 12
-- 17 16 15 14 13
--
-- It can be verified that the sum of the numbers on the diagonals is 101.
--
-- What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?

return {
solution = function()
Expand Down
1 change: 1 addition & 0 deletions rust/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Problems Solved
- ☒ `24 <./src/problems/p0024.rs>`__
- ☒ `25 <./src/problems/p0025.rs>`__
- ☒ `27 <./src/problems/p0027.rs>`__
- ☒ `28 <./src/problems/p0028.rs>`__
- ☒ `34 <./src/problems/p0034.rs>`__
- ☒ `35 <./src/problems/p0035.rs>`__
- ☒ `36 <./src/problems/p0036.rs>`__
Expand Down
1 change: 1 addition & 0 deletions rust/src/include/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ pub mod fibonacci;
pub mod math;
pub mod primes;
pub mod problems;
pub mod ranges;
pub mod triangles;
pub mod utils;
1 change: 1 addition & 0 deletions rust/src/include/problems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ seq!(N in 0001..=0020 {
24 => Some(( &24, p0024::p0024, false)),
25 => Some(( &25, p0025::p0025, false)),
27 => Some(( &27, p0027::p0027, true)),
28 => Some(( &28, p0028::p0028, true)),
34 => Some(( &34, p0034::p0034, false)),
35 => Some(( &35, p0035::p0035, true)),
36 => Some(( &36, p0036::p0036, false)),
Expand Down
32 changes: 32 additions & 0 deletions rust/src/include/ranges.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::cmp::PartialOrd;

use num_traits::{one,zero,NumAssign};

pub fn range_entry3<I>(start: I, idx: I, step: I) -> I
where I: NumAssign
{
return start + idx * step;
}

pub fn range_entry4<I>(start: I, stop: I, idx: I, step: I) -> Option<I>
where I: NumAssign + PartialOrd
{
let length = if step > zero() && start < stop {
one::<I>() + (stop - one() - start) / step
} else if step < zero() && start > stop {
one::<I>() + (start - one() - stop) / (-step)
} else {
zero::<I>()
};

let i = if idx < zero() {
length + idx
} else {
idx
};

if i < length {
return Some(range_entry3(start, i, step));
}
return None;
}
1 change: 1 addition & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ seq!(N in 01..=20 {
#[case::problem_24(24)]
#[case::problem_25(25)]
#[case::problem_27(27)]
#[case::problem_28(28)]
#[case::problem_34(34)]
#[case::problem_35(35)]
#[case::problem_36(36)]
Expand Down
1 change: 1 addition & 0 deletions rust/src/problems/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ seq!(N in 0001..=0025 {
pub mod p~N;
});
pub mod p0027;
pub mod p0028;
seq!(N in 0034..=0037 {
pub mod p~N;
});
Expand Down
Loading

0 comments on commit b324bf9

Please sign in to comment.