HOME | SIGN UP | ACCOUNT | SUBSCRIBE | STORE | WHAT'S NEW | TESTIMONIALS | TROUBLESHOOTING | SUPPORT AREA | SITE NEWS | LOGIN
Classes & methods · News · Jobs · Links · Downloads · Articles · Conferences · Magazines · Training · Consultants · Consulting · Blogs · FAQ
Books · Applications · Meetings · User groups · Speakers · User group leaders · Universal Thread Magazine · Coverages · Photos · Videos

 ID: 23746Created on: May 30, 2004 20:51Updated on: April 22, 2010 21:34

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

Comments
Add a comment

More entries from this member

  1. 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.

  2. 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.

  3. 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).

  4. 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.

  5. 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.

  6. 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.

  7. 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.

DevCon 2002
DateContentDetail
06/09 16:31MeetingSQL Server Tools, Strate...
02/09 00:44NewsPEM Editor 6 has been re...
27/08 17:57NewsSouthwest Fox 2010 Early...
25/08 04:30NewsCryptoLicensing For .Net...
22/08 09:58NewsWindows Phone 7 Gold Rush
16/08 03:05NewsVisual WebGui reveals it...
11/08 17:54MeetingSo You Think You Want To...
10/08 16:41News.NET Training for VFP Pr...
09/08 18:33MeetingLINQ & SQL Server Reporting
03/08 08:15JobsSQL/VFP Developer for Ma...
29/07 10:59News.NET 4 for VFP Developer...
26/07 10:13JobsVFP Developer (Two Perma...
25/07 06:44NewsWith Visual WebGui 6.4 R...
24/07 11:00JobsQuality Assurance Softwa...
24/07 10:53JobsIntegrations and Web Dev...

Copyright © 1993 - 2010 Level Extreme Inc., All Rights Reserved · Telephone: 506-783-9007 Email: info@universalthread.com · Privacy & Security · Copyright · Terms & Conditions