| Least common multiple and greatest common factor |
|---|
| Summary: | The code shows how to quickly obtain the greatest common factor, and the least common multiple. Both functions are used when manipulating fractions, among others. Several methods are possible; the method usually taught in school involves prime numbers, but this code will execute much faster (and it is much simpler) than the use of prime numbers. | | Rating: | Rate this entry | | Created by: |  | | Hilmar Zonneveld  Independent Consultant Cochabamba |
|
* Sample invocation:
? gcf(20, 30)
? lcm(20, 30)
function gcf(tnNumber1, tnNumber2)
* Find the greatest common factor between two numbers
* Note that in VFP, integer calculations are unreliable for large numbers.
* I will illustrate the basic principle with an example.
* Find the gcf of 14 and 10.
* This gcf is the same as the gcf of 10 and 4, where 4 is obtained as 14-10.
* Repeat until one of the numbers is divisible by the other.
local lnTemp
do while tnNumber2 # 0
lnTemp = tnNumber1 % tnNumber2
tnNumber1 = tnNumber2
tnNumber2 = lnTemp
enddo
return tnNumber1
endfunc
function lcm(tnNumber1, tnNumber2)
* Find the least common multiple of two numbers.
* Note that in VFP, integer calculations are unreliable for large numbers.
* The basic principle is simply this property:
* The product of the greatest common factor and the least common multiple of two numbers
* is equal to the product of the numbers themselves:
* gcf(x,y) * lcm(x,y) = x * y
* The int() is for display purposes only.
return int(tnNumber1 * tnNumber2 / gcf(tnNumber1, tnNumber2))
endfunc More entries from this member - Breaking Inheritance December 6, 2001
(The latest update contains minor edits only.)
Five easy and fun ways to get yourself into trouble with inheritance.
A frequent source of problems in OOP is called "breaking inheritance". This document briefly describes what inheritance is, how it applies to properties and methods, and how it can be maintained or broken. Problems appear especially when inheritance is broken in methods. - CHM help file shows error messages instead of help pages October 6, 2005
Due to a recent Windows security fix, users can no longer access a CHM file on a server. The table of contents appears, but the individual pages are replaced by error messages.
Access to CHM files in specific folders can be explicitly allowed through special registry settings. - Control Arrays July 20, 2001
(The last update contains minor edits only.)
The idea is to have several controls on a form controlled with an array. Thus, you can quickly go through all the controls on the form, managing the array.
The sample code included will help you get started quickly. You can easily adapt it to manage arrays of any type of control on your forms (or other containers). - Inheritance tutorial October 7, 2005
This is a step-by-step tutorial to show inheritance, specifically in Visual FoxPro forms, as a guidance for people who are not familiar with inheritance in general, or who don’t know how to implement it in Visual FoxPro.
The basic idea of inheritance is that all your forms, or several of your forms, obtain their properties and methods from a "base form". It is possible to do changes only once, in your "base form"; the changes will be propagated to all your forms.
- Open any document November 8, 2001
The following function will open any document, with its default association (the same application that will be called when you double-click on the file, in Windows Explorer). Use it to open a text-file, a Word or Excel document, an image, etc., with an external application. - Rushmore Optimization - not always optimal July 20, 2001
Rushmore Optimization can help make queries much faster. However, "Full Rushmore Optimization" is not always a desirable goal. "Partial Optimization" is sometimes much faster.
It is often believed that to speed things up, you need to have as many indices as possible. This article explains that some indices may actually make queries slower.
Also, some additional tips on query optimization. - Show seconds in a readable format June 7, 2002
If you need to check elapsed time with seconds() or a datetime value, this function allows you to display the elapsed time in a human-readable format, that is, hours:minutes:seconds, instead of the total number of seconds. Just pass a number of seconds as a parameter.
|