The Objective
The objective of this assignment is to become familiar with writing C programs in a Unix environment.Background
A palindromic number (or simply palindrome) is an integer whose digits are the same whether read forward or backward. Its first digit and last digit are the same, and if long enough, its second and next-to-last digits are the same, and so on. Some examples of palindromic numbers are 121, 93439, 6, 22, and 887020788.It is interesting to note that if an integer, N, is not a palindrome then reversing the digits of N (to form N') and forming the sum N + N' will often produce a palindrome. For example, if N = 123, then N' = 321 and N + N' = 444 is a palindrome.
If N + N' is not a palindrome, continuing this process will often produce a palindrome eventually. For example if N = 964, N' = 469 then N + N' = 1433 which is not a palindrome. Continuing, however, we set N = 1433 so N' = 3341 and now N + N' = 4774 which is a palindrome.
Although most numbers produce a palindromic number with just a few (less than 10) reverse and add iterations, some take many more iterations. For example, the number 89 takes 24 iterations of the reverse and add algorithm before a palindromic number is produced.
Some numbers are being tested that appear to never form a palindromic number regardless of the number of reverse and add iterations that have been performed. A lychrel (pronounced la-shrel) is a number that has been proven to never form a palindrome even after repeated reversal and addition. Wade VanLandingham is credited for the term lychrel. Although proof has been found that some numbers in other bases are lychrels, proof has not been found that any base 10 number is a lychrel. So numbers in base 10 that appear to be lychrels can technically only be called candidate lychrels. Several candidate lychrels are known. The smallest candidate lychrel is 196. The reverse/add algorithn has not yet produced a palindromic number from 196 even after millions of iterations. The first million non-palindromic numbers were discovered by John Walker.
If you're interested, more information about lychrels can be found at http://www.p196.org/
The Task
Your assignment will be to test integers input by the user to determine if the integer is a palindrome or candidate lycherl and if not, to perform the reverse/add algorithm described above to create a palindrome showing all of the sums produced along the way.Since we don't want your project to run forever, you'll need to check to see if the integer input by the user is a candidate lychrel first. If it is, don't attempt to perform the reversal and adds, just print a message and continue. See the sample output below. A list of candidate lychrels will be provided in a file that is read by your progam.
If the number input by the user is not a candidate lychrel, you should check to see if it is a palindrome. If it's not a palindrome, perform as many reverse/add iterations as necessary until a palindrome is formed, printing the numbers used and the sum at each iteration. After the palindrome has been formed you must display the number of reverse and add interation that were necessary to create it.
How does your program work?
Candidate Lychrel File Format
The text file containing the candidate lychrels consists of a set of integers separated by white space. The first integer in the file is the number of candidate lychrels which follow. The remaining integers are the candidate lychrels. The file is guaranteed to be well-formed.Hints, Notes and Requirements
unsigned long long
data type.
Use the %llu
format to input and output variables of this type.
Sample Output
linux3[132]% Project1 Please input the candidate lychrel filename: lychrels.dat Please input the value of N: 999 999 is a palindrome Please input the value of N: 7235 7235 + 5327 = 12562 12562 + 26521 = 39083 39083 + 38093 = 77176 77176 + 67177 = 144353 144353 + 353441 = 497794 A palindrome was obtained in 5 iterations Please input the value of N: 196 196 is a candidate lychrel Please input the value of N: 0 Goodbye linux1[104]%
Project Grading
Grading for this project will be broken into two parts. Point values shown below represent an approximate breakdown.Submitting the Program
You can submit your project using the submit command.
submit cs313 Proj1 project1.c
See this page for a description of other project submission related commands. To verify that your project was submitted, you can execute the following command at the Unix prompt. It will show all files that you submitted in a format similar to the Unix 'ls' command.
submitls cs313 Proj1