Rust:codewars product of consecutive Fib Numbers_rust

Source: Internet
Author: User

The Fibonacci NUMBERS:FN:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, ...
Where the relationship is:
F (n) = f (n-1) + f (n-2) with f (0) = 0 and F (1) = 1.

Given a number, try to find out whether it can be multiplied by successive contiguous the Fibonacci numbers;
If you cannot multiply it, find the upper boundary value. Write out the PRODUCTFIB function. Example:

Productfib (714) # should return (a, true),
# since f (8) =, f (9) = + and 714 = 21 * 34

Productfib (a) # should return (a, false), => upper boundary value
# since f (8) = e, f (9) =%, f (+) = + < A < 34 * 55

First, my solution

fn PRODUCT_FIB (prod:u64)-> (U64, U64, BOOL) {//Your code let Mut hp:hashmap<u64, u64> = hashmap::new
    ();
    Hp.insert (1u64, 0u64);
    Hp.insert (2u64, 1u64);
    Hp.insert (3u64, 1u64);
    Let Mut vec:vec<u64> = Vec::new ();
    Vec.push (0U64);
    Vec.push (1U64);
    Vec.push (1U64);
    Let Mut result:vec<u64> = Vec::new (); For I in 3u64.
        {Let i_1 = Hp.get (& (I-1)). Unwrap (). Clone ();
        Let i_2 = Hp.get (& (I-2)). Unwrap (). Clone ();
        Let val = i_2 + i_1;
        Hp.insert (I, Val);
        Vec.push (Val);
        Let val_i_2 = &vec[(i-2) as usize];
        Let val_i_1 = &vec[(i-1) as usize];

        Let val_i = &vec[i as usize];
        If val_i_1 * val_i = prod| |
            (Val_i_2 * Val_i_1 < PROD, val_i_1 * val_i > Prod) = = (True, true) {Result.push (*val_i);
            Result.push (*val_i_1);
        Break } (*result.get (0). Unwrap (), *result.get (1). Unwrap (), prod = = *result.get (0). Unwrap () * *result.get (1). Unwrap ())}
 

Second, the wonderful solution

1,

fn PRODUCT_FIB (prod:u64)-> (U64, U64, bool) {let
    mut a = 0; let mut b = 1;
    While A * b < prod {let
        tmp = A;
        A = b;
        b = tmp + b;
    } 
    Let BL = if a * b = = prod {true} else {false};
    (A, B, BL)
}

2,

Use Std::cmp::ordering::{greater, Equal};
fn PRODUCT_FIB (prod:u64)-> (U64, U64, bool) {let
  mut p0 = 0;
  Let mut p1 = 1;
  loop {let
    p = p0 * p1;
    Match p.cmp (&prod) {
      Greater => return (P0, p1, False),
      Equal => return (P0, p1, True),
      _ => ( )
    };
    Let P2 = P0 + p1;
    P0 = p1;
    P1 = p2;
  }

3,

fn PRODUCT_FIB (prod:u64)-> (U64, U64, bool) {let
    mut f = (0, 1);
    loop {
        F = (F.1, f.0 + f.1);
        If f.0 * F.1 >= prod {return
            (f.0, F.1, f.0 * F.1 = = prod)
        ;
    }
}

4,

 use std::cmp::ordering; fn PRODUCT_FIB (prod:u64)-> (U64, U64, bool) {let mut fib_vec:vec<u64> = vec![
    0,1];
    Let Mut V_len = Fib_vec.len ();
        loop {Let (n, n_1) = (fib_vec[v_len-1], fib_vec[v_len-2]);
                Match (n*n_1). CMP (&prod) {ordering::less => {let Next_fib = n + n_1;
                Fib_vec.push (NEXT_FIB);
            V_len = Fib_vec.len (); }, Ordering::equal => {return (n_1, n, True);}, Ordering::greater => {return (n_1, N, FAL SE); },
        }
    }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.