Page 63 - Touchcode_C7_Flipbook
P. 63

Algorithm:                                Optimised Algorithm:
                   for num=1, num <=n, increase num by 1:               ‘n’ is the input number.

                   if (n is divisible by num, and n / num==num)         Loop starts from 1 to n/2.
                   return true                                          During  iteration, for  every integer  ‘i’,
                   else                                                 it calculate x=i*i.
                   return false                                         With ‘x’ there are 3 possibilities:
                                                                        a.  If x==n; then ‘n’ is a perfect square,

                                                                             it will return true.
                                                                        b.  If  x  >  n;  then  x  has  crossed the
                                                                             n,  and it  is not  a perfect square.

                                                                             It will return false
                                                                        c.  If above steps a or b are not true,
                                                                             then continue.



                   Coding           uiz   01                                                          Critical Thinking



                    State whether the following statements are true (T) or false (F).

                    a.  A collection is a container which groups multiple elements into a
                        single object.


                    b.  Collection decreases the performance of the program.



                                          Coding fact


                   Muhammad Hamza Shahzad is currently the youngest coder in the world. He
                   says: “I want to be Bill Gates.”






                 WHAT ARE ARRAYS?

                 Array is a collection of variables of similar data types. Consider an example of array (Arr)
                 which is a collection of variables of data type integer. Array only contains variables of similar
                 data types i.e. you cannot make an array which have variables of integer and string data

                 type in the same collection.

                 Books in a library, is an example of arrays where all the shelves have a common data type
                 (read – book) in it.

                                                                            Understanding Arrays & Collections   61
   58   59   60   61   62   63   64   65   66   67   68