lab exercise
funloops
Background:
1. Magic square problem:
a. Some perfect squares have unique mathematical properties. For example, 36 is:
• a perfect square, 62
• and the sum of the integers 1..8 (1+2+3+4+5+6+7+8 = 36)
b. The next magic square is 1225:
• 352 = 1225
• 1225 = sum of 1 to 49
c. Write a function which prints the first n magic squares.
2. Reversing an integer problem:
a. Write a function which reverses the sequence of digits in an integer value.
• 123 ---> 321
• 1005 ---> 5001
• 2500 ---> 52 {you will not have to print out any leading zeroes, such as 0052}
3. Least Common Multiple problem:
a. write a function which determines the Least Common Multiple of two integers. For example, the LCM of the following pairs:
2,3 LCM = 6
4,10 LCM = 20
12,15 LCM = 60
7,70 LCM = 70
Assignment:
1. Code separate functions to solve each problem.
2. Test each function using the following instructions.
3. You will need to work with long integers for problems 1 & 2.
Instructions:
1. Find the first four magic squares. The first one is the integer 1.
2. Solve these values for reverse the digits:
12345 10001 1200 5
3. Find the LCM of the following pairs of values:
15, 18
40, 12
2, 7
100, 5
4. You may use the following form for function main
main ()
{
magicsquare (4);
cout << "12345 reversed ---> " << reverse (12345) << endl;
cout << "10001 reversed ---> " << reverse (10001) << endl;
cout << "1200 reversed ---> " << reverse (1200) << endl;
cout << "5 reversed ---> " << reverse (5) << endl << endl;
cout << "LCM (15,18) = " << lcm (15,18) << endl;
cout << "LCM (40,12) = " << lcm (40,12) << endl;
cout << "LCM (2,7) = " << lcm (2,7) << endl;
cout << "LCM (100,5) = " << lcm (100,5) << endl;
}
5. If you have a fast computer, try finding the first 6 magic squares. Careful, this will tie up the computer for awhile.