feat(26): complete reciprocal cycles
This commit is contained in:
parent
2c5ed7a6d3
commit
95c8f4738f
3 changed files with 62 additions and 2 deletions
|
@ -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
|
||||
|
|
57
src/bin/26.rs
Normal file
57
src/bin/26.rs
Normal file
|
@ -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);
|
||||
}
|
|
@ -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<u8>,
|
||||
|
@ -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!(
|
||||
|
|
Loading…
Reference in a new issue