-
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
488ce36
commit c420c00
Showing
12 changed files
with
164 additions
and
20 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,20 @@ | ||
Rust Implementation of Problem 44 | ||
================================= | ||
|
||
View source code :source:`rust/src/p0044.rs` | ||
|
||
Includes | ||
-------- | ||
|
||
- `math <./lib/math.html>`_ | ||
|
||
Problem Solution | ||
---------------- | ||
|
||
.. rust:fn:: pub fn p0044::p0044() -> utils::Answer | ||
.. literalinclude:: ../../../rust/src/p0044.rs | ||
:language: rust | ||
:linenos: | ||
|
||
.. tags:: figurate-number |
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 187 | ||
================================== | ||
|
||
View source code :source:`rust/src/p0187.rs` | ||
|
||
Includes | ||
-------- | ||
|
||
- `primes <./lib/primes.html>`_ | ||
|
||
Problem Solution | ||
---------------- | ||
|
||
.. rust:fn:: pub fn p0187::p0187() -> utils::Answer | ||
.. literalinclude:: ../../../rust/src/p0187.rs | ||
:language: rust | ||
:linenos: | ||
|
||
.. tags:: factorization, prime-number, rust-iterator |
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
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,44 @@ | ||
/* | ||
Project Euler Problem 44 | ||
Problem: | ||
Pentagonal numbers are generated by the formula, Pn=n(3n−1)/2. The first ten pentagonal numbers are: | ||
1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ... | ||
It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their difference, 70 − 22 = 48, is not pentagonal. | ||
Find the pair of pentagonal numbers, Pj and Pk, for which their sum and difference are pentagonal and D = ``|Pk − Pj|`` | ||
is minimised; what is the value of D? | ||
*/ | ||
use std::cmp::min; | ||
|
||
use crate::include::math::pentagonal; | ||
use crate::include::utils::Answer; | ||
|
||
|
||
fn is_pentagonal(x: u64) -> u64 { | ||
let root = ((24 * x + 1) as f64).sqrt(); | ||
let iroot = root.round() as u64; | ||
if iroot == root { | ||
return 0; | ||
} | ||
if (1 + iroot) % 6 != 0 { | ||
return 0; | ||
} | ||
return (1 + iroot) / 6; | ||
} | ||
|
||
pub fn p0044() -> Answer { | ||
let mut d: u64 = 1_000_000_000_000; | ||
let pentagonals = (1..2_500).map(pentagonal).collect::<Vec<u64>>(); | ||
for (idx, k) in pentagonals.iter().enumerate() { | ||
for j in &pentagonals[idx..] { | ||
if is_pentagonal(j + k) != 0 && is_pentagonal(k - j) != 0 { | ||
d = min(d, (k - j).abs()); | ||
} | ||
} | ||
} | ||
return Answer::Int(d.into()); | ||
} |
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,43 @@ | ||
/* | ||
Project Euler Problem 187 | ||
I was able to chain this with a previous problem. Probably a suboptimal | ||
solution because of it, but it felt prettier this way. | ||
I was able to add a short-circuited fail case to the is_prime() method, though | ||
Revision 1: | ||
Switched to a set comprehension with caching. This means that I can remove it | ||
from the slow list. | ||
Problem: | ||
A composite is a number containing at least two prime factors. For example, | ||
15 = 3 × 5; 9 = 3 × 3; 12 = 2 × 2 × 3. | ||
There are ten composites below thirty containing precisely two, not necessarily | ||
distinct, prime factors: 4, 6, 9, 10, 14, 15, 21, 22, 25, 26. | ||
How many composite integers, n < 10**8, have precisely two, not necessarily | ||
distinct, prime factors? | ||
*/ | ||
use std::collections::HashSet; | ||
|
||
use crate::include::primes::primes_until; | ||
use crate::include::utils::Answer; | ||
|
||
pub fn p0187() -> Answer { | ||
let ten_8: u64 = 10_u64.pow(8); | ||
let cached_primes: Vec<u64> = primes_until::<u64>(ten_8 / 2 + 1).collect(); | ||
let mut seen: HashSet<u64> = HashSet::new(); | ||
for &y in cached_primes.iter() { | ||
for &x in cached_primes.iter() { | ||
if x >= ten_8 / y { | ||
break; | ||
} | ||
seen.insert(x * y); | ||
} | ||
} | ||
return Answer::Int((seen.len() as u64).into()); | ||
} |