CSC281 Project: Calculating Last Digits of Large Integers

Verified

Added on  2022/08/20

|7
|809
|13
Project
AI Summary
This project, undertaken for CSC281 Discrete Mathematics at King Saud University, focuses on calculating the last few digits of large integers, such as 2017^2018^2019. The solution employs Euler's theorem and modular exponentiation to efficiently determine these digits. The assignment involves creating a Java program that implements an algorithm to compute the last digits, using methods for modular exponentiation and printing the results. The algorithm starts by creating a Java class and methods, including a power method for modular exponentiation, and a method to print the last k digits. The program takes user input, calls the relevant methods, and provides the final result. The project utilizes the concept that (a^b) mod k helps to find the last digits. The report concludes that the provided algorithm and Java implementation successfully determine the last digits, with the method adaptable to find varying numbers of last digits by adjusting the modulus used. The project includes a bibliography referencing related mathematical concepts and theorems.
Document Page
Running head: DISCRETE MATHEMATICS
DISCRETE MATHEMATICS
Enter name of the Student:
Enter name of the University:
Author note:
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
1DISCRETE MATHEMATICS
Algorithm
To implement this solution, a proper algorithm is used. The algorithm which is used
in this program is as follows:
1. Start.
2. Create the class
3. Create a method for power
4. Initialize result = 1.
5. while (y>0) and (y&1) !=0
6. result=(result *x).
7. return result.
8. Create another method for print the last 2 digits
9. Start loop from i=1 to k.
10. temp *=10.
11. temp = power(a,b,temp)
12. Start loop from i=0 to k-Integer.toString(temp).length().
13. if (temp !=0), then print temp.
14. Create main method.
15. Take integer as user input.
16. Call the method created in step no 7.
17. Stop.
Document Page
2DISCRETE MATHEMATICS
Description of the Algorithm:
In the starting of the algorithm, first a java class need to be created, here the class
name is MainMath.
After that, a parameterized method is created, named power. Then initialize the result
= 1. After that update the x if it is more than or equal to the p. After that a while loop is
started. In this while loop, check that if y is odd, then multiply the x with the result with the
equation result = (result * x) % p and after that the y must be even. Then return the result as
the integer data type.
After that, another method is created named printLastKDigits which is also a
parameterized method, that is print last 2 digits of the equation. It is also a parameterized
method. Then initialize the temp variable such as temp = 1. After that, generate the 10 ^ k and
for this reason, a for loop is started from i=1 to k and calculate temp as temp *= 10. Then call
the modular exponentiation as temp = power (a, b, temp). If there is zero in the left most part,
for this print the leftmost zeros, since (a ^ b) % k can have the digits less than the k. In that
case, zeros must be printed. So a for loop is generated from i=0 to k-Integer.toString
(temp).length (). If the value of temp is zero, then it is already printed. If the value of temp is
not equal to zero then print the value of temp. Then the main method is created and take the
numbers from the user as input. And then call the method printLastKDigits () that complete
the coding.
Coding
Document Page
3DISCRETE MATHEMATICS
Figure 1: Coding
(Source: Created by author)
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
4DISCRETE MATHEMATICS
Figure 2: Coding
(Source: Created by author)
Document Page
5DISCRETE MATHEMATICS
Figure 3: Coding
(Source: Created by author)
Figure 1: Output
(Source: Created by author)
Conclusion
From the above algorithm and coding, it can be concluded that, if the last two digits
need to find from the equation like 2017 ^ 2018 ^ 2019, then the above algorithm can be
used. Here Java programming language is used to implement this algorithm. 2017 ^ 2018 ^
2019 mod100 is used to find the last digit, if last three digits need to find, then mod 1000 is
used instead of mod 100. In the above algorithm the Euler’s theorem is used for the
calculation.
Document Page
6DISCRETE MATHEMATICS
Bibliography
[1] A., Straub, Core partitions into distinct parts and an analog of Euler’s theorem. European
Journal of Combinatorics, 57, pp.40-49, 2016.
[2] H., Steinlein, Fermat's Little Theorem and Gauss Congruence: Matrix Versions and
Cyclic Permutations, The American Mathematical Monthly, 124(6), pp.548-553, 2017.
[3] D., Chae, Liouville-type theorems for the forced Euler equations and the Navier–Stokes
equations. Communications in Mathematical Physics, 326(1), pp.37-48, 2014.
[4] Y., Yan, Z., Galias, X. Yu, and C., Sun, Euler’s discretization effect on a twisting
algorithm based sliding mode control, Automatica, 68, pp.203-208, 2016.
chevron_up_icon
1 out of 7
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]