CS1120 Spring 2009 Recursion Worksheet: Euclidean Algorithm and GCD in C#, Study Guides, Projects, Research of Computer Science

Instructions for implementing the euclidean algorithm, which is used to compute the greatest common divisor (gcd) of two integers, using recursion in c#. It includes a historical background of the algorithm and a step-by-step guide for creating a console application.

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 07/23/2009

koofers-user-he7
koofers-user-he7 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS1120 – Spring 2009
Re cu rs io n Wo rk sh ee t 01/14/ 2009
(T hi s wo rk sh ee t solution does not need to be turned i n) .
Historical Significance:
Euclidean Algorithm is one of the oldest algorithms – at least 300 BCE in Ancient
Greece. The Euclidean Algorithm is contains one of the “most famous” recursion
functions and is used to compute the greatest common divider (GCD) of any two
integers. It is extremely popular because it does not require any factoring. (For
more information: http://en.wikipedia.org/wiki/Euclidean_algorithm).
How GCD work:
Given two integers (let’s call them x and y. Both x and y are not equal to zero). If y
is equal to 0, then x is the GCD. Otherwise, repetitively do through the process
using y and the remainder of dividing x by y (commonly referred to as: x mod y).
Normally, the definition is written in the following format:
Writing the Program:
Today, we are going to write a C# console application (using recursion) to
implement the above algorithm. (Note: This algorithm can be written iteratively,
but we will be focusing on recursion).
1. Start Visual Studio 2008. Create a new Console Application Project.
2. Begin by creating a new class called RecursionGCD (Project Add Class
C# Class). This class should have a default constructor (i.e., an empty
constructor).
3. Inside RecursionGCD create a method called gcd that takes two integer
arguments and returns an integer. (This will be the recursive method). Using
the above formula for GCD, create an equivalent in C#. Remember that ==
is used for comparison for numbers and % is used for mod in C#.
4. Inside the class Program (i.e., the default class created by Visual Studio that
contains the main program) write a quick console application. The program
should display to the user a welcome message (Welcome to the GCD
Recursion Program). The program should prompt the user to enter the first
number (and read this number). The program should prompt the user to
enter the second number (and read this number). After receiving the
numbers from the user, create an instance of the class RecursionGCD and
call the gcd method. The program should display a final message containing
the GCD of the two numbers entered by the user.
pf2

Partial preview of the text

Download CS1120 Spring 2009 Recursion Worksheet: Euclidean Algorithm and GCD in C# and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

CS1120 – Spring 2009

Recursion Worksheet – 01/14/

(This worksheet solution does not need to be turned in).

Historical Significance: Euclidean Algorithm is one of the oldest algorithms – at least 300 BCE in Ancient Greece. The Euclidean Algorithm is contains one of the “most famous” recursion functions and is used to compute the greatest common divider (GCD) of any two integers. It is extremely popular because it does not require any factoring. (For more information: http://en.wikipedia.org/wiki/Euclidean_algorithm). How GCD work: Given two integers (let’s call them x and y. Both x and y are not equal to zero). If y is equal to 0, then x is the GCD. Otherwise, repetitively do through the process using y and the remainder of dividing x by y (commonly referred to as: x mod y). Normally, the definition is written in the following format: Writing the Program: Today, we are going to write a C# console application (using recursion) to implement the above algorithm. (Note: This algorithm can be written iteratively, but we will be focusing on recursion).

  1. Start Visual Studio 2008. Create a new Console Application Project.
  2. Begin by creating a new class called RecursionGCD (Project  Add Class  C# Class). This class should have a default constructor (i.e., an empty constructor).
  3. Inside RecursionGCD create a method called gcd that takes two integer arguments and returns an integer. (This will be the recursive method). Using the above formula for GCD, create an equivalent in C#. Remember that == is used for comparison for numbers and % is used for mod in C#.
  4. Inside the class Program (i.e., the default class created by Visual Studio that contains the main program) write a quick console application. The program should display to the user a welcome message (Welcome to the GCD Recursion Program). The program should prompt the user to enter the first number (and read this number). The program should prompt the user to enter the second number (and read this number). After receiving the numbers from the user, create an instance of the class RecursionGCD and call the gcd method. The program should display a final message containing the GCD of the two numbers entered by the user.

Challenge Question: Can you write the gcd method from RecursionGCD as only one line of code?