Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve p111 in rust #43

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions _data/answers.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ ID type size answer
92 int 32 8581146
97 int 64 8739992577
99 int 16 709
111 uint 64 0
118 uint 16 44680
123 int 16 21035
134 int 64 18613426663617118
Expand Down
2 changes: 2 additions & 0 deletions rust/src/include/problems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::p0069::p0069;
use crate::p0076::p0076;
use crate::p0077::p0077;
use crate::p0087::p0087;
use crate::p0111::p0111;
use crate::p0187::p0187;
use crate::p0357::p0357;
use crate::p0836::p0836;
Expand Down Expand Up @@ -50,6 +51,7 @@ seq!(N in 0001..=0020 {
76 => Some(( &76, p0076, false)),
77 => Some(( &77, p0077, false)),
87 => Some(( &87, p0087, false)),
111 => Some((&111, p0111, false)),
187 => Some((&187, p0187, false)),
357 => Some((&357, p0357, true)),
836 => Some((&836, p0836, false)),
Expand Down
1 change: 1 addition & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod p0069;
pub mod p0076;
pub mod p0077;
pub mod p0087;
pub mod p0111;
pub mod p0187;
pub mod p0357;
pub mod p0836;
Expand Down
2 changes: 2 additions & 0 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub mod p0069;
pub mod p0076;
pub mod p0077;
pub mod p0087;
pub mod p0111;
pub mod p0187;
pub mod p0357;
pub mod p0836;
Expand Down Expand Up @@ -97,6 +98,7 @@ seq!(N in 01..=20 {
#[case::problem_76(76)]
#[case::problem_77(77)]
#[case::problem_87(87)]
#[case::problem_111(111)]
#[case::problem_187(187)]
// #[case::problem_357(357)]
#[case::problem_836(836)]
Expand Down
30 changes: 30 additions & 0 deletions rust/src/p0111.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Project Euler Problem 111

*/
use std::iter::zip;

Check warning

Code scanning / clippy

unused import: std::iter::zip Warning

unused import: std::iter::zip

use crate::include::primes::primes_until;
use crate::include::utils::Answer;

pub fn p0111() -> Answer {
let ten_9: u64 = 10_u64.pow(9);
let ten_10: u64 = 10_u64.pow(10);
let mut answer: u64 = 0;

Check warning

Code scanning / clippy

variable does not need to be mutable Warning

variable does not need to be mutable

Check warning

Code scanning / clippy

variable does not need to be mutable Warning

variable does not need to be mutable

Check warning

Code scanning / clippy

unused variable: answer Warning

unused variable: answer

Check warning

Code scanning / clippy

unused variable: answer Warning

unused variable: answer
let mut subanswer: Vec<u64> = vec![0; 10];
let mut current: Vec<usize> = vec![0; 10];
for p in primes_until::<u64>(ten_10).filter(|x| *x > ten_9) {
let s = p.to_string();
for digit in 0..=9 {
let idx = digit as usize;
let count = s.bytes().filter(|b| *b == digit + b'0').count();
if count > current[idx] {
current[idx] = count;
subanswer[idx] = p;
} else if count == current[idx] {
subanswer[idx] += p;
}
Comment on lines +21 to +26

Check warning

Code scanning / clippy

if chain can be rewritten with match Warning

if chain can be rewritten with match
Comment on lines +21 to +26

Check warning

Code scanning / clippy

if chain can be rewritten with match Warning

if chain can be rewritten with match
}
}
return Answer::Int(subanswer.into_iter().sum::<u64>().into());
}
Loading