File tree Expand file tree Collapse file tree 5 files changed +48
-0
lines changed Expand file tree Collapse file tree 5 files changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include <stdio.h>
2
+ #include <stdlib.h>
3
+
4
+ int main () {
5
+ int n = 10 ; // number of fibonacci terms
6
+ int f0 = 0 ; // fibonacci number (1st term)
7
+ int f1 = 1 ; // fibonacci number (2nd term)
8
+ while (n > 0 ) {
9
+ printf ("%d\n" , f0 ); // print the current term
10
+ int nth = f0 + f1 ; // the nth term
11
+ f0 = f1 ; // update first
12
+ f1 = nth ; // update second
13
+ n -- ; // decrement number of terms left
14
+ }
15
+ return 0 ;
16
+ }
Original file line number Diff line number Diff line change
1
+ n = 10 # number of fibonacci terms
2
+ f0 = 0 # fibonacci number (1st term)
3
+ f1 = 1 # fibonacci number (2nd term)
4
+ while n > 0 :
5
+ print (f0 ) # print the current term
6
+ nth = f0 + f1 # the nth term
7
+ f0 = f1 # update first
8
+ f1 = nth # update second
9
+ n -= 1 ; # decrement number of terms left
Original file line number Diff line number Diff line change
1
+ fibonacci:
2
+ addi a0 , zero, 10 # number of terms we want
3
+ addi a1 , zero, 0 # fibonacci number (1st term)
4
+ addi a2 , zero, 1 # fibonacci number (2nd term)
5
+ # now, a1 holds fibonacci_0 and
6
+ # a2 holds fibonacci_1
7
+ # Now calculate the rest of the terms, which will
8
+ # go into a1 and a2 as the algorithm continues
9
+ # After 10 iterations, a1 should hold 55 and a2 should hold 89
10
+ fib_loop:
11
+ add a3 , a1 , a2 # nth term
12
+ addi a1 , a2 , 0 # update first
13
+ addi a2 , a3 , 0 # update second
14
+ addi a0 , a0 , -1 # decrement number of terms left
15
+ bgt a0 , zero, fib_loop # branch if not finished
16
+
Original file line number Diff line number Diff line change
1
+ li a0 , 67 # 0x43 or 0b01000111
2
+ li a1 , 0
3
+ loop:
4
+ andi a2 , a0 , 1
5
+ add a1 , a1 , a2
6
+ srli a0 , a0 , 1
7
+ bne a0 , x0, loop
You can’t perform that action at this time.
0 commit comments