From 95c8f4738fdacd7b0f1a465f807519f03c0daa9e Mon Sep 17 00:00:00 2001 From: newt Date: Wed, 9 Oct 2024 18:10:13 +0100 Subject: [PATCH] feat(26): complete reciprocal cycles --- readme.md | 4 ++-- src/bin/26.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++ src/commands/run.rs | 3 +++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/bin/26.rs diff --git a/readme.md b/readme.md index d4be15d..6acad87 100644 --- a/readme.md +++ b/readme.md @@ -17,7 +17,7 @@ Here is a [link to my profile](https://projecteuler.net/progress=newtykins). ## Challenge Completion -### 28 out of 100 public challenges completed. +### 29 out of 100 public challenges completed. - [x] 1 - [Multiples of 3 or 5](src/bin/1.rs) - [x] 2 - [Even Fibonacci numbers](src/bin/2.rs) @@ -44,7 +44,7 @@ Here is a [link to my profile](https://projecteuler.net/progress=newtykins). - [x] 23 - Non-abundant sums - [x] 24 - Lexicographic permutations - [x] 25 - 1000-digit Fibonacci number -- [ ] 26 - Reciprocal cycles +- [x] 26 - Reciprocal cycles - [x] 27 - [Quadratic primes](src/bin/27.rs) - [ ] 28 - Number spiral diagonals - [ ] 29 - Distinct powers diff --git a/src/bin/26.rs b/src/bin/26.rs new file mode 100644 index 0000000..888ae94 --- /dev/null +++ b/src/bin/26.rs @@ -0,0 +1,57 @@ +/* +Problem 26 - Reciprocal Cycles + +A unit fraction contains $1$ in the numerator. The decimal representation of the unit fractions with denominators $2$ to $10$ are given: +\begin{align} +1/2 &= 0.5\\ +1/3 &=0.(3)\\ +1/4 &=0.25\\ +1/5 &= 0.2\\ +1/6 &= 0.1(6)\\ +1/7 &= 0.(142857)\\ +1/8 &= 0.125\\ +1/9 &= 0.(1)\\ +1/10 &= 0.1 +\end{align} +Where $0.1(6)$ means $0.166666\cdots$, and has a $1$-digit recurring cycle. It can be seen that $1/7$ has a $6$-digit recurring cycle. +Find the value of $d \lt 1000$ for which $1/d$ contains the longest recurring cycle in its decimal fraction part. +*/ + +fn cycle_length(d: usize) -> usize { + let mut remainders = vec![0; d]; + let mut value = 1; + let mut position = 0; + + while remainders[value] == 0 && value != 0 { + remainders[value] = position; + value *= 10; + value %= d; + position += 1; + } + + if value == 0 { + return 0; + } else { + return position - remainders[value]; + } +} + +fn longest_cycle(limit: usize) -> usize { + let mut max_length = 0; + let mut result = 0; + + for d in 2..limit { + let length = cycle_length(d); + if length > max_length { + max_length = length; + result = d; + } + } + + result +} + +pub fn main() { + let number = longest_cycle(1000); + println!("The value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part is: {}", number); +} diff --git a/src/commands/run.rs b/src/commands/run.rs index a6cea95..fb4cd31 100644 --- a/src/commands/run.rs +++ b/src/commands/run.rs @@ -58,6 +58,8 @@ mod two; mod twenty_four; #[path = "../bin/25.rs"] mod twenty_five; +#[path = "../bin/26.rs"] +mod twenty_six; pub async fn execute( problem: Option, @@ -102,6 +104,7 @@ pub async fn execute( 23 => twenty_three::main(), 24 => twenty_four::main(), 25 => twenty_five::main(), + 26 => twenty_six::main(), _ => { exists = false; println!(