MIPS Programming instruction count issue - mips

I wrote this mips code to find the gcf but I am confused on getting the number of instructions executed for this code. I need to find a linear function as a function of number of times the remainder must be calculated before an answer. i tried running this code using Single step with Qtspim but not sure on how to proceed.
gcf:
addiu $sp,$sp,-4 # adjust the stack for an item
sw $ra,0($sp) # save return address
rem $t4,$a0,$a1 # r = a % b
beq $t4,$zero,L1 # if(r==0) go to L1
add $a0,$zero,$a1 # a = b
add $a1,$zero,$t4 # b = r
jr gcf
L1:
add $v0,$zero,$a1 # return b
addiu $sp,$sp,4 # pop 2 items
jr $ra # return to caller

There is absolutely nothing new to show here, the algorithm you just implemented is the Euclidean algorithm and it is well known in the literature1.
I will nonetheless write an informal analysis here as link only questions are evil.
First lets rewrite the code in an high level formulation:
unsigned int gcd(unsigned int a, unsigned int b)
{
if (a % b == 0)
return b;
return gcd(b, a % b);
}
The choice of unsigned int vs int was dicated by the MIPS ISA that makes rem undefined for negative operands.
Out goal is to find a function T(a, b) that gives the number of step the algorithm requires to compute the GDC of a and b.
Since a direct approach leads to nothing, we try by inverting the problem.
What pairs (a, b) makes T(a, b) = 1, in other words what pairs make gcd(a, b) terminates in one step?
We clearly must have that a % b = 0, which means that a must be a multiple of b.
There are actually an (countable) infinite number of pairs, we can limit our selves to pairs with the smallest, a and b2.
To recap, to have T(a, b) = 1 we need a = nb and we pick the pair (a, b) = (1, 1).
Now, given a pair (c, d) that requires N steps, how do we find a new pair (a, b) such that T(a, b) = T(c, d) + 1?
Since gcd(a, b) must take one step further then gcd(c, d) and since starting from gcd(a, b) the next step is gcd(b, a % b) we must have:
c = b => b = c
d = a % b => d = a % c => a = c + d
The step d = a % c => a = c + d comes from the minimality of a, we need the smallest a that when divided by c gives d, so we can take a = c + d since (c + d) % c = c % c d % c = 0 + d = d.
For d % c = d to be true we need that d < c.
Our base pair was (1, 1) which doesn't satisfy this hypothesis, luckily we can take (2, 1) as the base pair (convince your self that T(2, 1) = 1).
Then we have:
gcd(3, 2) = gcd(2, 1) = 1
T(3, 2) = 1 + T(2, 1) = 1 + 1 = 2
gcd(5, 3) = gcd(3, 2) = 1
T(5, 3) = 1 + T(3, 2) = 1 + 2 = 3
gcd(8, 5) = gcd(5, 3) = 1
T(8, 5) = 1 + T(5, 3) = 1 + 3 = 4
...
If we look at the pair (2, 1), (3, 2), (5, 3), (8, 5), ... we see that the n-th pair (starting from 1) is made by the number (Fn+1, Fn).
Where Fn is the n-th Fibonacci number.
We than have:
T(Fn+1, Fn) = n
Regarding Fibonacci number we know that Fn ∝ φn.
We are now going to use all the trickery of asymptotic analysis, particularly in the limit of the big-O notation considering φn or φn + 1 is the same.
Also we won't use the big-O symbol explicitly, we rather assume that each equality is true in the limit. This is an abuse, but makes the analysis more compact.
We can assume without loss of generality that N is an upper bound for both number in the pair and that it is proportional to φn.
We have N ∝ φn that gives logφ N = n, this ca be rewritten as log(N)/log(φ) = n (where logs are in base 10 and log(φ) can be taken to be 1/5).
Thus we finally have 5logN = n or written in reverse order
n = 5 logN
Where n is the number of step taken by gcd(a, b) where 0 < b < a < N.
We can further show that if a = ng and b = mg with n, m coprimes, than T(a, b) = T(n, m) thus the restriction of taking the minimal pairs is not bounding.
1 In the eventuality that you rediscovered such algorithm, I strongly advice against continue with reading this answer. You surely have a sharp mind that would benefit the most from a challenge than from an answer.
2 We'll later see that this won't give rise to a loss of generality.

Related

How does Unison compute the hashes of recursive functions?

In Unison, functions are identified by the hashes of their ASTs instead of by their names.
Their documentation and their FAQs have given some explanations of the mechanism.
However, the example presented in the link is not clear to me how the hashing actually works:
They used an example
f x = g (x - 1)
g x = f (x / 2)
which in the first step of their hashing is converted to the following:
$0 =
f x = $0 (x - 1)
g x = $0 (x / 2)
Doesn't this lose information about the definitions.
For the two following recursively-defined functions, how can the hashing distinguish them:
# definition 1
f x = g (x / 2)
g x = h (x + 1)
h x = f (x * 2 - 7)
# definition 2
f x = h (x / 2)
g x = f (x + 1)
h x = g (x * 2 - 7)
In my understanding, brutally converting all calling of f g and h to $0 would make the two definitions undistinguishable from each other. What am I missing?
The answer is that the form in the example (with $0) is not quite accurate. But in short, there's a special kind of hash (a "cycle hash") which is has the form #h.n where h is the hash of all the mutually recursive definitions taken together, and n is a number from 0 to the number of terms in the cycle. Each definition in the cycle gets the same hash, plus an index.
The long answer:
Upon seeing cyclical definitions, Unison captures them in a binding form called Cycle. It's a bit like a lambda, but introduces one bound variable for each definition in the cycle. References within the cycle are then replaced with those variables. So:
f x = g (x - 1)
g x = f (x / 2)
Internally becomes more like (this is not valid Unison syntax):
$0 = Cycle f g ->
letrec
[ x -> g (x - 1)
, x -> f (x / 2) ]
It then hashes each of the lambdas inside the letrec and sorts them by that hash to get a canonical order. Then the whole cycle is hashed. Then these "cycle hashes" of the form #h.n get introduced at the top level for each lambda (where h is the hash of the whole cycle and n is the canonical index of each term), and the bound variables get replaced with the cycle hashes:
#h.0 = x -> #h.1 (x - 1)
#h.1 = x -> #h.0 (x / 2)
f = #h.0
g = #h.1

Trying to find index of minimum value in a list of vars in Octave

I have a list of vars with different values
a = 2
b = 1
c= 12343243
d = 8998
Can find the smallest value
aSmallestVALUE = min([a, b, c, d])
and index
[v,idx]=min([a, b, c, d])
I want to find the index of variable and sort this list from 0 to up
something like the
sorted list = b, a, d, c
Obviously if you want to treat those four variables as a 'list' to be sorted, you need to be working with a 'list' construct, not 4 isolated variables.
L = [2, 1, 12343243, 8998];
Otherwise it makes no sense to talk about the 'index' of an existing independent variable (though obviously you can construct this L from a bunch of pre-existing variables if desired).
With L in hand, you can now do
[minval, idx] = min( L )
% minval = 1
% idx = 2
to find the minimum and its corresponding index, and
[sorted, sortedindices] = sort( L )
% sorted =
% 1.0000e+00 2.0000e+00 8.9980e+03 1.2343e+07
%
% sortedindices =
% 2 1 4 3
to obtain a sorted array, with corresponding indices.

Finding generators of a finite field

How to find generators of a finite field Fp[x]/f(x) with f(x) is a irreducible polynomial over Fp.
Input: p (prime number), n (positive number), f (irreducible polynomial)
Output: g (generator)
I have p = 2, n =3, f = x^3 + x + 1
I am a newbie so I don't know where to start.
Do you have any solution? Plese help me step by step
To find a generator (primitive element) α(x) of a field GF(p^n), start with α(x) = x + 0, then try higher values until a primitive element α(x) is found.
For smaller fields, a brute force test to verify that powers of α(x) will generate every non-zero number of a field can be done.
cnt = 0
m = 1
do
cnt = cnt + 1
m = (m*α)%f(x)
while (m != 1)
if cnt == (p^n-1) then α(x) is a generator for GF(p^n).
For a faster approach with larger fields, find all prime factors of p^n-1. Let q = any of those prime factors. If α(x) is a generator for GF(p^n), then while operating in GF(p^n):
α(x)^(p^n-1) % f(x) == 1
α(x)^((p^n-1)/q) % f(x) != 1, for all q that are prime factors of p^n-1
In this case GF(2^3) is a 3 bit field and since 2^3-1 = 7, which is prime, then it's just two tests, shown in hex: x^3 + x + 1 = b (hex)
α(x)^7 % b == 1
α(x)^1 % b != 1
α(x) can be any of {2,3,4,5,6,7} = {x,x+1,x^2,...,x^2+x+1}
As another example, consider GF(2^4), f(x) = x^4 + x^3 + x^2 + x + 1 (hex 1f). The prime factors of 2^4-1 = 15 are 3 and 5, and 15/3 = 5 and 15/5 = 3. So the three tests are:
α(x)^f % 1f == 1
α(x)^5 % 1f != 1
α(x)^3 % 1f != 1
α(x) can be any of {3,5,6,7,9,a,b,e}
For larger fields, finding all prime factors of p^n-1 requires special algorithms and big number math. Wolfram alpha can handle up to around 2^128-1:
https://www.wolframalpha.com/input/?i=factor%282%5E64-1%29
This web page can factor large numbers and includes an explanation and source code:
https://www.alpertron.com.ar/ECM.HTM
To test for α(x)^(large number) = 1 or != 1, use exponentiation by repeated squaring while performing the math in GF(p^n).
https://en.wikipedia.org/wiki/Exponentiation_by_squaring
For large fields, where p^n is greater than 2^32 (4 billion), a primitive polynomial where α(x) = x is searched for, using the test mentioned above.

Writing Fibonacci Sequence Elegantly Python

I am trying to improve my programming skills by writing functions in multiple ways, this teaches me new ways of writing code but also understanding other people's style of writing code. Below is a function that calculates the sum of all even numbers in a fibonacci sequence up to the max value. Do you have any recommendations on writing this algorithm differently, maybe more compactly or more pythonic?
def calcFibonacciSumOfEvenOnly():
MAX_VALUE = 4000000
sumOfEven = 0
prev = 1
curr = 2
while curr <= MAX_VALUE:
if curr % 2 == 0:
sumOfEven += curr
temp = curr
curr += prev
prev = temp
return sumOfEven
I do not want to write this function recursively since I know it takes up a lot of memory even though it is quite simple to write.
You can use a generator to produce even numbers of a fibonacci sequence up to the given max value, and then obtain the sum of the generated numbers:
def even_fibs_up_to(m):
a, b = 0, 1
while a <= m:
if a % 2 == 0:
yield a
a, b = b, a + b
So that:
print(sum(even_fibs_up_to(50)))
would output: 44 (0 + 2 + 8 + 34 = 44)

Generating random number with different digits

So I need to write a program which generates random numbers from 100 to 999, and the tricky part is that the digits in the number can't be the same.
For example: 222, 212 and so on are not allowed.
So far, I have this:
import random
a = int (random.randint(1,9)) <-- first digit
b = int (random.randint(0,9)) <-- second digit
c = int (random.randint(0,9)) <-- third digit
if (a != b and a != b and b != c):
print a,b,c
As you can see, I generate all three digits separately. I think it's easier to check if there are same digits in the number.
So, now I want to do a loop that will generate the numbers until the requirements are met (now it either prints blank page or the number). Note that it generates only once and I have to open the program again.
In C++ I did that with the 'while loop'. And here I don't know how to do it.
And one more question. How can I code the number(quantity) of random numbers I want to generate?
To be more specific:
I want to generate 4 random numbers. How should I code it?
P.S.
Thank you all for your answers and suggestions, I am really keen on learning new techniques and codes.
It might be easier to use random.sample to sample from the distribution [0,9] without replacement, rejecting any samples which select 0 first:
import random
def pick_number():
a = 0
while a == 0:
a, b, c = random.sample(range(10), 3)
return 100*a + 10*b + c
Further explanation: range(10) (in Python 2) generates the list [0,1,2,3,4,5,6,7,8,9], and random.sample picks 3 elements from it without replacement. The while loop repeats until the first element, a is not zero, so when it exits you have the three digits of a number that meets the requirements. You can turn this into a single integer by multiplying the first by 100, the second by 10 and then adding all three.
To loop until it's ok you can also use while in Python:
from random import randint
a = randint(1, 9)
b = randint(0, 9)
c = randint(0, 9)
while not (a!=b and b!=c and c!=a):
a = randint(1, 9)
b = randint(0, 9)
c = randint(0, 9)
You can also put it in a function:
def generate_number():
a = randint(1, 9)
b = randint(0, 9)
c = randint(0, 9)
while not (a!=b and b!=c and c!=a):
a = randint(1, 9)
b = randint(0, 9)
c = randint(0, 9)
return (a, b, c)
And if you want n such numbers (they are not actually numbers since (a, b, c) is a tuple of 3 int values), you can call it n times:
for i in range(n):
print(generate_number())
If you prefer formatting the values, you can also do:
for i in range(n):
print('%d %d %d'%generate_number()) # old style formatting
print('{} {} {}'.format(*generate_number())) # new style
Finally, you can use get n from the command line:
import sys
n = sys.argv[1]
Or you can ask it directly:
n = int(input("Please enter some number: ")) # in python2.x you'd use raw_input instead of input
You'll get an exception if the value cannot be converted; you can catch the exception and loop as for the generation of numbers.
Putting it all together with the typical main construct:
from random import randint
import sys
def generate_number():
a = randint(1, 9)
b = randint(0, 9)
c = randint(0, 9)
while not (a!=b and b!=c and c!=a):
a = randint(1, 9)
b = randint(0, 9)
c = randint(0, 9)
return (a, b, c)
def main():
n = sys.argv[1]
for i in range(n):
print('{} {} {}'.format(*generate_number()))
if __name__=='__main__':
main()
Instead of generating 3 random numbers create a list of numbers 0-9 then call random.shuffle on it. You then just pull out as many digits as you need from the shuffled list and no digit will be repeated
import random
def generate_number():
numbers = range(10) # Generates a list 0-9
random.shuffle(numbers) # Shuffle the list
return (numbers[0], numbers[1], numbers[2]) #take the first 3 items
(This answer is pretty much the same as xnx's answer execpt it uses the method shuffle which can be used in version prior to 2.3 and it does not loop waiting for a non 0 first digit.)
On top of #xnx answer, some background: this is called Fisher-Yates (or F-Y-Knuth) shuffle, and it is similar to randomly picking lottery tickets, using ticket only once. O(n) complexity, more to read here http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle