/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
fn runfn(uri_fn: String,uri_arg:String) -> String{
if uri_fn.len() >2 {
let pre="Running the function passed in the url ";
let f = uri_arg.trim().parse::<i16>().unwrap_or_default();
let result =
match uri_fn.as_str().trim() {
"fibbonaci"=>
if f<94 {
fibbonaci(uri_arg)
}else{
bigfib(uri_arg)
},
"bigfib"=>bigfib(uri_arg),
_ => nomatch(),
};
let r = format!("{pre}{uri_fn}{result}");
return r;
}else{
let pre="You must provide a function to run in the url as in ?fn=best5";
let r = format!("{pre}{uri_fn}");
println!("{r}");
return r;
}
}
fn fibbonaci(length:String)->String{
let mut pre= String::from("<br>the result of the given function is ");
let f = length.trim().parse::<i8>();
let mut length= f.unwrap_or_else(|error|{
pre=error.to_string();
0
});
length=length-2;
let result = go_get_fibbonaci(length);
if result.as_ref().unwrap().len()>91{
pre=String::from("<br>The result of the given function can only return 93 values due to type u64 overflow");
}
let post= String::from("<br><br><a href='https://onlinegdb.com/tGEtRqvOp'>Code for this function</a>");
let r = format!(" {pre} {:?} {post}",result);
return r;
}
fn bigfib(length:String)->String{
let mut pre= String::from("<br>the result of the given function is ");
let f = length.trim().parse::<i16>();
let mut length= f.unwrap_or_else(|error|{
pre=error.to_string();
0
});
length=length-1;
let result = go_get_bigfib(length);
let post= String::from("<br><br><a href='https://onlinegdb.com/tGEtRqvOp'>Code for this function</a>");
let r = format!(" {pre} {:?} {post}",result);
return r;
// This is a very large number.
// Println!("fib(1000) = {}", fib(1000));
}
//Start fibbonaci
#[derive(Debug)]
struct Fibonacci {
next_value: u64,
current: u64,
}
impl Fibonacci {
fn new() -> Self {
Self { next_value: 1, current: 0 }
}
}
impl Iterator for Fibonacci {
type Item = u64;
fn next(&mut self) -> Option<Self::Item> {
if let Some(next_value) = self.current.checked_add(self.next_value) {
self.current = self.next_value;
self.next_value = next_value;
Some(self.current)
} else {
None
}
}
}
//Result<Vec<(String, u8)>, Box<dyn Error>>
fn go_get_fibbonaci(length:i8) -> Result<Vec<u64>,Box<dyn Error>> {
let mut i: i8=0;
let mut fibvec = Vec::new();
for fb in Fibonacci::new() {
fibvec.push(fb);
if i>length {
break;
}
i=i+1;
};
Ok(fibvec)
}
use num_bigint::BigUint;
use num_traits::{Zero, One};
// Calculate large fibonacci numbers.
fn go_get_bigfib(n: i16) -> BigUint {
let mut f0: BigUint = Zero::zero();
let mut f1: BigUint = One::one();
for _ in 0..n {
let f2 = f0 + &f1;
f0 = f1;
f1 = f2;
}
f0
}