Wednesday 22 June 2011

LCM and HCF algorithms

/*work out the Highest Common Factor, i.e the largest number that can
fit into the two numbers
and the lowest common multiple
i.e the smallest whole number which is a multiple of the two numbers
i.e 12 and 15
*/


/*the two numbers I'm working out LCM and HCF for */
long a =12;
long b =15;

long d=2;
long s=1;
void setup() {


while(a>=d && b>=d)
{
if((a%d==0) && (b%d==0)) {

s=s*d;
a=a/d;
b=b/d;
}
else
{
d++;
}
}
println("HCF = " + s);
println("LCM= " + s*a*b);
}


void draw() {
//do nothing here
}

No comments:

Post a Comment