# 2: HCF and LCM of numbers¶

## Example number 2.1, Page number 2.5¶

In :
#importing modules
import math
from __future__ import division
from fractions import gcd

#Variable declaration
a=200;
b=320;    #given two numbers

#Calculation
n=gcd(a,b);      #required number

#Result
print "greatest number that will exactly divide is",n

greatest number that will exactly divide is 40


## Example number 2.2, Page number 2.5¶

In :
#importing modules
import math
from __future__ import division
from fractions import gcd

#Variable declaration
a=148;
b=246;
c=623;     #given three numbers
a1=4;
b1=6;
c1=11;     #remainders left

#Calculation
A=a-a1;
B=b-b1;
C=c-c1;      #obtained three numbers
n=gcd(A,B);     #GCD of A and B
rn=gcd(n,C);     #GCD of all the 3 numbers

#Result
print "greatest number that will exactly divide is",rn

greatest number that will exactly divide is 12


## Example number 2.3, Page number 2.5¶

In :
#importing modules
import math
from __future__ import division
from fractions import gcd

#Variable declaration
n1=27;
n2=35;
n3=45;
n4=49;    #given numbers
r=6;      #remainder

#Calculation
l1=n1*n2/gcd(n1,n2);     #lcm of n1 and n2
l2=n3*n4/gcd(n3,n4);     #lcm of n3 and n4
l=l1*l2/gcd(l1,l2);      #lcm of the given 4 numbers
L=l+6;             #required least number

#Result
print "required least number is",L

required least number is 6621.0


## Example number 2.4, Page number 2.5¶

In :
#importing modules
import math
from __future__ import division
from fractions import gcd

#Variable declaration
a=36;
b=48;
c=64;     #given three numbers
a1=25;
b1=37;
c1=53;     #remainders left

#Calculation
A=a-a1;
B=b-b1;
C=c-c1;      #obtained three numbers
l1=a*b/gcd(a,b);     #lcm of a and b
l2=l1*c/gcd(l1,c);     #lcm of the given 3 numbers
L=l2-A;             #required least number

#Result
print "required least number is",L

required least number is 565.0


## Example number 2.5, Page number 2.5¶

In :
#importing modules
import math
from __future__ import division
from fractions import gcd

#Variable declaration
a=300;
b=510;
c=1290;     #given three numbers(cm)

#Calculation
n=gcd(a,b);   #gcd of a and b
rn=gcd(n,c);    #gcd of all the numbers

#Result
print "greatest possible length of scale is",rn,"cm"

greatest possible length of scale is 30 cm


## Example number 2.6, Page number 2.5¶

In :
#importing modules
import math
from __future__ import division
from fractions import gcd

#Variable declaration
a=15;
b=21;
c=30;     #given three numbers
r=8;      #change

#Calculation
l1=a*b/gcd(a,b);     #lcm of a and b
l2=l1*c/gcd(l1,c);     #lcm of the given 3 numbers
L1=l2-r;             #required least number when increased by change
L2=l2+r;             #required least number when decreased by change

#Result
print "required least number when increased by change is",L1
print "required least number when decreased by change is",L2

required least number when increased by change is 202.0
required least number when decreased by change is 218.0


## Example number 2.7, Page number 2.5¶

In :
#importing modules
import math
from __future__ import division
from fractions import gcd

#Variable declaration
n=9999;    #greatest 4 digit number
n0=1000;    #smallest 4 digit number
n1=12;
n2=15;
n3=20;
n4=35;     #given four numbers

#Calculation
l1=n1*n2/gcd(n1,n2);     #lcm of n1 and n2
l2=n3*n4/gcd(n3,n4);     #lcm of n3 and n4
l=l1*l2/gcd(l1,l2);      #lcm of the given 4 numbers
a=n%l;
A=n-a;     #greatest 4 digit number divisible by given numbers
b=n0%l;
B=n0+l-b;     #smallest 4 digit number divisible by given numbers

#Result
print "greatest 4 digit number divisible by given numbers is",A
print "answer given in the book is wrong"
print "smallest 4 digit number divisible by given numbers is",B

greatest 4 digit number divisible by given numbers is 9660.0
answer given in the book is wrong
smallest 4 digit number divisible by given numbers is 1260.0


## Example number 2.8, Page number 2.6¶

In :
#importing modules
import math
from __future__ import division
from fractions import gcd

#Variable declaration
n1=6;
n2=7;
n3=8;
n4=9;     #intervals(sec)
n=2;      #number of hours

#Calculation
l1=n1*n2/gcd(n1,n2);     #lcm of n1 and n2
l2=n3*n4/gcd(n3,n4);     #lcm of n3 and n4
l=l1*l2/gcd(l1,l2);      #lcm of the given 4 numbers
t=n*60*60/l;           #number of times they toll together

print "all bells toll together after an interval of",l,"seconds"
print "number of times they toll together in 2 hours is",int(t),"times"

all bells toll together after an interval of 504.0 seconds
number of times they toll together in 2 hours is 14 times


## Example number 2.9, Page number 2.6¶

In :
#importing modules
import math
from __future__ import division
from fractions import gcd

#Variable declaration
n=9999;    #greatest 4 digit number
n0=1000;    #smallest 4 digit number
n1=12;
n2=18;
n3=21;
n4=28;     #given four numbers
r=3;       #remainder

#Calculation
l1=n1*n2/gcd(n1,n2);     #lcm of n1 and n2
l2=n3*n4/gcd(n3,n4);     #lcm of n3 and n4
l=l1*l2/gcd(l1,l2);      #lcm of the given 4 numbers
a=n%l;
A=n-a+r;     #greatest 4 digit number divisible by given numbers
b=n0%l;
B=n0+l-b+r;     #smallest 4 digit number divisible by given numbers

#Result
print "greatest 4 digit number divisible by given numbers is",A
print "answer given in the book is wrong"
print "smallest 4 digit number divisible by given numbers is",B

greatest 4 digit number divisible by given numbers is 9831.0
answer given in the book is wrong
smallest 4 digit number divisible by given numbers is 1011.0


## Example number 2.10, Page number 2.6¶

In :
#importing modules
import math
from __future__ import division
from fractions import gcd

#Variable declaration
a=6;
b=8;
c=9;     #given three numbers
r=5;      #remainder

#Calculation
l1=a*b/gcd(a,b);     #lcm of a and b
l2=l1*c/gcd(l1,c);     #lcm of the given 3 numbers
n1=(l2*3)+r;
n2=(l2*4)+r;           #required numbers

#Result
print "required numbers are",n1,"and",n2

required numbers are 221.0 and 293.0


## Example number 2.11, Page number 2.6¶

In :
#importing modules
import math
from __future__ import division
from fractions import gcd

#Variable declaration
a=960;
b=512;        #length of wires(cm)

#Calculation
hcf=gcd(a,b);    #HCF of two lengths
n1=a/hcf;         #number of Aluminium wire pieces
n2=b/hcf;         #number of Copper wire pieces

#Result
print "number of Aluminium wire pieces is",n1
print "number of Copper wire pieces is",n2

number of Aluminium wire pieces is 15.0
number of Copper wire pieces is 8.0


## Example number 2.12, Page number 2.7¶

In :
#importing modules
import math
from __future__ import division
from fractions import gcd

#Variable declaration
hcf=16;        #HCF 0f two numbers
lcm=240;       #LCM of two numbers
a=48;     #one of the numbers

#Calculation
b=hcf*lcm/a;     #the other number

#Result
print "the other number is",b

the other number is 80.0


## Example number 2.13, Page number 2.7¶

In :
#importing modules
import math
from __future__ import division
from fractions import gcd

#Variable declaration
a=105;     #number of oranges
b=175;     #number of bananas

#Calculation
hcf=gcd(a,b);     #number of students

#Result
print "number of students is",hcf

number of students is 35


## Example number 2.14, Page number 2.7¶

In :
#importing modules
import math
from __future__ import division
from fractions import gcd

#Variable declaration
a=11;
b=121;
c=1331;     #given three numbers(cm)

#Calculation
n=gcd(a,b);   #gcd of a and b
rn=gcd(n,c);    #gcd of all the numbers

#Result
print "resultant HCF is",rn/10**4

resultant HCF is 0.0011


## Example number 2.15, Page number 2.7¶

In :
#importing modules
import math
from __future__ import division
from fractions import gcd

#Variable declaration
a=22;
b=540;
c=108;     #given three numbers

#Calculation
l1=a*b/gcd(a,b);     #lcm of a and b
l=l1*c/gcd(l1,c);     #lcm of the given 3 numbers

#Result
print "resultant LCM is",l/10

resultant LCM is 594.0


## Example number 2.16, Page number 2.7¶

In :
#importing modules
import math
from __future__ import division
from fractions import gcd

#Variable declaration
a=3**5;
b=3**9;
c=3**14;     #given three numbers(cm)
base=3;
i=[5, 9, 14]      #indices

#Calculation
n=gcd(a,b);   #gcd of a and b
rn=gcd(n,c);    #gcd of all the numbers
hcf=min(i);

#Result
print "resultant HCF is",rn,"or",base,"**",hcf

resultant HCF is 243 or 3 ** 5


## Example number 2.17, Page number 2.7¶

In :
#importing modules
import math
from __future__ import division
from fractions import gcd

#Variable declaration
a=4**5;
b=4**-81;
c=4**12;
d=4**7;        #given three numbers(cm)
base=4;
i=[5, -81, 12, 7]      #indices

#Calculation
lcm=max(i);      #resultant LCM

#Result
print "resultant LCM is",base,"**",lcm

resultant LCM is 4 ** 12