2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,30 +1,12 @@
|
|||
#![feature(zero_one)]
|
||||
use std::num::One;
|
||||
use std::ops::Add;
|
||||
|
||||
struct Fib<T> {
|
||||
curr: T,
|
||||
next: T,
|
||||
}
|
||||
|
||||
impl<T> Fib<T> where T: One {
|
||||
fn new() -> Self {
|
||||
Fib {curr: T::one(), next: T::one()}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Iterator for Fib<T> where T: Add<T, Output=T> + Copy {
|
||||
type Item = T;
|
||||
fn next(&mut self) -> Option<Self::Item>{
|
||||
let new = self.curr + self.next;
|
||||
self.curr = self.next;
|
||||
self.next = new;
|
||||
Some(self.curr)
|
||||
}
|
||||
}
|
||||
|
||||
use std::mem;
|
||||
fn main() {
|
||||
for i in Fib::<u64>::new() {
|
||||
println!("{}", i);
|
||||
let mut prev = 0;
|
||||
// Rust needs this type hint for the checked_add method
|
||||
let mut curr = 1usize;
|
||||
|
||||
while let Some(n) = curr.checked_add(prev) {
|
||||
prev = curr;
|
||||
curr = n;
|
||||
println!("{}", n);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +1,12 @@
|
|||
use std::mem;
|
||||
fn main() {
|
||||
fn fib(n: i32) -> i32 {
|
||||
fn _fib(n: i32, a: i32, b: i32) -> i32 {
|
||||
match (n, a, b) {
|
||||
(0, _, _) => a,
|
||||
_ => _fib(n-1, a+b, a)
|
||||
}
|
||||
}
|
||||
fibonacci(0,1);
|
||||
}
|
||||
|
||||
_fib(n, 0, 1)
|
||||
}
|
||||
|
||||
for n in 0..20 {
|
||||
println!("{}", fib(n));
|
||||
fn fibonacci(mut prev: usize, mut curr: usize) {
|
||||
mem::swap(&mut prev, &mut curr);
|
||||
if let Some(n) = curr.checked_add(prev) {
|
||||
println!("{}", n);
|
||||
fibonacci(prev, n);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
14
Task/Fibonacci-sequence/Rust/fibonacci-sequence-3.rust
Normal file
14
Task/Fibonacci-sequence/Rust/fibonacci-sequence-3.rust
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#![feature(conservative_impl_trait)]
|
||||
|
||||
fn main() {
|
||||
for num in fibonacci_gen(10) {
|
||||
println!("{}", num);
|
||||
}
|
||||
}
|
||||
|
||||
fn fibonacci_gen(terms: i32) -> impl Iterator<Item=f64> {
|
||||
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).floor())
|
||||
}
|
||||
29
Task/Fibonacci-sequence/Rust/fibonacci-sequence-4.rust
Normal file
29
Task/Fibonacci-sequence/Rust/fibonacci-sequence-4.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