Question

In: Computer Science

Sample Execution : ** Recursive Euclidean Algorithm to calculate GCD ** Type a positive integer for...

Sample Execution : ** Recursive Euclidean Algorithm to calculate GCD **

Type a positive integer for A: -2

Sorry, you must enter a positive integer (>0). Please try again.

** Recursive Euclidean Algorithm to calculate GCD **

Type a positive integer for A: 35

Type a positive integer for B: 63

The GCD is 7

Write a MIPS assembly program that prompts the user for 2 positive integers (>0). Then it uses the Recursive Euclidean Algorithm to calculate GCD (the Greatest Common Divisor). Please see below for the Recursive Euclidean Algorithm in Java and C++.

Make sure program the error for negative number.

Sample Execution : ** Recursive Euclidean Algorithm to calculate GCD **

Type a positive integer for A: -2

Sorry, you must enter a positive integer (>0). Please try again.

// Java: Euclidean Algorithm to calculate GCD // import java.util.Scanner; public class FindGCD { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("The Euclidean Algorithm to calculate GCD.\n"); System.out.println("Enter a positive integer A: "); int A = input.nextInt(); System.out.println("Enter a positive integer B: "); int B = input.nextInt(); if (A < 1 || B < 1) System.out.println("\nError, please enter a positive (>0) integer."); else { if (A < B) { int temp = A; A = B; B = temp; } System.out.println("The GCD is :" + GCD(A, B)); } } public static int GCD(int A, int B) { if (B == 0) return A; else return GCD(B, A % B); } }

// C++: Euclidean Algorithm to calculate GCD // #include using namespace std; int GCD(int A, int B) { if (B == 0) return A; else return GCD(B, A%B); } int main() { int A, B; cout > A; cout > B; if (A < 1 || B < 1) cout 0) integer.\n”; else { if (A < B) { int temp = A; A = B; B = temp; } cout << "The GCD is :" << GCD(A, B) << endl; } return 0; }

Solutions

Expert Solution

.text
         .globl __start

__start: li $v0, 4
         la $a0, First
         syscall                # Ask for the first integer

         li $v0, 5
         syscall                # Read an integer
         add $t0, $0, $v0       # and store it in $t0

         li $v0, 4
         la $a0, Second
         syscall                # Ask for the second integer

         li $v0, 5
         syscall                # Read an integer
         add $t1, $0, $v0       # and store it in $t1

# Include here one of the following two versions

# Version 1: based on integer division

loop:    div $t0, $t1
         mfhi $t2               # $t2 = $t0 mod $t1
         add $t0, $0, $t1       # $t0 = $t1
         add $t1, $0, $t2       # $t1 = $t2
         bne $t1, $0, loop

# Version 2: based on subtract

#loop:    beq $t0, $t1, exit     # if the numbers are equal then exit
#         bgt $t0, $t1, skip     # subtract the smaller from the bigger
#         sub $t1, $t1, $t0      # and replace the bigger with the result
#         b loop                 # then continue
#skip:    sub $t0, $t0, $t1
#         b loop

# end of inclusion

exit:    li $v0, 4
         la $a0, Ans
         syscall                # Print "The GCD is: "

         add $a0, $0, $t0
         li $v0, 1
         syscall                # Print GCD

         li $v0, 4
         la $a0, nl
         syscall                # Print new line

         b __start              # Go to __start (Press Ctrl-C for exit)

         .data
First:   .asciiz "Calculating GCD of two integers.\nEnter first integer: "
Second: .asciiz "Enter second integer: "
Ans:     .asciiz "Their GCD is: "
nl:      .asciiz "\n\n"


Related Solutions

a. Using the Euclidean Algorithm and Extended Euclidean Algorithm, show that gcd(99; 5) = 1 and...
a. Using the Euclidean Algorithm and Extended Euclidean Algorithm, show that gcd(99; 5) = 1 and find integers s1 and t1 such that 5s1 + 99t1 = 1. [Hint: You should find that 5(20) + 99(?1) = 1] b. Solve the congruence 5x 17 (mod 99) c. Using the Chinese Remainder Theorem, solve the congruence x 3 (mod 5) x 42 (mod 99) d. Using the Chinese Remainder Theorem, solve the congruence x 3 (mod 5) x 6 (mod 9)...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
Use the Euclidean algorithm to find the GCD of 3 + 9i and 7-i
Use the Euclidean algorithm to find the GCD of 3 + 9i and 7-i
find a linear combination for gcd(259,313). use extended euclidean algorithm. what is inverse of 259 in...
find a linear combination for gcd(259,313). use extended euclidean algorithm. what is inverse of 259 in z subscript 313? what is inverse of 313 in z subscript 259?
All necessary steps much show for these problems, please. Use the Euclidean algorithm to find gcd(12345,...
All necessary steps much show for these problems, please. Use the Euclidean algorithm to find gcd(12345, 54321). Write gcd(2420, 70) as a linear combination of 2420 and 70. The work to obtain the gcd is provided. 2420 = 34(70) + 40 70 = 1(40) + 30 40 = 1(30) + 10 30 = 3(10) + 0 Determine if 1177 is prime or not. If it is not, then write 1177 as a product of primes Find gcd(8370, 465) by unique...
a. Design a recursive algorithm for computing 3n for any nonnegative integer n that is based...
a. Design a recursive algorithm for computing 3n for any nonnegative integer n that is based on the formula 3n = 3n−1 + 3n−1 + 3n−1 b. Set up a recurrence relation for the number of additions made by the algorithm and solve it. c. Draw a tree of recursive calls for this algorithm and count the number of calls made by the algorithm. d. Is it a good algorithm for solving this problem?
Develop a recursive algorithm that multiply two integer numbers x and y, where x is an...
Develop a recursive algorithm that multiply two integer numbers x and y, where x is an m-bit number and y is an n-bit number (10 points), and analyze the time complexity of this algorithm (10 points).
java Write a recursive program to reverse a positive integer. . Your method should take a...
java Write a recursive program to reverse a positive integer. . Your method should take a non negative integer as a parameter and return the reverse of the number as an integer. e.g. if you pass 12345, your method should return 54321.
Develop, write and certify a properly recursive procedure reverseDigits to input a positive integer n and...
Develop, write and certify a properly recursive procedure reverseDigits to input a positive integer n and to output the integer formed by reversing the digits of n. Thus (reverseDigits 1234) returns the integer 4321. Please answer using Dr Racket(R5RS language)
2. Give a recursive algorithm to compute the product of two positive integers, m and n,...
2. Give a recursive algorithm to compute the product of two positive integers, m and n, using only addition and subtraction. Java Language...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT