SlideShare ist ein Scribd-Unternehmen logo
1 von 8
Downloaden Sie, um offline zu lesen
ACMACMACMACMACMACMACMA
CMACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
                       ACM Training Session

MACMACMACMACMACMACMAC   BRAC UNIVERSITY



MACMACMACMACMACMACMAC
                             8/7/2012

                             Mahbub


MACMACMACMACMACMACMAC
     This work is licensed under a Creative Commons Attribution-

MACMACMACMACMACMACMAC
          NonCommercial-ShareAlike 3.0 Unported License.




MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
Page 1 of 7


                           CHECK A NUMBER IS EVEN OR ODD MORE EFFICIENTLY



We all know if a number is divisible by two that is an even number else odd. But in computer
divide is not an easy task, it comes up with a cost.



We know, computer works with bits so if we can convert this odd, even check using bits that
will be faster than our common method. Look carefully the following numbers.




                       Decimal                    Binary
                 Representations           Representations
                            0               0        0        0
                            1               0        0        1
                            2               0        1        0
                            3               0        1        1
                            4               1        0        0
                            5               1        0        1
                            6               1        1        0
                            7               1        1        1



Look how the last bit is changed, for every odd number the last bit is always one and for even
its zero. Using this pattern we can check if a number is odd or even. Here we will use bitwise
operator & (and), we know when both bit is one the answer is one else zero. So if we and
with one with an odd number the answer will be always one else zero if the number is even.

Java implementation of this method is,



// this method returns true if odd
public static boolean isOdd(int i) {

    return (i & 1) == 1;

}




S. Mahbub – Uz – Zaman (09301004)
Page 2 of 7


Binary representation of 6 (even) is 110 and 1 is 001


6 & 1 = 110 & 001 = 000


Binary representation of 7 (odd) and 1 is 001


7 & 1 = 111 & 001 = 001




S. Mahbub – Uz – Zaman (09301004)
Page 3 of 7


                    HOW TO CHECK EFFICIENTLY IF A NUMBER IS PRIME OR NOT



A number is a prime number if that number has precisely two distinct divisors, one and itself.
First ten prime numbers are



2, 3, 5, 7, 11, 13, 17, 19, 23, 29



So, if we can find that N has two divisors than it’s a prime number else not, this is actually
brute force approach and the complexity is O (N). How we do that, starting from 1 to N we
have check if N is divisible by 1, 2, 3, ….., N each time it divide we increment our divisor
count by one and at the end we will check if the divisor count is 2 or not.



Can we do better, yes we can. Look carefully the only even prime is 2. If we add an if
condition that if the number is 2 return true else false if the number is even, because other
even numbers can’t not be a prime number. For even numbers the complexity becomes O (1).
So what about odd numbers? How can we improve that? We can reduce the complexity for
odd number O (N / 2). See we don’t need to divide by even numbers because the Number N
is an odd number, so it will never be divide by an even number. So we have to check if N is
divisible by 1, 3, 5, 7, 9, 11, 13, 15 …. N.



We never satisfied! We need more yes the ultimate complexity of an odd number to check
whether it’s prime or not is O (√ ).

For finding if the number has any divisors other then 1 and itself it will appear under the
square root of N, we don’t need to check up to N.




S. Mahbub – Uz – Zaman (09301004)
Page 4 of 7


Java implementation of this method is,



public static boolean IsPrime(long num) {



       if(num < 2)

            return false;



      if(num == 2)
             return true;



       if( (num & 1) == 0)       // the number is even

            return false;



       long sqrt = (int) Math.sqrt(num);



       for(int i = 3; i <= sqrt; i += 2) {

            if(num % i == 0)

               return false;

       }

    return true;

}




S. Mahbub – Uz – Zaman (09301004)
Page 5 of 7


                                                                   Fibonacci number



Leonardo Pisano Bigollo introduced the Fibonacci sequence. He was an Italian
Mathematician.



0, 1, 1, 2, 3, 5, 8, 13, 21



Each number is the sum of the previous two numbers.

Mathematical representation of Fibonacci sequence is



Fn = Fn-1 + Fn-2, where F0 = 0 and F1 = 1



We can Generate Fibonacci numbers using Dynamic Programming in O (n) and also find a
particular Fibonacci number in O (log n).




S. Mahbub – Uz – Zaman (09301004)
Page 6 of 7


                                                                                Linear Searching



In our daily life we often search thing like books, movie etc. Similarly in computer science
searching is an important area. There are several search technique some are easy to
understand today I will discuss an easy search technique called linear search which
complexity is O (n).



Suppose we have an array of String which contains name of 5 chocolates now we want to do
some search.



         Index                0             1               2            3                4

 Name of chocolates           a             b               c            d                e



So in linear search we start form the first or last and do a check that the current element is our
searched item or not, if we found it then return true or else move to next element until we
have searched the whole array. That’s why the complexity is O (n) because in worst case the
searched item can be in the last position of the array or can be not present.



Case 1: Search a. Found it; a is in the index 0 position.

Case 2: Search g. Not found it. (Worst case)

Case 3: Search e. found it; e is in the index 4 position. (Worst case)



But in real life the input data is not too small always. For 1000000 data linear search will be
too slow, yes there are other search techniques which are faster than linear search.




S. Mahbub – Uz – Zaman (09301004)
Page 7 of 7


                                                                        Perfect Square Number



Perfect square number is an integer that is the square of an integer. For example we can say
that 25 is a perfect square number, since it is a square of 5.

                                         5 * 5 = 25

We can also say that the square root of a perfect square number is also a integer.

                                           √    =6



We can check if a number is perfect square or not. First we get the square root of the number,
then we multiply the result by itself, if it is a squre number then it shuld macth to original
number.

                                           √    =6

               6 * 6 = 36 [36 is a perfect square number]

                     √    = 5.9160 (cast it into long) = 5

                         5 * 5        35 [whether 35 is not]



Java implementation of this method is,

boolean isPSN(long num) {

    if (num < 0)
      return false;

    long sqr = (long) Math.sqrt(num);
    return sqr * sqr == num;
}




S. Mahbub – Uz – Zaman (09301004)

Weitere ähnliche Inhalte

Was ist angesagt?

Kakuro: Solving the Constraint Satisfaction Problem
Kakuro: Solving the Constraint Satisfaction ProblemKakuro: Solving the Constraint Satisfaction Problem
Kakuro: Solving the Constraint Satisfaction ProblemVarad Meru
 
4 10 Notes B
4 10 Notes B4 10 Notes B
4 10 Notes Bmbetzel
 
Boolean algebra.pptx
Boolean algebra.pptxBoolean algebra.pptx
Boolean algebra.pptxMhhh7
 
Algebra factoring
Algebra factoringAlgebra factoring
Algebra factoringTrabahoLang
 
11 applications of factoring
11 applications of factoring11 applications of factoring
11 applications of factoringelem-alg-sample
 
Distributive Property (Algebra 1)
Distributive Property (Algebra 1)Distributive Property (Algebra 1)
Distributive Property (Algebra 1)rfant
 
Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1Techglyphs
 
Sequence and series 02
Sequence and series 02Sequence and series 02
Sequence and series 02manrak
 
Solving Inequalities (Algebra 2)
Solving Inequalities (Algebra 2)Solving Inequalities (Algebra 2)
Solving Inequalities (Algebra 2)rfant
 

Was ist angesagt? (17)

factoring
factoringfactoring
factoring
 
Kakuro: Solving the Constraint Satisfaction Problem
Kakuro: Solving the Constraint Satisfaction ProblemKakuro: Solving the Constraint Satisfaction Problem
Kakuro: Solving the Constraint Satisfaction Problem
 
4 10 Notes B
4 10 Notes B4 10 Notes B
4 10 Notes B
 
Boolean algebra.pptx
Boolean algebra.pptxBoolean algebra.pptx
Boolean algebra.pptx
 
Calculus Assignment Help
Calculus Assignment HelpCalculus Assignment Help
Calculus Assignment Help
 
Apti book for gate
Apti book for gateApti book for gate
Apti book for gate
 
Algebra factoring
Algebra factoringAlgebra factoring
Algebra factoring
 
11 applications of factoring
11 applications of factoring11 applications of factoring
11 applications of factoring
 
Distributive Property (Algebra 1)
Distributive Property (Algebra 1)Distributive Property (Algebra 1)
Distributive Property (Algebra 1)
 
Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1
 
Lemh105
Lemh105Lemh105
Lemh105
 
Mp neuron
Mp neuronMp neuron
Mp neuron
 
Sequence and series 02
Sequence and series 02Sequence and series 02
Sequence and series 02
 
1 1 number theory
1 1 number theory1 1 number theory
1 1 number theory
 
Ch08
Ch08Ch08
Ch08
 
Solving Inequalities (Algebra 2)
Solving Inequalities (Algebra 2)Solving Inequalities (Algebra 2)
Solving Inequalities (Algebra 2)
 
Rsa example
Rsa exampleRsa example
Rsa example
 

Andere mochten auch

Android SMS (GDayX, Dhaka, Bangladesh)
Android SMS (GDayX, Dhaka, Bangladesh)Android SMS (GDayX, Dhaka, Bangladesh)
Android SMS (GDayX, Dhaka, Bangladesh)Lifeparticle
 
WorkShop on Arduino
WorkShop on Arduino WorkShop on Arduino
WorkShop on Arduino Lifeparticle
 
Android security and parsing 101
Android security  and parsing  101Android security  and parsing  101
Android security and parsing 101Lifeparticle
 
Tech Talk: App Functionality (Android)
Tech Talk: App Functionality (Android)Tech Talk: App Functionality (Android)
Tech Talk: App Functionality (Android)Lifeparticle
 

Andere mochten auch (8)

Android SMS (GDayX, Dhaka, Bangladesh)
Android SMS (GDayX, Dhaka, Bangladesh)Android SMS (GDayX, Dhaka, Bangladesh)
Android SMS (GDayX, Dhaka, Bangladesh)
 
WorkShop on Arduino
WorkShop on Arduino WorkShop on Arduino
WorkShop on Arduino
 
Buacm 2
Buacm 2Buacm 2
Buacm 2
 
Command line
Command lineCommand line
Command line
 
Workshop on python
Workshop on pythonWorkshop on python
Workshop on python
 
Android security and parsing 101
Android security  and parsing  101Android security  and parsing  101
Android security and parsing 101
 
All about android
All about androidAll about android
All about android
 
Tech Talk: App Functionality (Android)
Tech Talk: App Functionality (Android)Tech Talk: App Functionality (Android)
Tech Talk: App Functionality (Android)
 

Ähnlich wie Buacm 3

Counting (Notes)
Counting (Notes)Counting (Notes)
Counting (Notes)roshmat
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to AlgorithmsMohamed Essam
 
ix-number system-ppt(2).pptx
ix-number system-ppt(2).pptxix-number system-ppt(2).pptx
ix-number system-ppt(2).pptxRajkumarknms
 
Mathematical Statistics Assignment Help
Mathematical Statistics Assignment HelpMathematical Statistics Assignment Help
Mathematical Statistics Assignment HelpExcel Homework Help
 
Introduction to Prime Numbers
Introduction to Prime NumbersIntroduction to Prime Numbers
Introduction to Prime NumbersLuke Dunn
 
Alg complex numbers
Alg complex numbersAlg complex numbers
Alg complex numbersTrabahoLang
 
Tenth class state syllabus-text book-em-ap-ts-mathematics
Tenth class state syllabus-text book-em-ap-ts-mathematicsTenth class state syllabus-text book-em-ap-ts-mathematics
Tenth class state syllabus-text book-em-ap-ts-mathematicsNaukriTuts
 
The Complexity Of Primality Testing
The Complexity Of Primality TestingThe Complexity Of Primality Testing
The Complexity Of Primality TestingMohammad Elsheikh
 
Number systems / Algebra
Number systems / Algebra Number systems / Algebra
Number systems / Algebra indianeducation
 
Pseudorandom number generators powerpoint
Pseudorandom number generators powerpointPseudorandom number generators powerpoint
Pseudorandom number generators powerpointDavid Roodman
 
Introduction to Dynamic Programming.pptx
Introduction to Dynamic Programming.pptxIntroduction to Dynamic Programming.pptx
Introduction to Dynamic Programming.pptxPochupouOwo
 
Insider mathematical
Insider   mathematicalInsider   mathematical
Insider mathematicalAditi Saxena
 
CPAP.com Introduction to Coding: Part 1
CPAP.com Introduction to Coding: Part 1CPAP.com Introduction to Coding: Part 1
CPAP.com Introduction to Coding: Part 1johnnygoodman
 

Ähnlich wie Buacm 3 (20)

The Attractor
The AttractorThe Attractor
The Attractor
 
Analysis.pptx
Analysis.pptxAnalysis.pptx
Analysis.pptx
 
Counting (Notes)
Counting (Notes)Counting (Notes)
Counting (Notes)
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
ix-number system-ppt(2).pptx
ix-number system-ppt(2).pptxix-number system-ppt(2).pptx
ix-number system-ppt(2).pptx
 
Mathematical Statistics Assignment Help
Mathematical Statistics Assignment HelpMathematical Statistics Assignment Help
Mathematical Statistics Assignment Help
 
Introduction to Prime Numbers
Introduction to Prime NumbersIntroduction to Prime Numbers
Introduction to Prime Numbers
 
Python Math Concepts Book
Python Math Concepts BookPython Math Concepts Book
Python Math Concepts Book
 
Alg complex numbers
Alg complex numbersAlg complex numbers
Alg complex numbers
 
RSA
RSARSA
RSA
 
Tenth class state syllabus-text book-em-ap-ts-mathematics
Tenth class state syllabus-text book-em-ap-ts-mathematicsTenth class state syllabus-text book-em-ap-ts-mathematics
Tenth class state syllabus-text book-em-ap-ts-mathematics
 
The Complexity Of Primality Testing
The Complexity Of Primality TestingThe Complexity Of Primality Testing
The Complexity Of Primality Testing
 
Math Chapter 1 - Integers
Math Chapter 1 - IntegersMath Chapter 1 - Integers
Math Chapter 1 - Integers
 
Number systems / Algebra
Number systems / Algebra Number systems / Algebra
Number systems / Algebra
 
Pseudorandom number generators powerpoint
Pseudorandom number generators powerpointPseudorandom number generators powerpoint
Pseudorandom number generators powerpoint
 
Introduction to Dynamic Programming.pptx
Introduction to Dynamic Programming.pptxIntroduction to Dynamic Programming.pptx
Introduction to Dynamic Programming.pptx
 
Probability module 1
Probability module 1Probability module 1
Probability module 1
 
Insider mathematical
Insider   mathematicalInsider   mathematical
Insider mathematical
 
CPAP.com Introduction to Coding: Part 1
CPAP.com Introduction to Coding: Part 1CPAP.com Introduction to Coding: Part 1
CPAP.com Introduction to Coding: Part 1
 
Mathematical Statistics Assignment Help
Mathematical Statistics Assignment HelpMathematical Statistics Assignment Help
Mathematical Statistics Assignment Help
 

Buacm 3

  • 1. ACMACMACMACMACMACMACMA CMACMACMACMACMACMACMAC MACMACMACMACMACMACMAC ACM Training Session MACMACMACMACMACMACMAC BRAC UNIVERSITY MACMACMACMACMACMACMAC 8/7/2012 Mahbub MACMACMACMACMACMACMAC This work is licensed under a Creative Commons Attribution- MACMACMACMACMACMACMAC NonCommercial-ShareAlike 3.0 Unported License. MACMACMACMACMACMACMAC MACMACMACMACMACMACMAC MACMACMACMACMACMACMAC MACMACMACMACMACMACMAC MACMACMACMACMACMACMAC MACMACMACMACMACMACMAC MACMACMACMACMACMACMAC MACMACMACMACMACMACMAC MACMACMACMACMACMACMAC
  • 2. Page 1 of 7 CHECK A NUMBER IS EVEN OR ODD MORE EFFICIENTLY We all know if a number is divisible by two that is an even number else odd. But in computer divide is not an easy task, it comes up with a cost. We know, computer works with bits so if we can convert this odd, even check using bits that will be faster than our common method. Look carefully the following numbers. Decimal Binary Representations Representations 0 0 0 0 1 0 0 1 2 0 1 0 3 0 1 1 4 1 0 0 5 1 0 1 6 1 1 0 7 1 1 1 Look how the last bit is changed, for every odd number the last bit is always one and for even its zero. Using this pattern we can check if a number is odd or even. Here we will use bitwise operator & (and), we know when both bit is one the answer is one else zero. So if we and with one with an odd number the answer will be always one else zero if the number is even. Java implementation of this method is, // this method returns true if odd public static boolean isOdd(int i) { return (i & 1) == 1; } S. Mahbub – Uz – Zaman (09301004)
  • 3. Page 2 of 7 Binary representation of 6 (even) is 110 and 1 is 001 6 & 1 = 110 & 001 = 000 Binary representation of 7 (odd) and 1 is 001 7 & 1 = 111 & 001 = 001 S. Mahbub – Uz – Zaman (09301004)
  • 4. Page 3 of 7 HOW TO CHECK EFFICIENTLY IF A NUMBER IS PRIME OR NOT A number is a prime number if that number has precisely two distinct divisors, one and itself. First ten prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 So, if we can find that N has two divisors than it’s a prime number else not, this is actually brute force approach and the complexity is O (N). How we do that, starting from 1 to N we have check if N is divisible by 1, 2, 3, ….., N each time it divide we increment our divisor count by one and at the end we will check if the divisor count is 2 or not. Can we do better, yes we can. Look carefully the only even prime is 2. If we add an if condition that if the number is 2 return true else false if the number is even, because other even numbers can’t not be a prime number. For even numbers the complexity becomes O (1). So what about odd numbers? How can we improve that? We can reduce the complexity for odd number O (N / 2). See we don’t need to divide by even numbers because the Number N is an odd number, so it will never be divide by an even number. So we have to check if N is divisible by 1, 3, 5, 7, 9, 11, 13, 15 …. N. We never satisfied! We need more yes the ultimate complexity of an odd number to check whether it’s prime or not is O (√ ). For finding if the number has any divisors other then 1 and itself it will appear under the square root of N, we don’t need to check up to N. S. Mahbub – Uz – Zaman (09301004)
  • 5. Page 4 of 7 Java implementation of this method is, public static boolean IsPrime(long num) { if(num < 2) return false; if(num == 2) return true; if( (num & 1) == 0) // the number is even return false; long sqrt = (int) Math.sqrt(num); for(int i = 3; i <= sqrt; i += 2) { if(num % i == 0) return false; } return true; } S. Mahbub – Uz – Zaman (09301004)
  • 6. Page 5 of 7 Fibonacci number Leonardo Pisano Bigollo introduced the Fibonacci sequence. He was an Italian Mathematician. 0, 1, 1, 2, 3, 5, 8, 13, 21 Each number is the sum of the previous two numbers. Mathematical representation of Fibonacci sequence is Fn = Fn-1 + Fn-2, where F0 = 0 and F1 = 1 We can Generate Fibonacci numbers using Dynamic Programming in O (n) and also find a particular Fibonacci number in O (log n). S. Mahbub – Uz – Zaman (09301004)
  • 7. Page 6 of 7 Linear Searching In our daily life we often search thing like books, movie etc. Similarly in computer science searching is an important area. There are several search technique some are easy to understand today I will discuss an easy search technique called linear search which complexity is O (n). Suppose we have an array of String which contains name of 5 chocolates now we want to do some search. Index 0 1 2 3 4 Name of chocolates a b c d e So in linear search we start form the first or last and do a check that the current element is our searched item or not, if we found it then return true or else move to next element until we have searched the whole array. That’s why the complexity is O (n) because in worst case the searched item can be in the last position of the array or can be not present. Case 1: Search a. Found it; a is in the index 0 position. Case 2: Search g. Not found it. (Worst case) Case 3: Search e. found it; e is in the index 4 position. (Worst case) But in real life the input data is not too small always. For 1000000 data linear search will be too slow, yes there are other search techniques which are faster than linear search. S. Mahbub – Uz – Zaman (09301004)
  • 8. Page 7 of 7 Perfect Square Number Perfect square number is an integer that is the square of an integer. For example we can say that 25 is a perfect square number, since it is a square of 5. 5 * 5 = 25 We can also say that the square root of a perfect square number is also a integer. √ =6 We can check if a number is perfect square or not. First we get the square root of the number, then we multiply the result by itself, if it is a squre number then it shuld macth to original number. √ =6 6 * 6 = 36 [36 is a perfect square number] √ = 5.9160 (cast it into long) = 5 5 * 5 35 [whether 35 is not] Java implementation of this method is, boolean isPSN(long num) { if (num < 0) return false; long sqr = (long) Math.sqrt(num); return sqr * sqr == num; } S. Mahbub – Uz – Zaman (09301004)