mirror of
https://github.com/supleed2/ELEC40006-P1-CW.git
synced 2024-11-10 02:05:48 +00:00
Added infrastructure for Fibonacci's number
This commit is contained in:
parent
3e4e5569b1
commit
d6bb9bd762
23
testing/fibonacci.cpp
Normal file
23
testing/fibonacci.cpp
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
Takes one input via stdin, the integer n
|
||||
*/
|
||||
|
||||
int fib(const int n){
|
||||
int y;
|
||||
if (n <= 1) {
|
||||
y = 1;
|
||||
}else {
|
||||
y = fib(n-1);
|
||||
y = y + fib(n-2);
|
||||
}
|
||||
return y;
|
||||
}
|
||||
|
||||
int main(){
|
||||
int n;
|
||||
cin >> n;
|
||||
cout << fib(n) << endl;
|
||||
}
|
44
testing/fibonacci.txt
Normal file
44
testing/fibonacci.txt
Normal file
|
@ -0,0 +1,44 @@
|
|||
Comments:
|
||||
R1 holds y, as described in the source code
|
||||
R2 holds a, as described in the source code
|
||||
R3 holds b, as described in the source code
|
||||
R4 holds n, as described in the source code
|
||||
R5 holds the output (sum), as described in the source code
|
||||
R6 holds the number 0xB (for jumping to the end of the program)
|
||||
R7 holds the number 0x6 (for jumping back)
|
||||
|
||||
(0x0) LDA R1 ##MEMORY_LOCATION_OF_s## (loads the seed, s, into R1, which then becomes y as in the source code)
|
||||
(0x1) LDA R2 ##MEMORY_LOCATION_OF_a## (loads a into R2)
|
||||
(0x2) LDA R3 ##MEMORY_LOCATION_OF_b## (loads b into R3)
|
||||
(0x3) LDA R4 ##MEMORY_LOCATION_OF_n## (loads n into R4)
|
||||
(0x4) LDA R6 ##MEMORY_LOCATION_CONTAINING_0xA## (loads 0xB, memory location of program end to R6)
|
||||
(0x5) LDA R7 ##MEMORY_LOCATION_CONTAINING_0x6## (loads 0x6, memory location for jumping back)
|
||||
(0x6) JC4 R6 R4 (if R4, in this case, n, is equal to 0, then jump to end, STP)
|
||||
(0x7) MLA R1 R2 R3 (multiply and add, same as in the source code y=y*a+b)
|
||||
(0x8) ADD R5 R5 R1 (adds y to the sum, so that sum+=y, as in the source code)
|
||||
(0x9) SBO R4 R4 (decrease R4, i.e. n by one, so no infinite loops occur)
|
||||
(0xA) JMP R7 (jumps back to check whether n>0 and if so, repeats again)
|
||||
(0xB) STP
|
||||
|
||||
|
||||
Example that can be used with the instruction generator program:
|
||||
LDA R1 0
|
||||
LDA R2 1
|
||||
LDA R3 2
|
||||
LDA R4 3
|
||||
LDA R6 4
|
||||
LDA R7 5
|
||||
JC4 R6 R4
|
||||
MLA R1 R2 R3
|
||||
ADD R5 R5 R1
|
||||
SBO R4 R4
|
||||
JMP R7 R6
|
||||
STP
|
||||
|
||||
Requires setting the following data memory location:
|
||||
Set location '0' to value of s;
|
||||
Set location '1' to value of a;
|
||||
Set location '2' to value of b;
|
||||
Set location '3' to value of n;
|
||||
Set location '4' to value of 0xB;
|
||||
Set location '5' to value of 0x6;
|
51
testing/fibonacci_alternative.cpp
Normal file
51
testing/fibonacci_alternative.cpp
Normal file
|
@ -0,0 +1,51 @@
|
|||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int fib(int n){
|
||||
int temp = n; //temporary variable, set equal to n
|
||||
stack <int> s;
|
||||
//if n is non-negative, decrease until reaches zero to fill up stack
|
||||
while(temp>1){
|
||||
s.push(0); //n is not 0 or 1, hence push 0 onto stack, come back later
|
||||
temp--; //prevents an infinite loop
|
||||
}
|
||||
s.push(1);
|
||||
s.push(1);
|
||||
|
||||
//go back, increase temp until equal to n
|
||||
while(temp<n){
|
||||
/*
|
||||
Retrieve the top two elements
|
||||
*/
|
||||
int smaller = s.top();
|
||||
s.pop();
|
||||
int larger = s.top();
|
||||
s.pop();
|
||||
|
||||
/*
|
||||
this removes the current element, which is 0, but add it back later, once calculated
|
||||
*/
|
||||
s.pop();
|
||||
/*
|
||||
Calculate the next element and add it and then to the top of the stack add the larger of thee previous two values
|
||||
*/
|
||||
int current = smaller+larger;
|
||||
s.push(current);
|
||||
s.push(larger);
|
||||
temp++; //prevents an infinite loop
|
||||
}
|
||||
/*
|
||||
the top element is 'bigger', which would be used to calculate next Fibonacci number but this is no longer required,
|
||||
so remove and return top element;
|
||||
*/
|
||||
s.pop();
|
||||
return s.top();
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
int n;
|
||||
cin >> n;
|
||||
cout << fib(n) << endl;
|
||||
}
|
Loading…
Reference in a new issue