Skip to content

Commit

Permalink
Solve p0357 in rust
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Jul 24, 2024
1 parent a7d2822 commit 6057c9c
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ LivInTheLookingGlass’s Project Euler solutions
| | Pypy 3.6+ |br| | | |Py-Cov| |
| | GraalPy 23.1+ | | |
+------------+-------------------------+--------+-------------------+
| Rust | 1.69+ | 15 | |Rust| |br| |
| Rust | 1.69+ | 16 | |Rust| |br| |
| | | | |Rs-Cov| |
+------------+-------------------------+--------+-------------------+
| Documentation | |Pages| |
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ Key:
+-----------+------------+------------+------------+------------+------------+------------+
|:prob:`206`| | | | |:py-d:`0206`| |
+-----------+------------+------------+------------+------------+------------+------------+
|:prob:`357`| | | | |:py-s:`0357`| |
|:prob:`357`| | | | |:py-s:`0357`|:rs-d:`0357`|
+-----------+------------+------------+------------+------------+------------+------------+

.. toctree::
Expand Down
1 change: 1 addition & 0 deletions docs/rust.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Library Code

rust/digits
rust/fibonacci
rust/math
rust/primes

Problems Solved
Expand Down
18 changes: 18 additions & 0 deletions docs/rust/p0357.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Rust Implementation of Problem 357
==================================

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

Includes
--------

- `primes <./primes.html>`_

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

.. rust:fn:: p0357::p0357() -> i128
.. literalinclude:: ../../rust/src/p0357.rs
:language: rust
:linenos:
10 changes: 6 additions & 4 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ use rstest::rstest;
#[cfg(test)]
use itertools::Itertools;

mod math;
mod primes;
seq!(N in 0001..=0012 {
mod p~N;
});
mod p0015;
mod p0034;
mod p0076;
mod math;
mod primes;
mod p0357;

type ProblemType = fn() -> i128;
type ProblemRef<'a> = (&'a str, ProblemType, i128);
const ANSWERS: [ProblemRef; 15] = [
const ANSWERS: [ProblemRef; 16] = [
("p0001", p0001::p0001, 233168),
("p0002", p0002::p0002, 4613732),
("p0003", p0003::p0003, 6857),
Expand All @@ -36,6 +37,7 @@ const ANSWERS: [ProblemRef; 15] = [
("p0015", p0015::p0015, 137846528820),
("p0034", p0034::p0034, 40730),
("p0076", p0076::p0076, 190569291),
("p0357", p0357::p0357, 1739023853137),
];

fn main() {
Expand All @@ -50,7 +52,7 @@ fn main() {
}

#[cfg(test)]
seq!(N in 0..15 {
seq!(N in 0..16 {
#[rstest]
#[timeout(Duration::new(60, 0))]
#(
Expand Down
39 changes: 39 additions & 0 deletions rust/src/p0357.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Project Euler Problem 357
A key note is that n // d is always also a factor, so it ends up being pairs. This means you can halve your search space
Problem:
Consider the divisors of 30: 1,2,3,5,6,10,15,30.
It can be seen that for every divisor d of 30, d+30/d is prime.
Find the sum of all positive integers n not exceeding 100 000 000
such that for every divisor d of n, d+n/d is prime.
*/
use std::collections::HashSet;

use crate::primes::{is_prime,primes_until,proper_divisors};

pub fn p0357() -> i128 {
let mut answer: u64 = 1 + 2; // don't bother trying 1, 2, they're correct
let prime_squares = HashSet::from_iter(
primes_until::<u64>(10001).map(|x| x * x)
);
for n in (6..100000000).step_by(4) {
// n can't be odd (unless 1) because then n + n/d is even, and can't be a multiple of 4 as shown below
broken = false;
for d in proper_divisors(n).chain(vec![n].into_iter()) {
if prime_squares.contains(d) || !is_prime(d + n / d) {
// this works because if n=kp^2, then whenever d=p, (p + kp^2/p) = (k+1)p, which isn't prime
// but since detecting if n % d^2 == 0 is expensive, I just wait for p^2 to show up
broken = true;
break;
}
}
if !broken {
answer += n;
}
}
return answer.into();
}

0 comments on commit 6057c9c

Please sign in to comment.