Additional Problems.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

AI Pathfinding Representing the Search Space
Problems and Their Classes
Marking Schema question1: 40 marks question2: 40 marks question3: 20 marks total: 100 marks.
MATH 224 – Discrete Mathematics
CSE 330: Numerical Methods
1 Chapter Six Algorithms. 2 Algorithms An algorithm is an abstract strategy for solving a problem and is often expressed in English A function is the.
WS Algorithmentheorie 03 – Randomized Algorithms (Primality Testing) Prof. Dr. Th. Ottmann.
Primality Testing Patrick Lee 12 July 2003 (updated on 13 July 2003)
CSC 2300 Data Structures & Algorithms March 16, 2007 Chapter 7. Sorting.
Chapter 1 – Basic Concepts
Efficiency of Algorithms
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
Elementary Number Theory and Methods of Proof. Basic Definitions An integer n is an even number if there exists an integer k such that n = 2k. An integer.
Hints for homework 6.2. divisors A divisor of an integer n, also called a factor of n, is an integer which evenly divides n without leaving a remainder.
Section Section Summary Recursive Algorithms Proving Recursive Algorithms Correct Recursion and Iteration (not yet included in overheads) Merge.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
TK3043 Analysis and Design of Algorithms Introduction to Algorithms.
Induction and recursion
Quadratic Residuosity and Two Distinct Prime Factor ZK Protocols By Stephen Hall.
Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
Number Theory.  A prime number is a natural number greater than 1 that has exactly two factors (or divisors), itself and 1.  Prime numbers less than.
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 4 (Part 3): Mathematical Reasoning, Induction.
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
CSCI 125 & 161 Lecture 13 Martin van Bommel. Floating Point Data Floating point numbers are not exact Value 0.1 in binary is very close to 1/10, but not.
Graph Algorithms Mathematical Structures for Computer Science Chapter 6 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraph Algorithms.
Approximate computations Jordi Cortadella Department of Computer Science.
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
The Integers. The Division Algorithms A high-school question: Compute 58/17. We can write 58 as 58 = 3 (17) + 7 This forms illustrates the answer: “3.
Genome Rearrangements Unoriented Blocks. Quick Review Looking at evolutionary change through reversals Find the shortest possible series of reversals.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
1 Flow of control Sequential Executing instructions one by one, in exact order given Selection Choosing to execute a particular set of statements depending.
More While Loop Examples CS303E: Elements of Computers and Programming.
CSC 211 Data Structures Lecture 13
Even more problems.. Mean (average) I need a program that calculates the average of student test scores. I need a program that calculates the average.
Recursive Algorithms &
NP-Complete Problems. Running Time v.s. Input Size Concern with problems whose complexity may be described by exponential functions. Tractable problems.
More on Correctness. Prime Factorization Problem: Write a program that computes all the prime factors of a given number Solution (Idea): Factors are less.
UNIT-I INTRODUCTION ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 1:
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
ALGORITHMS.
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
Slide Copyright © 2009 Pearson Education, Inc. Slide Copyright © 2009 Pearson Education, Inc. Chapter 1 Number Theory and the Real Number System.
CSE 330: Numerical Methods. What is true error? True error is the difference between the true value (also called the exact value) and the approximate.
Recursive Algorithms Section 5.4.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
More on Problem Solving
Probabilistic Algorithms
TK3043 Analysis and Design of Algorithms
Integer Programming An integer linear program (ILP) is defined exactly as a linear program except that values of variables in a feasible solution have.
Section 1.6 Factoring Trinomials
Chapter 1 Introduction Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Public Key Cryptosystems - RSA
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
See Through Fog Imaging Project: P06441
Chapter 8 Search and Sort
Discrete Math for CS CMPSC 360 LECTURE 12 Last time: Stable matching
Algorithm Discovery and Design
NP-Complete Problems.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
P.O.D. #38 Using the formula: a² + b² = c²
Discrete Mathematics CS 2610
Chapter 1 Introduction Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Additional Problems

Smallest Divisor of an integer Problem: Given an integer m, write a program that will find its smallest exact divisor other than 1. Is the problem clear and well-defined? what is a divisor? For any number m, there are divisors There are at least two divisors All divisors of m can be totally ordered Can m be 0? Can it be negative? What if m is prime? Assume: m > 0 Assert: if m is prime, return 1 else the smallest divisor other than 1

Towards a strategy All divisors of m are between 1 and m Strategy 1: Enumerate all divisors of m choose the smallest one Better Strategy: Starting from 2, check one by one whether it is a divisor of m If one is found before m is reached then return. Otherwise, return 1. How to check a divisor? Can we do better?

Towards a Better Strategy The above inspects (m-1) numbers, in the worst case for even numbers, the answer is 2 for odd numbers, no need to check an even number How to check whether a number is even or not? Do we have go up to m?

The Algorithm Algorithm st_divisor Input m Assume: m > 0 Output d Assert: if m is prime, d is 1; otherwise, d is the smallest divisor other than 1 1. if ( even(m)) then d = 2 else 1.1 d = 3 1.2 do while ((m mod d /= 0 ) and (d < sqrt(m))) 1.2.1 d = d + 2 2. if (m mod d /= 0) then d = 1 end algorithm

Observation How to compute Square roots? Better to remove the square root computation before the loop Look for removing computations inside loops Note m mod d can not be removed Is the algorithm correct? Does it terminate?

Loop invariant The invariant condition is: Bound Function: (m – d) m is odd, d is odd, 1 < d <= sqroot(m) + 1 for no x: 1 < x < d: x is a divisor of m Bound Function: (m – d)

Improvements How many iterations? Is there a better algorithm? In the worst case, the largest integer less than or equal to sqroot(m)/2 0, if m is even Is there a better algorithm? A lot of redundant checks are still made Look for prime divisors

The square root problem Problem: Write a program that computes the square root of a given number. Is the problem definition clear? If 25 is the input, then 5 is the output If 81 is the input, then 9 is the output If 42 is the input, then ? For non perfect squares, the square root is a real number So the output should be close to the real square root How close? to a given accuracy

A more precise specification Problem: Write a program that given a number m outputs a real value r s.t. r*r differs from m by a given accuracy value e More precisely, the program outputs r s.t. |r*r - m| < e

Solution Strategy ... Guess and Correct Strategy: Choose an initial guess r less than m If r*r > m then keep decreasing r by 1 until r*r is less than or equal to m. If r*r < m then keep increasing r by 0.1, … until r*r exceeds or equals m If r*r > m then decrease r by 0.01 until r*r exceeds or equals m. ... Terminate the computation when r*r equals m or differs from m by a given small number.

Idea Number of iteration depends upon the initial guess If m is 10,00,000 and the initial guess is 300 then over 700 steps are needed Can we have a better strategy?

Towards a better strategy The basic idea of the strategy is to obtain a series of guesses that falls on either side of the actual value narrows down closer and closer To make the guess fall on either side increase/decrease the guess systematically To narrow the guess the amount of increase/decrease is reduced Improving the strategy faster ways of obtaining new guess from the old one

One Strategy Given a guess a for square root of m m/a falls on the opposite side (a + m/a)/2, can be the next guess This gives rise to the following solution start with an arbitrary guess, r_0 generate new guesses r_1, r_2, etc by using the averaging formula. When to terminate? when the successive guesses differ by a given small number

The algorithm Algorithm Sq_root Input m, e: real assume: m>0, 1 > e > 0 Output r1, r2: real assert:: |(r2 * r2 - m) | <= |(r1 * r1 - m)|, |r1 - r2| < e    1. r1 = m/2 2. r2 = r1   3. Do while (|r1 - r2| > e) steps 3.1 and 3.2 3.1 r1 = r2 3.2 r2 = (r1+m/r1)/2 end Algorithm

Analysis of the algorithm Is it correct? Find the loop invariant and bound function Can the algorithm be improved? More general techniques available Numerical analysis