Skip to content

Fix-bu-gitstudenthub-python #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
Binary file added a.exe
Binary file not shown.
8 changes: 4 additions & 4 deletions fixme.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ int fibonacci(int n) {
if (n <= 0) return 0;
if (n == 1) return 1;

int a = 0, b = 1, temp;
int a = 0, b = 1, temp=0;
for (int i = 2; i <= n; i++) {
temp = a + b;
temp = b + a;
a = b;
b = temp;
return b; //Fix me
b = temp; //fix me
}
return b;
}

int main() {
Expand Down
70 changes: 41 additions & 29 deletions fixme.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
#include <iostream>
using namespace std;

// Function to generate Fibonacci series up to n terms
void fibonacci(int n) {
int first = 0, second = 1, next;

cout << "Fibonacci Series: ";
for (int i = 0; i < n; i++) {
// FIXME: Print the first term correctly
cout << "?" << " ";

// FIXME: Compute the next term correctly
next = ?;
first = second;
second = next;
}
cout << endl;
}

int main() {
int n;
cout << "Enter the number of terms: ";
cin >> n;

// FIXME: Call the function with correct argument
fibonacci(?);

return 0;
}
using namespace std;

// Function to generate Fibonacci series up to n terms
void fibonacci(int n) {
int first = 0, second = 1, next;

cout << "Fibonacci Series: ";
for (int i = 0; i < n; i++) {
// Print the current term
if (i == 0) {
cout << first << " ";
continue;
}
if (i == 1) {
cout << second << " ";
continue;
}

// Compute the next term
next = first + second;
cout << next << " ";
first = second;
second = next;
}
cout << endl;
}

int main() {
int n;
cout << "Enter the number of terms: ";
cin >> n;

// Call the function with the correct argument
if (n <= 0) {
cout << "Please enter a positive integer." << endl;
} else {
fibonacci(n);
}

return 0;
}
Binary file added fixme.exe
Binary file not shown.
69 changes: 42 additions & 27 deletions fixme.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,62 @@
public class FibonacciChallenge {

public static void main(String[] args) {
// Fix me: Should we allow negative numbers as input?
System.out.println("Fibonacci(5) = " + fibonacci(5));
System.out.println("Fibonacci(10) = " + fibonacci(10));

// Fix me: What happens with this test case?
System.out.println("First 8 Fibonacci numbers:");
printFibonacciSequence(8);

System.out.println("\nUsing efficient algorithm:");
System.out.println("Fibonacci(40) = " + efficientFibonacci(40));
// Ensure negative numbers are not processed for Fibonacci sequence
try {
System.out.println("Fibonacci(5) = " + fibonacci(5));
System.out.println("Fibonacci(10) = " + fibonacci(10));

System.out.println("First 8 Fibonacci numbers:");
printFibonacciSequence(8);

System.out.println("\nUsing efficient algorithm:");
System.out.println("Fibonacci(40) = " + efficientFibonacci(40));
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}

public static int fibonacci(int n) {
// Fix me: Remember, Fibonacci sequence starts with 0, 1, ...

return fibonacci(n - 1) + fibonacci(n - 2);
// Base cases for Fibonacci sequence
if (n < 0) {
throw new IllegalArgumentException("Fibonacci number cannot be negative.");
}
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}

return fibonacci(n - 1) + fibonacci(n - 2); // Recursion for Fibonacci
}

public static void printFibonacciSequence(int n) {
// Fix me: Off-by-one error?
for (int i = 1; i <= n; i++) {
// Loop from 0 to n-1 for the correct sequence
for (int i = 0; i < n; i++) {
System.out.print(fibonacci(i) + " ");
}
System.out.println();
}

public static int efficientFibonacci(int n) {
// Fix me: How can you avoid recalculating the same Fibonacci numbers repeatedly?
int a = 0;
int b = 1;

// Iterative method to avoid recalculating the same values
if (n < 0) {
throw new IllegalArgumentException("Fibonacci number cannot be negative.");
}
if (n == 0) {
return a;
return 0;
}
if (n == 1) {
return 1;
}

for (int i = 2; i < n; i++) {

int a = 0, b = 1;
for (int i = 2; i <= n; i++) {
int temp = a;
a = b;
b = temp + a;
}

return b;
}
}
4 changes: 2 additions & 2 deletions fixme.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
def fibonacci_iterative(n):
if n <= 0:
return 0
elif n == 2: #Fix me
elif n == 1: #Fix me
return 1

a, b = 0, 1
for _ in range(2, n+1):
a, b = b, a + b
return b # Fix me.
return b

n = 10
print(f"The {n}th Fibonacci number is: {fibonacci_iterative(n)}")