Assignment 3 - fall 2012

Report 3 Downloads 167 Views
COMP 208: Computers in Engineering Fall 2012: Assignment 3 The Quality of Life of Numbers Integrity These assignments are to be done individually. You can collaborate on understanding the problem but you must write the solution individually. Your submission might be subject to Plagiarism detection software. Due Date Assignment 3 is due on Wednesday, October 24th, at midnight. The cutoff is automated and is exactly at this time. Assignments submitted within the next hour will be considered late. After that time they will not be accepted at all. Introduction Math geeks and computer nerds love to anthropomorphize numbers and assign emotions and personalities to them. Thus there is defined the concept of a “happy” number. A happy number is defined as an integer in which the following sequence ends with the number 1. 1. Start with the number itself. 2. Calculate the sum of the square of each individual digit. 3. If the sum is equal to 1, then the number is happy. If the sum is not equal to 1, then repeat steps 1 and 2. A number is considered unhappy once the same number occurs multiple times in a sequence because this means there is a loop and it will never reach 1. For example, the number 7 is a “happy” number: 1. 72 = 49 2. 42 + 92 = 16 + 81 = 97 3. 92 + 72 = 81 + 49 = 130 4. 12 + 32 + 0 = 10 5. 12 + 02 = 1 1

Once the sequence reaches the number 1, it will stay there forever since 12 = 1 On the other hand, the number 6 is not a happy number as the sequence that is generated is the following: 6, 36, 45, 41, 17, 50, 25, 29, 85, 89, 145, 42, 20, 4, 16, 37, 52, 29 Once the same number occurs twice in the sequence, the sequence is guaranteed to go on infinitely, never hitting the number 1, since it repeat this cycle. Assignment Your task is to write a program which will do the following: 1. Ask the user to enter a number. Call this number N 2. Print a list of all happy numbers between 1 and N (including 1 and N) 3. Print the total count of how many happy numbers there are between 1 and N (inclusive) You may assume that the input N is less than 10,000. Sample output Here is a sample run of the program: Enter a number and I will tell you all the happy numbers less than or equal to it 100 1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100 There are 20 happy numbers between 1 and 100 Methodology Here we outline a general way you can solve this problem. 1. First write a function called IsHappyNumber. This function will take as input an integer and return a logical. 2. Inside this function, create an array that will be used to keep track of what numbers have already appeared in the sequence. You may assume that the sequence has a length of less than 1000. You

2

should make it so that this array initially contains only 0s and as you encounter numbers in the sequence you should add them to the array. 3. Start with the number the user entered. Calculate the sum of the square of its digits. (See hint below) 4. Check if the sum is equal to 1. If it is, then it is a happy number. Next, check if the number is already inside the array. If it is, then you can conclude that it is not a happy number. Otherwise, add the sum to the first free spot of your array, and repeat step 3 with the sum instead of the original number. 5. Once you have this function written, your main program will involve only 1 loop, in which you call the function IsHappyNumber() for all integer values from 1 until N and store the happy numbers into an array. Hint for calculating the sum of the square digits: There are two mathematical properties you may find helpful to extract the individual digits of a number. 1. To get the ones column of an integer, you can use the built in MOD function by checking the remainder of a number when divided by 10. For example MOD(86, 10) = 6 and MOD(100, 10) = 0 2. Every time you divide an integer by 10, it will shift the decimal point by one. This means that something formerly in the 10s column is now in the 1s column, something formerly in the 100s column is now in the 10s column, etc. You can use these two properties to extract the individual digits of the number and then calculate the sum of their squares. General hints for assignment: Solving this problem involves a lot of variables. You may find it useful to start with a smaller problem and work your way up. For example, start by trying to calculate the sum of the squares of the digits of a number. Then move on keeping track of the sequence values. We have talked in class about the ideas of functions and subroutines. Although not required, breaking your problem into smaller problems by writing functions, for example, CalculateSumSquareDigits(x), CheckIfArrayContainsValue(array, target, arraysize), will greatly reduce the chances of errors as the code will be more clearly divided. This will make your code significantly easier to test because you can

3

test your functions separately, and once you conclude they are working not worry about changing them again. Requirements Your code must meet these requirements: The program must be written in Fortran Use sensible variable names. Comment and indent your code. Code not properly indented may lose marks. Submit only the .f90 file. Don’t submit the .exe (name your source file A3_123456789.f90 where 123456789 is your ID) If any of the above requirements is not respected you might lose marks.

4