![]() |
![]() ![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]()
![]() While driving along the highway, I noticed that the license plate ahead of me was 183184. "Curious," I thought, "the left-most three digits and the right-most three digits form numbers that differ by 1. Furthermore, the license plate number is a perfect square!" ![]() ![]() ![]() Download the A3 Zip file containing this page plus QBASIC files for you to try. ![]() Go back to the Case Study page. |
![]() |
![]()
|
|||||||||
![]() |
![]() This problem is a good candidate for solving through the use of the computer. A simple problem, yet if you try finding the numbers using just pen and paper, it's possible that you'd spend the better part of the night doing sums and products. With a computer, you just type in several lines, run the program, and presto! You have solved one of life's greatest mysteries! ![]() Separating the left and right digits. One of the first things we have to program is a way of extracting the left three digits and the right three digits of a given six-digit number. The program code used by the person who wrote this puzzle (who used GW-BASIC) is one of the simplest methods albeit it is very obtuse.
![]() The logic is that the left-most three digits are the number of thousands in the number and the right-most digits are what's left over. Following is a more "modern" and clearer way. The INT function is replaced by integer division and the right-hand digits are obtained using the MOD operand. This way, both groups are obtained in just one step each. Plus the calculations are pure integer mathsomething which you should know is very fast.
![]() Getting their difference is the next step. We do this by subtracting one from the other and taking the absolute value. (If you haven't figured this out, then you have no right to call yourself a programmer!)
![]() Now, we have almost everything we need to code in the program! ![]() Six-digit squares. By this time, you probably figured out that we could test every six-digit number, see if it's a square, and use the code above to find out if the difference is 1. The problem with that approach is that you still have to devise a test to see if a number is a square. ![]()
![]() So in our program, we should have a FOR-NEXT loop going through the range 317 to 999 and an inner variable that is assigned the square of those numbers. Take note that the squared variable has to be in long integer format or you'll get overflow errors. ![]() Final program. Listing A3.1 contains the final program. Here, we print both the six-digit square and its root.
![]() The output of this program is the following, and these numbers are the correct answers:
![]() The largest difference. To answer the question of what is the largest such difference possible, we have to revise the program a bit. We now introduce the concept of the Current Maximum/Minimum variable (at least that's how I refer to it). Anyway it works like this: As you scan each and every data (that is not sorted in the desired order), the Current Max/Min variable will keep track of the last piece of data that matched your specifications. The only problem many programmers have with this technique is that they forget to initialize the variable first. ![]() ![]() ![]()
![]() ![]()
![]() If you run the program in Listing A3.3, you'll find out that there's only one such square: 8281. |
|||||||||
![]()
|
||||||||||
![]() |
![]() While we are in the process of solving problems left and right, here are some more teasers for you to try (they're a bit harder, mind you). ![]() Six-digit cubes and powers of two. What are all the six-digit cubes whose difference between the left and right halves is a cube? What about those whose difference is a square? ![]() ![]() Tips and code for solving these problems. Generating six-digit cubes is easy and I'll leave it to you to find the proper loop values. However, testing whether the difference is a cube is much, much harder. There are many ways of doing this. First, you can test a proper range of integers and see if their cubes equal the difference. Or you can create a 1000-dimensioned integer array and turn it into a precalculated table of cubes, and search it for the difference. Or you can obtain the cube root of the difference and see if it's an integer. Since there's no built-in function for cube roots, just use exponentiation:
![]() Testing if the difference is a square is a bit simpler. ![]()
![]() To test if a number is a power of two, you can successively divide the number by two and see if it reaches 1. If at any stage the number becomes odd (except when it's 1), then the number is not a power of 2. Alternatively, you can get the logarithm of the number to the base 2 and see if it's an integer. I leave it to you to implement these ideas. ![]() Answers to the two problems. Here are the answers. Use them to check if your program is working correctly. ![]() ![]()
![]() |
![]() |
![]() Go back to the Case Study page. Home Page | Program Nook | Instructional | Open Forum Portfolio | Visitor's Area | Connections | About the Site ![]() Copyright © 1997-1999, SEAV Softwares. All rights reserved. Webmaster: Eugene Villar (SEAV); e-mail: evillar@iname.com |