diff --git a/Contributors.md b/Contributors.md index 30191bc..c941941 100644 --- a/Contributors.md +++ b/Contributors.md @@ -1,7 +1,7 @@ # Contributors Add your name at the end of the list after Successfull contribution. - + fharookshaik cozyDoomer AasthaGoyalgit @@ -14,3 +14,4 @@ shravan20 Avatar bettman-latin MrJithi +shahafashash \ No newline at end of file diff --git a/fibonacci_series/C++/cpp_fibonacci_binet.cpp b/fibonacci_series/C++/cpp_fibonacci_binet.cpp new file mode 100644 index 0000000..3d587ca --- /dev/null +++ b/fibonacci_series/C++/cpp_fibonacci_binet.cpp @@ -0,0 +1,30 @@ +#include +#include + +using namespace std; + +unsigned long long fibonacciBinet(unsigned int n) { + if (n == 0) return 0; + if (n == 1) return 1; + + const double phi = (1 + sqrt(5)) / 2; + const double psi = (1 - sqrt(5)) / 2; + + return static_cast(round((pow(phi, n) - pow(psi, n)) / sqrt(5))); +} + + +int main() { + int input; + cout << "Enter the nth Fibonacci number to find: "; + cin >> input; + + if (input < 0) { + cout << "Please enter a non-negative integer." << endl; + return 1; + } + + unsigned int n = static_cast(input); + cout << "Fibonacci(" << n << ") = " << fibonacciBinet(n) << endl; + return 0; +} \ No newline at end of file diff --git a/fibonacci_series/C/C_fibonacci_binet.c b/fibonacci_series/C/C_fibonacci_binet.c new file mode 100644 index 0000000..8be82dc --- /dev/null +++ b/fibonacci_series/C/C_fibonacci_binet.c @@ -0,0 +1,31 @@ +#include +#include + +unsigned long long fibonacciBinet(unsigned int n) { + if (n == 0) return 0; + if (n == 1) return 1; + + const double phi = (1 + sqrt(5)) / 2; + const double psi = (1 - sqrt(5)) / 2; + + return (unsigned long long)(round((pow(phi, n) - pow(psi, n)) / sqrt(5))); +} + +int main() { + int input; + printf("Enter the nth Fibonacci number to find: "); + if (!(scanf("%d", &input) == 1)) { + printf("Invalid input. Please enter a number.\n"); + return 1; + } + + if (input < 0) { + printf("Please enter a non-negative integer.\n"); + return 1; + } + + unsigned int n = (unsigned int)input; + + printf("Fibonacci(%u) = %llu\n", n, fibonacciBinet(n)); + return 0; +} \ No newline at end of file diff --git a/fibonacci_series/Go/Go_fibonacci_binet.go b/fibonacci_series/Go/Go_fibonacci_binet.go new file mode 100644 index 0000000..50f941b --- /dev/null +++ b/fibonacci_series/Go/Go_fibonacci_binet.go @@ -0,0 +1,31 @@ +package main + +import ( + "fmt" + "math" +) + +func binetFibonacci(n int) int { + if n < 0 { + return -1 // Invalid input + } + if n <= 1 { + return n + } + + phi := (1 + math.Sqrt(5)) / 2 + psi := (1 - math.Sqrt(5)) / 2 + + return int(math.Round((math.Pow(phi, float64(n)) - math.Pow(psi, float64(n))) / math.Sqrt(5))) +} + + +func main() { + fmt.Print("Enter the number of elements: ") + + var fibNum int + fmt.Scanf("%d", &fibNum) + + fmt.Print("Fibonacci (Binet's Formula):\n") + fmt.Println(binetFibonacci(fibNum)) +} \ No newline at end of file diff --git a/fibonacci_series/Lua/lua_fibonacci_binet.lua b/fibonacci_series/Lua/lua_fibonacci_binet.lua new file mode 100644 index 0000000..dcf40fa --- /dev/null +++ b/fibonacci_series/Lua/lua_fibonacci_binet.lua @@ -0,0 +1,24 @@ +local getInput = require("input") + +-- Lua implementation of Fibonacci sequence using Binet's formula +function fibonacci(n) + if n < 0 then + return nil -- Invalid input + elseif n == 0 then + return 0 + elseif n == 1 then + return 1 + end + + local phi = (1 + math.sqrt(5)) / 2 + local psi = (1 - math.sqrt(5)) / 2 + return math.floor((phi^n - psi^n) / math.sqrt(5) + 0.5) +end + +-- Get user input +local n = getInput(true) +if (n ~= nil) then + io.write(string.format("Fibonacci(%i) = %i\n", n, fibonacci(n))) +else + io.write("Invalid input.") +end \ No newline at end of file diff --git a/fibonacci_series/Python/python_fibonacci_binet.py b/fibonacci_series/Python/python_fibonacci_binet.py new file mode 100644 index 0000000..3af9957 --- /dev/null +++ b/fibonacci_series/Python/python_fibonacci_binet.py @@ -0,0 +1,30 @@ + +def fibonacci(n: int) -> int: + """Calculate the nth Fibonacci number using Binet's formula. + + Args: + n (int): The term in the Fibonacci sequence to calculate. + + Returns: + int: The nth Fibonacci number. + + Raises: + ValueError: If n is a negative integer. + """ + if n < 0: + raise ValueError("Wrong Input! n must be a non-negative integer.") + elif n == 0: + return 0 + elif n == 1: + return 1 + + phi = (1 + 5 ** 0.5) / 2 + psi = (1 - 5 ** 0.5) / 2 + return int((phi ** n - psi ** n) / (2 * phi - 1)) + +if __name__ == "__main__": + num = int(input("Enter the nth term: ")) + try: + print(fibonacci(num)) + except ValueError as e: + print(e) \ No newline at end of file diff --git a/fibonacci_series/Rust/rust_fibonacci_binet.rs b/fibonacci_series/Rust/rust_fibonacci_binet.rs new file mode 100644 index 0000000..7cb3e4d --- /dev/null +++ b/fibonacci_series/Rust/rust_fibonacci_binet.rs @@ -0,0 +1,31 @@ +use std::io; + +fn fib_binet(n: u32) -> u128 { + if n == 0 { + return 0; + } else if n == 1 { + return 1; + } + + if n > 78 { + panic!("Warning: Results inaccurate for n > 78 due to f64 precision limits"); + } + + let phi = (1.0 + 5.0f64.sqrt()) / 2.0; + let psi = (1.0 - 5.0f64.sqrt()) / 2.0; + + let fib_n = (phi.powf(n as f64) - psi.powf(n as f64)) / (5.0f64.sqrt()); + + fib_n.round() as u128 +} + + +fn main() { + println!("Enter the nth term: "); + let mut input = String::new(); + io::stdin() + .read_line(&mut input) + .expect("Failed to read input"); + let n: u32 = input.trim().parse().expect("Please enter a valid number"); + println!("{}", fib_binet(n)); +} \ No newline at end of file