0% found this document useful (0 votes)
2 views

Lab1_array

This document outlines Programming Assignment #1 for the Data Structures course at National Yang Ming Chiao Tung University, focusing on calculating the greatest common divisor (GCD) of two large integers using efficient methods. Students are required to implement their solutions in C/C++, submit their work by October 10, 2023, and follow specific submission guidelines to avoid penalties. The assignment includes input/output formats, grading policies, and hints for handling large integers.

Uploaded by

張任禔
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab1_array

This document outlines Programming Assignment #1 for the Data Structures course at National Yang Ming Chiao Tung University, focusing on calculating the greatest common divisor (GCD) of two large integers using efficient methods. Students are required to implement their solutions in C/C++, submit their work by October 10, 2023, and follow specific submission guidelines to avoid penalties. The assignment includes input/output formats, grading policies, and hints for handling large integers.

Uploaded by

張任禔
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

National Yang Ming Chiao Tung University September 26, 2023

Department of Electrical and Computer Engineering Instructor: H.-M. Chen


Data Structures, Fall 2023 TA: H.-J. Luo

Programming Assignment #1
Arrays

1. Problem Description
The greatest common divisor 𝑔𝑐𝑑⁡(𝑎, 𝑏) between two positive integers 𝑎 and 𝑏
is defined as the largest positive integer that divides both 𝑎 and 𝑏 without a
remainder.
GCD is a very powerful tool in modern cryptography, and when the target integers
to be calculated are small (less than 108 ), GCD can be calculated in a few seconds with
a naïve method. However, the numbers in modern cryptography requires at least 512
digits to prevent attackers from using a brute-force method to derive the secret key. This
required number is too large for the naïve methods to calculate GCD in a reasonable
time and the numbers exceeds the limit of even long long in the C language. In this
problem, you will need to calculate the GCD of two big integers efficiently.

2. Input Format
One line containing two integers, 𝑎 and 𝑏, where 0 < 𝑎, 𝑏 < 10256 .

3. Output Format
An integer representing 𝑔𝑐𝑑⁡(𝑎, 𝑏) with a single end-of-line (endl).

4. Sample Input / Output


Sample Input 1
20230925 52903202

Sample Output 1
11
Sample Input 2
111111222222333333444444555555 666666777777888888999999

Sample Output 2
333333

5. Submission Information
1. Your program must be written in C/C++ language and can be compiled on the
Linux platform.
2. Please put the required files in a folder named with your Student_ID and the
required files should also be named with your Student_ID (.cpp, .c).
3. To submit your program, please use the command below to compress the folder
named with “[Student_ID].tar” in the Linux environment and upload it to E3.
tar cvf Student_ID.tar Student_ID

6. Due Date
Be sure to upload the tar file by “October 10, 2023”. There will be a 10% penalty per
day for the first four days (weekend included) and will not be accepted afterwards.

7. Grading Policy
The programming assignment will be graded based on the following rules:
˙ Pass the open cases with compilable source code (60%)
˙ Pass the hidden cases with compilable source code (40%)
˙ -10% of your total score if any file occurs naming error or not compress
˙ No credits for plagiarism

Hint
To deal with the big integers, we need a “data structure”, such as an integer array in C
to represent larger values. For instance, you can use an integer array where each element
represents one (decimal) digit, like representing 202309 by the following code snippet.
vector<int> digits = {9, 0, 3, 2, 0, 2};
It is not required to use the representation above, though. You can use any representation
that facilitates your implementation.

Algorithm: Binary Algorithm for Greatest Common Divisor


Input: Two positive integers 𝑎 and 𝑏.
Output: A positive integer 𝑎𝑛𝑠 representing greatest common divisor of 𝑎 and 𝑏.
𝑛 ← 𝑚𝑖𝑛(𝑎, 𝑏) , 𝑚 ← 𝑚𝑎𝑥(𝑎, 𝑏) , 𝑎𝑛𝑠 ← 1
while 𝑛 ≠ 0 and 𝑚 ≠ 0 do
if 𝑛 is even and 𝑚 is even then
𝑎𝑛𝑠 ← 𝑎𝑛𝑠 × 2, 𝑛 ← 𝑛/2, 𝑚 ← 𝑚/2
else if 𝑛 is even then
𝑛 ← 𝑛/2
else if 𝑚 is even then
𝑚 ← 𝑚/2
end
if 𝑛 > 𝑚 then
𝑠𝑤𝑎𝑝(𝑛, 𝑚)
𝑚 ← (𝑚 − 𝑛)
end
return 𝑛 × 𝑎𝑛𝑠

You might also like