Professional Documents
Culture Documents
Ques 5
Ques 5
Ques 5
You are given a bag filled with squares made out of wire. No two squares have the
same size. The bag contains a
single square of length equal to every possible integer between 39 and 93
inclusive. You must discard from the bag
only those squares that have an odd area.
(a) Avoiding manual addition, what the the total length of wire used to make all
the remaining squares in the bag?
(b) Write a pseudo code algorithm entitled SUM_SQUARES that solves this problem
using variables and a loop.
BRAINSTORMING
Every possible integer between 39 and 93 inclusive which would be X = 39 + 40 +
41...+ 92 + 93
That is until all squares that have an odd area are discarded which would be X = 40
+ 42 + 44...+ 90 + 92
SOLUTION
a) Let X be the sum of all integers
X = 40 + 42 + 44...+ 90 + 92
X = 2(20 + 21 + 22...+ 45 + 46)
X = 2[(1 + 2 + 3...+ 45 + 46)-(1 + 2 + 3...+ 18 + 19)]
X = 2[S(46) - S(19)]
X = 2[S(46) - S(19)] = 2[46 x 47/2 - 19 x 20/2] = 2[23 x 47 - 19 x 10]
X = 2[23 x (30 + 17) - (20 - 1) x 10]
X = 2[690 - 190] = 2[500] = 1000
b) SUM_SQUARES
BEGIN
i <- 40
sum <- 0
WHILE i <= 92 :
IF i MOD 2 == 0 THEN
sum = sum + 2
ENDIF
i = i + 2
ENDWHILE
PRINT "Sum of all even integers from 40 to 92 inclusive: ", sum, sep=''
END