Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Contributors.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Contributors

Add your name at the end of the list after Successfull contribution.

fharookshaik
cozyDoomer
AasthaGoyalgit
Expand All @@ -14,3 +14,4 @@ shravan20
Avatar
bettman-latin
MrJithi
shahafashash
30 changes: 30 additions & 0 deletions fibonacci_series/C++/cpp_fibonacci_binet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>
#include <cmath>

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<unsigned long long>(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<unsigned int>(input);
cout << "Fibonacci(" << n << ") = " << fibonacciBinet(n) << endl;
return 0;
}
31 changes: 31 additions & 0 deletions fibonacci_series/C/C_fibonacci_binet.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <stdio.h>
#include <math.h>

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;
}
31 changes: 31 additions & 0 deletions fibonacci_series/Go/Go_fibonacci_binet.go
Original file line number Diff line number Diff line change
@@ -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))
}
24 changes: 24 additions & 0 deletions fibonacci_series/Lua/lua_fibonacci_binet.lua
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions fibonacci_series/Python/python_fibonacci_binet.py
Original file line number Diff line number Diff line change
@@ -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)
31 changes: 31 additions & 0 deletions fibonacci_series/Rust/rust_fibonacci_binet.rs
Original file line number Diff line number Diff line change
@@ -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));
}