September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -1,14 +1,7 @@
|
|||
#![feature(conservative_impl_trait)]
|
||||
|
||||
fn main() {
|
||||
for num in fibonacci_gen(10) {
|
||||
println!("{}", num);
|
||||
fn fib(n: u32) -> u32 {
|
||||
match n {
|
||||
0 => 0,
|
||||
1 => 1,
|
||||
n => fib(n - 1) + fib(n - 2),
|
||||
}
|
||||
}
|
||||
|
||||
fn fibonacci_gen(terms: i32) -> impl Iterator<Item=u64> {
|
||||
let sqrt_5 = 5.0f64.sqrt();
|
||||
let p = (1.0 + sqrt_5) / 2.0;
|
||||
let q = 1.0/p;
|
||||
(1..terms).map(move |n| ((p.powi(n) + q.powi(n)) / sqrt_5 + 0.5) as u64)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,29 +1,14 @@
|
|||
use std::mem;
|
||||
|
||||
struct Fib {
|
||||
prev: usize,
|
||||
curr: usize,
|
||||
}
|
||||
|
||||
impl Fib {
|
||||
fn new() -> Self {
|
||||
Fib {prev: 0, curr: 1}
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for Fib {
|
||||
type Item = usize;
|
||||
fn next(&mut self) -> Option<Self::Item>{
|
||||
mem::swap(&mut self.curr, &mut self.prev);
|
||||
self.curr.checked_add(self.prev).map(|n| {
|
||||
self.curr = n;
|
||||
n
|
||||
})
|
||||
}
|
||||
}
|
||||
#![feature(conservative_impl_trait)]
|
||||
|
||||
fn main() {
|
||||
for num in Fib::new() {
|
||||
for num in fibonacci_gen(10) {
|
||||
println!("{}", num);
|
||||
}
|
||||
}
|
||||
|
||||
fn fibonacci_gen(terms: i32) -> impl Iterator<Item=u64> {
|
||||
let sqrt_5 = 5.0f64.sqrt();
|
||||
let p = (1.0 + sqrt_5) / 2.0;
|
||||
let q = 1.0/p;
|
||||
(1..terms).map(move |n| ((p.powi(n) + q.powi(n)) / sqrt_5 + 0.5) as u64)
|
||||
}
|
||||
|
|
|
|||
29
Task/Fibonacci-sequence/Rust/fibonacci-sequence-5.rust
Normal file
29
Task/Fibonacci-sequence/Rust/fibonacci-sequence-5.rust
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
use std::mem;
|
||||
|
||||
struct Fib {
|
||||
prev: usize,
|
||||
curr: usize,
|
||||
}
|
||||
|
||||
impl Fib {
|
||||
fn new() -> Self {
|
||||
Fib {prev: 0, curr: 1}
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for Fib {
|
||||
type Item = usize;
|
||||
fn next(&mut self) -> Option<Self::Item>{
|
||||
mem::swap(&mut self.curr, &mut self.prev);
|
||||
self.curr.checked_add(self.prev).map(|n| {
|
||||
self.curr = n;
|
||||
n
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
for num in Fib::new() {
|
||||
println!("{}", num);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue