1 / 17

Mathematical

Mathematical. Approach. Many of these problems read as brain teasers at first, but can be worked through in a logical way. Just remember to rely on the rules of mathematics to develop an approach, and then to carefully translate that idea into code. Example.

saber
Download Presentation

Mathematical

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Mathematical

  2. Approach • Many of these problems read as brain teasers at first, but can be worked through in a logical way. • Just remember to rely on the rules of mathematics to develop an approach, and then to carefully translate that idea into code.

  3. Example • Given two numbers m and n, write a method to return the first number r that is divisible by both (e.g., the least common multiple).

  4. hints • What does it mean for r to be divisible by m and n? • It means that all the primes in m must go into r, and all primes in n must be in r. • What if m and n have primes in common? • For example, if m is divisible by 3^5 and n is divisible by 3^7, what does this mean about r? • It means r must be divisible by 3^7. • The Rule • For each prime p such that p^a \ m (e.g., m is divisible by p^a) and p^b \ n, r must be divisible by p^max(a, b).

  5. Find the LCM of these sets of numbers. • 3, 9, 21Solution: List the prime factors of each. 3: 3 9: 3 × 3 21: 3 × 763 can be divided evenly by 3, 9, and 21. • 12, 80 Solution: List the prime factors of each.12: 2 × 2 × 3 80: 2 × 2 × 2 × 2 × 5 = 80 240 can be divided by both 12 and 80.

  6. Algorithm

  7. Prime • A number is prime if it is only divisible by 1 and itself. So for example 2, 3, 5, 79, 311 and 1931 are all prime, while 21 is not prime because it is divisible by 3 and 7. • To find if a number n is prime we could simply check if it divides any numbers below it. • We can use the modulus (%) operator to check for divisibility:

  8. Solution • for (inti=2; i<n; i++) • if (n%i==0) • return false; • return true; • We can make this code run faster by noticing that we only need to check divisibility for values of i that are less or equal to the square root of n 

  9. Implementation • public booleanisPrime (int n) { • if (n<=1) • return false; • if (n==2) • return true; • if (n%2==0) • return false; • int m=Math.sqrt(n); • for (inti=3; i<=m; i+=2) • if (n%i==0) • return false; • return true; • }

  10. Problem • Design an algorithm to find the kth number such that the only prime factors are 3, 5, and 7.

  11. Hints

  12. Hints • 3 * (previous number in list) • 5 * (previous number in list) • 7 * (previous number in list) • How would we find the next number in the list? • Well, we could multiply 3, 5 and 7 times each number in the list and find the smallest element that has not yet been added to our list. • This solution is O(n^2). • Not bad, but I think we can do better

  13. Hints Red: duplications

  14. Hints • In our current algorithm, we’re doing 3*1, 3*3, 3*5, 3*7, 3*9, 3*15, 3*21, 3*25 …, and the same for 5 and 7.We’ve already done almost all this work before—why are we doing it again? • We can fix this by multiplying each number we add to our list by 3, 5, 7 and putting the results in one of the three first-in-first-out queues. • To look for the next “magic” number, we pick the smallest element in the three queues.

  15. Solution

More Related