3: Fraction

Example number 3.1, Page number 3.4

In [7]:
#importing modules
import math
from __future__ import division

#Variable declaration
numbers1=[5/6,3/4,5/8,6/7];
numbers2=[1/2,3/5,3/10,21/50];
numbers3=[7/12,5/16,17/36,1/3];
numbers4=[3/5,5/7,13/16,97/104];
numbers5=[2/91,5/177,22/1091,13/558];

#Calculation
a1=sorted(numbers1,reverse=True);     #descending order for 1st case
a2=sorted(numbers2,reverse=True);     #descending order for 2nd case
a3=sorted(numbers3,reverse=True);     #descending order for 3rd case
a4=sorted(numbers4,reverse=True);     #descending order for 4th case
a5=sorted(numbers5,reverse=True);     #descending order for 5th case

#Result
print "the descending order for 1st case is",a1
print "the descending order for 2nd case is",a2
print "the descending order for 3rd case is",a3
print "the descending order for 4th case is",a4
print "the descending order for 5th case is",a5
the descending order for 1st case is [0.8571428571428571, 0.8333333333333334, 0.75, 0.625]
the descending order for 2nd case is [0.6, 0.5, 0.42, 0.3]
the descending order for 3rd case is [0.5833333333333334, 0.4722222222222222, 0.3333333333333333, 0.3125]
the descending order for 4th case is [0.9326923076923077, 0.8125, 0.7142857142857143, 0.6]
the descending order for 5th case is [0.02824858757062147, 0.023297491039426525, 0.02197802197802198, 0.02016498625114574]

Example number 3.2, Page number 3.5

In [4]:
#importing modules
import math
from __future__ import division

#Variable declaration
p1=5/12;     #part
p2=3+(3/4);   #part
a=100;    #amount(Rs)

#Calculation
x=p2*a/p1;     #required amount(Rs)

#Result
print "required amount is",x,"Rs"
required amount is 900.0 Rs

Example number 3.3, Page number 3.5

In [5]:
#importing modules
import math
from __future__ import division

#Variable declaration
n=5*12;     #number of bananas
fp=6;    #fraction part

#Calculation
rf=fp/n;    #required fraction

#Result
print "required fraction is",rf
required fraction is 0.1

Example number 3.4, Page number 3.5

In [6]:
#importing modules
import math
from __future__ import division

#Variable declaration
n=40;    #number of students
np=7/10;   #number of students present

#Calculation
na=(1-np)*n;   #number of absentees

#Result
print "number of absentees is",na
number of absentees is 12.0

Example number 3.5, Page number 3.6

In [2]:
#importing modules
import math
from __future__ import division

#Variable declaration
ps=2/7;    #part of savings
al=1000;   #amount left(Rs) 

#Calculation
s=al/(1-ps);     #savings(Rs)

#Result
print "savings is",s,"Rs"
savings is 1400.0 Rs

Example number 3.6, Page number 3.6

In [3]:
#importing modules
import math
from __future__ import division

#Variable declaration
abyb=3/8;     
xbyy=4/5;
pl=40;     #pages left

#Calculation
p=pl/((1-abyb)*(1-xbyy));    #total number of pages

#Result
print "total number of pages is",p
total number of pages is 320.0

Example number 3.7, Page number 3.6

In [4]:
#importing modules
import math
from __future__ import division

#Variable declaration
abyb=2/5;     
xbyy=3/10;
mbyn=1/8;
ba=1400;     #balance amount(Rs)

#Calculation
ts=ba/(1-(abyb+xbyy+mbyn));     #total salary(Rs)

#Result
print "total salary is",ts,"Rs"
total salary is 8000.0 Rs

Example number 3.8, Page number 3.6

In [7]:
#importing modules
import math
from __future__ import division

#Variable declaration
abyb=1/3;     
xbyy=1/4;
mbyn=1/5;
ba=1760;     #balance amount(Rs)

#Calculation
ts=ba/((1-abyb)*(1-xbyy)*(1-mbyn));     #total salary(Rs)

#Result
print "total salary is",ts,"Rs"
total salary is 4400.0 Rs

Example number 3.9, Page number 3.7

In [9]:
#importing modules
import math
from __future__ import division

#Variable declaration
abyb=1/3;     
xbyy=1/4;
mbyn=1/5;
ba=1760;     #balance amount(Rs)

#Calculation
ts=ba/((1-abyb)*(1-(xbyy+mbyn)));     #total salary(Rs)

#Result
print "total salary is",ts,"Rs"
total salary is 4800.0 Rs

Example number 3.10, Page number 3.7

In [10]:
#importing modules
import math
from __future__ import division

#Variable declaration
L=250;    #length in mud(Rs)
p=(4/7)-(1/3);    #part in mud

#Calculation
Tl=L/p;     #total length of pole(cm)

#Result
print "total length of pole is",Tl,"cm"
total length of pole is 1050.0 cm

Example number 3.11, Page number 3.7

In [12]:
#importing modules
import math
from __future__ import division

#Variable declaration
dt=60;    #distance travelled(km)
dp=5/8;   #part travelled 

#Calculation
x=dt*(1-dp)/dp;     #journey still left(km)

#Result
print "journey still left is",x,"km"
journey still left is 36.0 km

Example number 3.12, Page number 3.8

In [13]:
#importing modules
import math
from __future__ import division

#Variable declaration
a=0.685;
b=325;
c=300;

#Calculation
x=c-(a*b);     #amount to be added

#Result
print "amount to be added is",x
amount to be added is 77.375

Example number 3.13, Page number 3.8

In [14]:
#importing modules
import math
from __future__ import division

#Variable declaration
abyb=0.15;     
xbyy=0.65;
l=20;     #length of pole(m)

#Calculation
L=l*(1-(abyb+xbyy));     #length above water(m)

#Result
print "length above water is",L,"m"
length above water is 4.0 m

Example number 3.14, Page number 3.8

In [16]:
#importing modules
import math
from __future__ import division

#Variable declaration
abyb=0.375;     
xbyy=0.4;
ba=3375;     #balance amount(Rs)

#Calculation
ts=ba/(1-(abyb+xbyy));     #total salary(Rs)
ws=abyb*ts;        #wife's share of money(Rs)

#Result
print "wife's share of money is",ws,"Rs"
wife's share of money is 5625.0 Rs

Example number 3.15, Page number 3.8

In [8]:
#importing modules
import math
from __future__ import division
from fractions import Fraction

#Variable declaration
#a=1/3
a1=1;
a2=3;
#b=4/5
b1=4;
b2=5;
x1=2;
x2=1;
#y=7/2
y1=7;
y2=2;

#Calculation
f1=(a1+b1)/(a2+b2);       #fraction between 1/3 and 4/5
f2=(x1+y1)/(x2+y2);       #fraction between 2 and 7/2

#Result
print "fraction between 1/3 and 4/5 is",Fraction(f1)
print "fraction between 2 and 7/2 is",f2
fraction between 1/3 and 4/5 is 5/8
fraction between 2 and 7/2 is 3.0

Example number 3.16, Page number 3.8

In [12]:
#importing modules
import math
from __future__ import division
from fractions import Fraction

#Variable declaration
#a=1/3
a1=1;
a2=3;
#b=4/5
b1=4;
b2=5;

#Calculation
f11=a1+b1;    #numerator of 1st fraction
f12=a2+b2;    #denominator of 1st fraction
f1=f11/f12;   #1st fraction
f21=a1+f11;   #numerator of 2nd fraction
f22=a2+f12;   #denominator of 2nd fraction
f2=f21/f22;   #2nd fraction
f31=f11+b1;   #numerator of 3rd fraction
f32=f12+b2;   #denominator of 3rd fraction 
f3=f31/f32;   #3rd fraction 

#Result
print "the three fractions are",f2,Fraction(f1),f3
the three fractions are 0.545454545455 5/8 0.692307692308

Example number 3.17, Page number 3.9

In [9]:
#importing modules
import math
from __future__ import division

#Variable declaration
a=22/63;
b=4/11;
c=15/46;
d=33/98;
e=1/3;

#Calculation
if(a<e):
    print "22/63 is lesser than 1/3"
if(b<e):
    print "4/11 is lesser than 1/3"
if(c<e):
    print "15/46 is lesser than 1/3"
if(d<e):
    print "33/98 is lesser than 1/3"        
15/46 is lesser than 1/3