How to resolve the algorithm Hofstadter-Conway $10,000 sequence step by step in the Rust programming language
How to resolve the algorithm Hofstadter-Conway $10,000 sequence step by step in the Rust programming language
Table of Contents
Problem Statement
The definition of the sequence is colloquially described as: Note that indexing for the description above starts from alternately the left and right ends of the list and starts from an index of one. A less wordy description of the sequence is: The sequence begins: Interesting features of the sequence are that:
The sequence is so named because John Conway offered a prize of $10,000 to the first person who could find the first position, p in the sequence where It was later found that Hofstadter had also done prior work on the sequence. The 'prize' was won quite quickly by Dr. Colin L. Mallows who proved the properties of the sequence and allowed him to find the value of n (which is much smaller than the 3,173,375,556 quoted in the NYT article).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Hofstadter-Conway $10,000 sequence step by step in the Rust programming language
Source code in the rust programming language
struct HofstadterConway {
current: usize,
sequence: Vec<usize>,
}
impl HofstadterConway {
/// Define a constructor for the struct.
fn new() -> HofstadterConway {
HofstadterConway {
current: 0,
sequence: vec![1, 1],
}
}
}
impl Default for HofstadterConway {
fn default() -> Self {
Self::new()
}
}
/// Implement the hofstadter q iteration sequence.
impl Iterator for HofstadterConway {
type Item = usize;
/// This gets called to fetch the next item of the iterator.
fn next(&mut self) -> Option<usize> {
let max_index = self.sequence.len() - 1;
let last_value = self.sequence[max_index];
if self.current > max_index {
let new_x = self.sequence[last_value - 1] + self.sequence[max_index - last_value + 1];
self.sequence.push(new_x);
}
self.current += 1;
Some(self.sequence[self.current - 1])
}
}
#[allow(clippy::cast_precision_loss)]
fn main() {
let mut hof = HofstadterConway::new();
let mut winning_num = 0_usize;
for p in 0..20 {
let max_hof = (2_usize.pow(p)..2_usize.pow(p + 1))
.map(|n| (n, hof.next().unwrap() as f64 / n as f64))
.fold(f64::NAN, |a, (n, b)| {
if b >= 0.55 {
winning_num = n;
}
a.max(b)
});
println!("2^{:>2}-2^{:>2}, {:>.8}", p, p + 1, max_hof);
}
println!("Winning number: {}", winning_num);
}
You may also check:How to resolve the algorithm Largest proper divisor of n step by step in the FOCAL programming language
You may also check:How to resolve the algorithm Loops/Infinite step by step in the Wart programming language
You may also check:How to resolve the algorithm Stream merge step by step in the Haskell programming language
You may also check:How to resolve the algorithm One-dimensional cellular automata step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Empty program step by step in the Dart programming language