-
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
470a8a7
commit b324bf9
Showing
18 changed files
with
324 additions
and
23 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
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,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: |
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,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 |
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,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: |
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,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 |
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
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,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; |
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,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; |
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
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
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,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; | ||
} |
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
Oops, something went wrong.