mirror of
https://github.com/ringtailsoftware/uvm32.git
synced 2026-06-05 22:43:39 +00:00
Fibonacci sample
This commit is contained in:
parent
f235265e3f
commit
30cf60561f
2 changed files with 75 additions and 0 deletions
48
apps/fib/fib.c
Normal file
48
apps/fib/fib.c
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
#define USE_MAIN
|
||||
#include "uvm32_target.h"
|
||||
|
||||
// print n terms of fibonacci series
|
||||
void printFib(int n) {
|
||||
int prev1 = 1;
|
||||
int prev2 = 0;
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
if (i > 2) {
|
||||
int curr = prev1 + prev2;
|
||||
prev2 = prev1;
|
||||
prev1 = curr;
|
||||
printd(curr);
|
||||
} else if (i == 1) {
|
||||
printd(prev2);
|
||||
} else if (i == 2) {
|
||||
printd(prev1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// print n terms of fibonacci series, recursively
|
||||
void fib_recursive(int n, int prev1, int prev2) {
|
||||
if (n < 3) {
|
||||
return;
|
||||
}
|
||||
|
||||
int curr = prev1 + prev2;
|
||||
printd(curr);
|
||||
|
||||
return fib_recursive(n - 1, prev2, curr);
|
||||
}
|
||||
|
||||
void printFibRec(int n) {
|
||||
printd(0);
|
||||
printd(1);
|
||||
fib_recursive(n, 0, 1);
|
||||
}
|
||||
|
||||
void main(void) {
|
||||
int n = 40;
|
||||
println("fib() loop");
|
||||
printFib(n);
|
||||
println("fib() recursive");
|
||||
printFibRec(n);
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue