![]() |
![]() ![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]()
![]() Make a program that inputs a number and prints out all of its integral factors. Use the facts that the factors divide into the number evenly and there cannot be an integral factor that is more than half of the number except the number itself. The factors do not have to be sorted when printed. ![]() ![]() ![]() Download the A1 Zip file containing this page plus QBASIC files for you to try. ![]() Go back to the Case Study page. |
![]() |
![]()
|
||||||||||||||||||||||||||||||||||
![]() |
![]() Review of factors. If you have taken up grade school math, you'll know what are factors or divisors of a number. With number, I mean the natural numbers (positive integers). Throughout the rest of the text, we'll be using the word "number" to refer to positive integers. Factors are numbers that divide into the given number evenly. For example, the number 12 has six factors, namely, 1, 2, 3, 4, 6, and 12. A number is a factor in itself and 1 is a factor of every number. ![]() ![]() A simple and straightforward solution. Going back to the problem, we can find the factors of a number by simply going through all the numbers from 1 to the given number and testing if the numbers are factors of the given number. Here's the pseudocode for this solution:
![]() Checking number divisibility. Now, how does one know if a number is divisible by another? Remember the process you used to do during Math class? You divide the number by the other and see if there's anything left over (i.e., there are fractional parts). I once used the following code to determine divisibility:
![]() The code above is very simple. We check if the number when divided by another has any fractional parts. But to make things more clear, we could use integer division, Num% \ Factor%, for efficiency instead of invoking the INT function, but it would be more efficient to use the MOD or modulus operator. The modulus operator takes two operands, divides the first operand by the second and returns the remainder. Thus, a number is divisible by the other if the remainder returned is zero:
![]() It's as simple as that. ![]() First draft. Now we can write the actual code to our program. We won't put any error checking in the input statement (positive integers would be the only values acceptedyou can do this easily).
![]() The factors do not have to be sorted in increasing values, but in this program, we made it so. Alternatively, we can store the factors into an array and do all sorts of things on them. We can count them, tabulate them, add them and so on. ![]() Making it more efficient. Our program, being straightforward, would naturally be inefficient. Observe that we are testing numbers greater than half the given number when it is already stated in the problem that there are no factors in that area. So we can reduce the loop to test values up to half the given number. ![]()
![]() As you can see, we are already having ideas on making the program more efficient by removing unnecessary computation. ![]()
![]() Perfect numbers. A number is perfect if the sum of its proper divisors (factors not including the number) is the number itself. The ancient Greeks only knew the first four such numbers: 6, 28, 496, and 8128. The number 1 is not perfect since it has no proper divisors. Now, there are at least thirty numbers discovered, the thirtieth having 130,100 digits. All of them are even and nobody has proven if there is an odd perfect number. ![]()
![]() Near the end of the program, we test if the number entered is 1 or else, the program will say that it is perfect, when it is not. ![]() |
||||||||||||||||||||||||||||||||||
![]()
|
|||||||||||||||||||||||||||||||||||
![]() |
![]() More about perfect numbers. Perfect numbers have aroused a lot of interest from mathematicians. They have discovered and proven all sorts of things about them. For instance, Euclid discovered that the first four perfect numbers are generated by the formula 2n - 1(2n - 1):
![]() Noticing that 2n - 1 is prime in all computations, Euclid proved that the formula 2n - 1(2n - 1) gives a perfect even number whenever 2n - 1 is prime. ![]() ![]() ![]()
![]() The fifth perfect number has 8 digits, thus debunking the first assumption. For the second assumption, the fifth perfect number indeed ends with a 6. However, the sixth also ends in a 6. It hasn't been proven that all perfect even numbers always end in a 6 or 8, but so far, the first thirty do. Also, the perfect numbers that end in 8, really ends in 28. ![]() ![]() Friendly or amicable numbers. Mathematicians, as they always do, have extended the concept of perfect numbers to such things called friendly or amicable numbers. These are number pairs that have the special property that when one adds the proper divisors of one of the numbers, one will get the other number and vice versa. The first such pair is 220 and 284. When you add the proper divisors of 220: 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110, you'll get 284. The same goes for the proper divisors of 284: 1 + 2 + 4 + 71 + 142 = 220. Perfect numbers are their own friends (they're so perfect that they don't need friends!). ![]() ![]()
![]() |
![]() |
![]() 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 |