Recall: d is a divisor of n  n % d == 0

Slides:



Advertisements
Similar presentations
Euclidean Algorithm Applied Symbolic Computation CS 567 Jeremy Johnson.
Advertisements

WS Algorithmentheorie 03 – Randomized Algorithms (Primality Testing) Prof. Dr. Th. Ottmann.
Thinking Mathematically
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.
Number Theory and the Real Number System
Thinking Mathematically
February 19, 2015Applied Discrete Mathematics Week 4: Number Theory 1 The Growth of Functions Question: If f(x) is O(x 2 ), is it also O(x 3 )? Yes. x.
Types of Number
Lecture 7 Sept 17 Goals: Complete Chapter 4 Chapters 5 and 6.
Structured programming
Lecture 7 Sept 29 Goals: Chapters 5 and 6. Scripts Sequence of instructions that we may want to run can be stored in a file (known as script). by typing.
6/20/2015 5:05 AMNumerical Algorithms1 x x1x
Lecture 7 Sept 29 Goals: Chapters 5 and 6. Chapter 5 - Exercises Exercise 5.2. Write a script swap.m that swaps the values of variables a and b. For example:
Introduction to Programming (in C++) Introduction Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
LECTURE 5 Learning Objectives  To apply division algorithm  To apply the Euclidean algorithm.
Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Chapter 2 The Fundamentals: Algorithms, the Integers, and Matrices
Programming Training Main Points: - Working with Functions in Python
Programming Training Main Points: - Lists / Arrays in Python. - Fundamental algorithms on Arrays.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Programming Training Main Points: - Python Statements - Problems with selections.
Prime numbers Jordi Cortadella Department of Computer Science.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Programming Training Main Points: - Problems with repetitions. - Discuss some important algorithms.
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
A step-by-step procedure for solving a problem in a finite number of steps.
Copyright © Zeph Grunschlag, Basic Number Theory Zeph Grunschlag.
5 While-Statements © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
3 Basics © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Chapter 2: Algorithm Analysis Application of Big-Oh to program analysis Logarithms in Running Time Lydia Sinapova, Simpson College Mark Allen Weiss: Data.
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.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Computer Science 320 A First Program in Parallel Java.
Recitation 8 Programming for Engineers in Python.
Programming Training kiddo Main Points: - Python Statements - Problems with selections.
Application: Algorithms Lecture 19 Section 3.8 Tue, Feb 20, 2007.
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.
Quiz1 Question  Add Binary Numbers a) b) c) d) e) none
Do Now Write as an exponent 3 x 3 x 3 x 3 4 x 4 x 4 x 4 x 4 5 x 5 x 5 What is a factor? Define in your own words.
Number Theory: Prime and Composite Numbers
Find LCM Least Common Multiple of 3 and 5: List the Multiples of each number, The multiples of 3 are 3, 6, 9, 12, 15, 18,... etc The multiples of 5 are.
Agenda Review:  Relation Properties Lecture Content:  Divisor and Prime Number  Binary, Octal, Hexadecimal Review & Exercise.
Divisibility and Primes
Main Points: - Python Statements - Problems with selections.
Warm Ups: 1) (4x3 + 2x2 + 1) – (x2 + x – 5)
Numerical Algorithms x x-1 Numerical Algorithms
GC211Data Structure Lecture2 Sara Alhajjam.
Number Theory and the Real Number System
MATH301- DISCRETE MATHEMATICS Copyright © Nahid Sultana Dr. Nahid Sultana Chapter 4: Number Theory and Cryptography.
Applied Discrete Mathematics Week 3: Algorithms
Numerical Algorithms x x-1
Number Theory (Chapter 7)
Number Theory and the Real Number System
Prime Factorisation Factor Trees
Coding Concepts (Sub- Programs)
Programming Training Main Points:
Python: Algorithms Damian Gordon.
Programming Training Main Points: - Working with Functions in Python
Programming Training Main Points: - Problems with repetitions.
Repetitious operations
Cube Root-Least number
Application: Algorithms
Application: Algorithms
National Central University, Taiwan
Least common multiple of three numbers
Square Least number Add & Subtract.
From the last time: gcd(a, b) can be characterized in two different ways: It is the least positive value of ax + by where x and y range over integers.
COMPUTING.
Unit 1: Number System Fluency
Lecture 6 - Recursion.
Presentation transcript:

Recall: d is a divisor of n  n % d == 0 last digit of n  last = n % 10 removing last digit  n = n // 10 adding digit to n  n = n * 10 + digit How to solve a problem? identify inputs and outputs find the calculations, selection and repetitions to solve it

A function is a piece of code introduced by def that: Python functions Python programs are made of a sequence of functions. These function can be called within the program or outside in the shell. A function is a piece of code introduced by def that: Has a name Has some arguments which are the inputs Returns some values which are the outputs Has some code to calculate the outputs from the inputs

The function head starts with def and finished with : def functionName(arg1, arg2, …): statem1 statem2 … return output # end functionName The function head starts with def and finished with : The function body calculates the output from the input args The output is returned. The function is called by its name output = functionName(a1,a2,..)

Function to calculate the min, max values of a and b. - inputs: a, b are arguments - output: min, max must returned - how to calculate? Use an if selection

def minMax(a, b) : ‘’’ function to calculate the max and min values of a and b. ‘’’ if a>b : max, min = a, b else: max, min = b, a # endif return min, max # end minMax

‘’’ find the max value for a,b,c,d.’’’ min1, max1 = minMax(a,b) Function to calculate the maximum value of 4 numbers a, b, c, d. def maximum4(a,b,c,d) : ‘’’ find the max value for a,b,c,d.’’’ min1, max1 = minMax(a,b) min2, max2 = minMax(c,d) min, max = minMax(max1, max2) return max # end maximum4

Functions are written once and used multiple times.

Prime numbers n is prime when: 1) it has only 2 divisors 1 and n 2) it has not any proper divisor in range of 2, 3, 4,…, sqrt(n)

Write a function to test if a number n is prime. - input: n - output: ans - how to do it? 1) test 2  ans = True 3) test even number  ans = False 4) repeat for a potential odd divisor 3,5,.., ? if d is a divisor then ans = False ans = true

def isPrime(n): ‘’’ function to test primality.’’’ if n == 1 : return False # endif if n == 2 : return True #endif for d in range(3, math.sqrt(n)+1;2) : if n % d == 0 : return False # endif #endfor return True

The Euclidian Algorithm Find the gcd of 2 numbers a, b. Solutions: Find prime decomposition and choose the common primes with the smallest exponents a = 2**4 * 3**2 * 5 and b = 2 * 5 **2 gcd = 2 ** 1 * 5 **1 Euclidian algorithm does consecutive divisions Divide a to b to find res and rem Change a = b and b = rem

Example a = 56 and b = 40 1) a = 56, b= 40  res = a // b = 1 rem = a % b = 16 2) a = 40, b = 16  res = a // b = 2 rem = a%b =8 3) a = 16, b = 8 res = a//b = 2 rem = a%b =0 4) a = 8, b = 0  repetition stops gcd = 8 the last remainder different than 0

The number of division in the algorithm is log (max(a,b)) hence just few divisions.