UP | HOME

Exercise 9 solutions

Table of Contents


Python

e09.py

def fib(n):
    if (n == 1):
        return 0
    elif (n == 2):
        return 1
    else:
        fprev2 = 1
        fprev1 = 1
        fn = 1
        i = 3
        while (i < n):
            fn = fprev1 + fprev2
            fprev2 = fprev1
            fprev1 = fn
            i = i + 1
        return fn

n = int(raw_input("Which Fibonacci number do you want? "))
print "Fibonacci number " + str(n) + " is equal to " + str(fib(n))

MATLAB / Octave

fib.m

function fn = fib(n)
  if (n == 1)
    fn = 0;
  elseif (n == 2)
    fn = 1;
  else
    fprev2 = 1;
    fprev1 = 1;
    fn = 1;
    i = 3;
    while (i < n)
      fn = fprev1 + fprev2;
      fprev2 = fprev1;
      fprev1 = fn;
      i = i + 1;
    end
  end
end

e09.m

n = input('Which Fibonacci number do you want? ');
disp(['Fibonacci number ',num2str(n),' is equal to ',num2str(fib(n))]);

R

e09.R

fib <- function(n){
    if (n == 1) {
	return(0)
    }
    else if (n == 2) {
	return(1)
    }
    else {
	fprev2 <- 1
	fprev1 <- 1
	fn <- 1
	i <- 3
	while (i < n) {
	    fn <- fprev1 + fprev2
	    fprev2 <- fprev1
	    fprev1 <- fn
	    i <- i + 1
	}
	return(fn)
    }
}

n <- as.integer(readline("Which Fibonacci number do you want? "))
cat("Fibonacci number",n,"is equal to",fib(n),"\n")

C

e09.c

// compile with: gcc -o e09 e09.c

#include <stdio.h>

int fib(int n) {
  if (n==1) {
    return 0;
  }
  else if (n==2) {
    return 1;
  }
  else {
    int fprev2 = 1;
    int fprev1 = 1;
    int fn = 1;
    int i = 3;
    while (i < n) {
      fn = fprev1 + fprev2;
      fprev2 = fprev1;
      fprev1 = fn;
      i = i + 1;
    }
    return fn;
  }
}

int main(int argc, char *argv[]) {

  int n;
  printf("Which Fibonacci number do you want? ");
  scanf("%d", &n);
  printf("Fibonacci number %d is equal to %d\n", n, fib(n));

  return 0;
}

Bonus Question 1

Python

e09b1.py

def fibR(n):
    if (n == 1):
        return 0
    elif (n == 2):
        return 1
    else:
        return fibR(n-2) + fibR(n-1)

n = int(raw_input("Which Fibonacci number do you want? "))
print "Fibonacci number " + str(n) + " is equal to " + str(fibR(n))

MATLAB / Octave

fibR.m

function fn = fibR(n)
  if (n == 1)
    fn = 0;
  elseif (n == 2)
    fn = 1;
  else
    fn = fibR(n-2) + fibR(n-1);
  end
end

e09b1.m

n = input('Which Fibonacci number do you want? ');
disp(['Fibonacci number ',num2str(n),' is equal to ',num2str(fibR(n))]);

R

e09b1.R

fibR <- function(n){
    if (n == 1) {
	return(0)
    }
    else if (n == 2) {
	return(1)
    }
    else {
	return(fibR(n-2) + fibR(n-1))
    }
}

n <- as.integer(readline("Which Fibonacci number do you want? "))
cat("Fibonacci number",n,"is equal to",fibR(n),"\n")

C

e09b1.c

// compile with: gcc -o e09b1 e09b1.c

#include <stdio.h>

int fibR(int n) {
  if (n==1) {
    return 0;
  }
  else if (n==2) {
    return 1;
  }
  else {
    return fibR(n-2) + fibR(n-1);
  }
}

int main(int argc, char *argv[]) {

  int n;
  printf("Which Fibonacci number do you want? ");
  scanf("%d", &n);
  printf("Fibonacci number %d is equal to %d\n", n, fibR(n));

  return 0;
}

Paul Gribble | fall 2014
This work is licensed under a Creative Commons Attribution 4.0 International License
Creative Commons License