CSCI 125 & 161 Lecture 12 Martin van Bommel. Prime Numbers Prime number is one whose only divisors are the number 1 and itself Therefore, number is prime.

Slides:



Advertisements
Similar presentations
Control Flow Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Advertisements

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.
Chapter 5 Number Theory © 2008 Pearson Addison-Wesley. All rights reserved.
4-2 6 th grade math Prime Factorization. Objective To use divisibility rules to check for divisibility and write the prime factorization of numbers in.
Decimals and Fractions
5.1 Number Theory. The study of numbers and their properties. The numbers we use to count are called the Natural Numbers or Counting Numbers.
Prime Factorization No, not this kind of prime..
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.
ACM Workshop Number Theory.
Using factors to find the prime factorization of a number
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
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.
Unit 171 Algorithms and Problem Solving  Introduction  Algorithm Design  Algorithm Properties  Algorithm Control Flow  Examples  Comparing Algorithms.
Unit 171 Algorithms and Problem Solving - II Algorithm Efficiency Primality Testing Improved Primality Testing Sieve of Eratosthenes Primality Testing.
CSE 321 Discrete Structures Winter 2008 Lecture 8 Number Theory: Modular Arithmetic.
More on Recursion Averting Program Crashes CSC 1401: Introduction to Programming with Java Week 9 – Lecture 3 Wanda M. Kunkle.
TK3043 Analysis and Design of Algorithms Introduction to Algorithms.
CS1101: Programming Methodology Aaron Tan.
{8, 16, 24, 32, …} are the multiples of 8 Common Multiples
Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Divisibility October 8, Divisibility If a and b are integers and a  0, then the statement that a divides b means that there is an integer c such.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
MATH 224 – Discrete Mathematics
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Algorithmic Problem Solving CMSC 201 Adapted from slides by Marie desJardins (Spring 2015 Prof Chang version)
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Prime numbers Jordi Cortadella Department of Computer Science.
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.
Factors
Multiples, Factors, Primes & Composite Numbers
Data Structures Chapter 1- Introduction Mohamed Mustaq.A.
CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n,
WIKIPEDIA HAS MANY MORE DIVISIBILITY RULES. EXAMPLE Since 52=13(4) is divisible by 4, is divisible by 4 Since 452=56(8)+4 is not divisible.
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Repeated Subtraction: Division
Greatest Common Divisor Jordi Cortadella Department of Computer Science.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Mar 3, 2006.
Copyright © 2009 Pearson Education, Inc. Chapter 5 Section 1 - Slide 1 Chapter 1 Number Theory and the Real Number System.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Review of Recursion For those of you that have slept since.
Prime Factorization SWBAT find the prime factorization of a composite number.
CSE 311: Foundations of Computing Fall 2014 Lecture 12: Primes, GCD.
More on Efficiency Issues. Greatest Common Divisor given two numbers n and m find their greatest common divisor possible approach –find the common primes.
UNIT-I INTRODUCTION ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 1:
Factors Prime Factors Multiples Common Factors and Highest Common Factor (HCF) Factors and Multiples Common Multiples and Lowest Common Multiple (LCM)
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
Application: Algorithms Lecture 20 Section 3.8 Wed, Feb 21, 2007.
Chapter 2: Algorithm Analysis Application of Big-Oh to program analysis Logarithms in Running Time Lydia Sinapova, Simpson College Mark Allen Weiss: Data.
AF2. Turn off your phones Primes, gcd, some examples, reading.
Prime Numbers Damian Gordon. Prime Numbers So let’s say we want to express the following algorithm: – Read in a number and check if it’s a prime number.
MTH 231 Section 4.1 Divisibility of Natural Numbers.
Slide Copyright © 2009 Pearson Education, Inc. 5.1 Number Theory.
Part I: Numbers and Operations Lesson 1: Numbers.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Number Theory: Prime and Composite Numbers
Slide Copyright © 2009 Pearson Education, Inc. Slide Copyright © 2009 Pearson Education, Inc. Chapter 1 Number Theory and the Real Number System.
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett.
Factors
Introduction to C++ Programming Language
Greatest Common Divisor
Greatest Common Divisor
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Discrete Math for CS CMPSC 360 LECTURE 12 Last time: Stable matching
Topic Past Papers –Proofs
Copyright © Zeph Grunschlag,
Functions and Recursion
while loop Condition Statement list
Presentation transcript:

CSCI 125 & 161 Lecture 12 Martin van Bommel

Prime Numbers Prime number is one whose only divisors are the number 1 and itself Therefore, number is prime if it has two positive divisors One way to test for prime is to count its divisors

Prime- First Try bool IsPrime(int n) { int i, divisors = 0; for (i=1; i<=n; i++) { if (n % i == 0) divisors++; } return (divisors == 2); }

Prime - Second Thought Number is not prime if it has divisor other than 1 and itself If number not divisible by 2, will not be divisible by any even number Check for two, then only check odds Only have to check up to square root of n

Prime - Second Try bool IsPrime(int n) { int i, limit; if (n == 2) return true; if (n % 2 == 0) return false; limit = sqrt(n) + 1; for (i = 3; i <= limit; i += 2) if (n % i == 0) return false; return true; }

Efficiency Trade-off Recall implementations of IsPrime Final version more efficient Original is more readable and easier to prove correct Principal concern must be correctness Secondary factors are efficiency, clarity, and maintainability No “best” algorithm from all perspectives

GCD Greatest Common Divisor of two numbers –largest number that divides evenly into both Function to determine GCD of two values int GCD(int x, int y); e.g. –GCD(49, 35) = 7 –GCD(6, 18) = 6 –GCD(32, 33) = 1

Brute Force GCD int GCD(int x, int y) { int g = x; while (x % g != 0 || y % g != 0) { g--; } return g; }

Improved GCD int GCD(int x, int y) { int g; if (x < y) g = x; else g = y; while (x % g != 0 || y % g != 0) { g--; } return g; }

Problems with Brute Force Poor choice for efficiency –e.g. GCD(10005, 10000) = 5 Long running loop to find simple answer Can’t count up! Why? Other choices?

Euclid’s Algorithm for GCD 1. Divide x by y; call remainder r 2. If r is zero, answer is y. 3. If r is not zero, set x equal to old value of y, set y equal to r, repeat entire process Difficult to prove correct

Euclid’s GCD int GCD(int x, int y) { int r = x % y; while (r != 0) { x = y; y = r; r = x % y; } return y; }