Python: Algorithms Damian Gordon.

Slides:



Advertisements
Similar presentations
Prime Numbers and Prime Factorization
Advertisements

Prime Numbers and Prime Factorization
Prime Numbers and Prime Factorization. Factors Factors are the numbers you multiply together to get a product. For example, the product 24 has several.
Factors, Divisibility, & Prime / Composite numbers
Tutorial #6 PseudoCode (reprise)
A number is divisible by another number if the quotient is a whole number with no remainder.
Divisibility Rules Page 10 in textbook.
Flow Charting, Structured English and PseudoCode
Tutorial #7 Flowcharts (reprise) Program Design. Introduction We mentioned it already, that if we thing of an analyst as being analogous to an architect,
Flow Charting Damian Gordon. Introduction We mentioned it already, that if we thing of an analyst as being analogous to an architect, and a developer.
division algorithm Before we study divisibility, we must remember the division algorithm. r dividend = (divisor ⋅ quotient) + remainder.
A number that divides evenly into a larger number without a remainder Factor- Lesson 7 - Attachment A.
Python: Modularisation Damian Gordon. Modularisation Remember the prime checker program:
SWBAT to use divisibility rules for 2, 3, 4, 5, 6, 8, 9, and 10
Prime Numbers and Prime Factorization. Factors Factors are the numbers you multiply together to get a product. For example, the product 24 has several.
Fibonacci Numbers Damian Gordon. Fibonacci Numbers.
Iteration: WHILE Loop Damian Gordon. WHILE Loop Consider the problem of searching for an entry in a phone book with only SELECTION:
Searching Damian Gordon. Google PageRank Damian Gordon.
Python: Arrays Damian Gordon. Arrays In Python arrays are sometimes called “lists” or “tuple” but we’ll stick to the more commonly used term “array”.
Modularisation Damian Gordon. Modularisation Let’s imagine we had code as follows:
Prime Numbers and Prime Factorization. Factors Factors are the numbers you multiply together to get a product. For example, the product 24 has several.
Introduction to Python Damian Gordon. The Python Programming Language Python was developed by Guido van Rossum in the Netherlands in Van Rossum.
Prime Numbers & Prime Factorization. Factors Factors are the numbers you multiply together to get a product. For example, the product 24 has several factors.
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.
Python: Selection Damian Gordon. Python: Selection We’ll consider two ways to do selection: The IF statement The CASE statement.
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.
Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version of the prime number checking program:
Prime and Composite Numbers A prime number is a natural number greater than 1 whose only factors are 1 and itself. The first few prime numbers are 2,
Recursion Damian Gordon. Recursion Recursion: Factorial Recursion is a feature of modularisation, when a module can call itself to complete a solution.
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.
Python: Iteration Damian Gordon. Python: Iteration We’ll consider four ways to do iteration: – The WHILE loop – The FOR loop – The DO loop – The LOOP.
Recursion Damian Gordon. Recursion Factorial Fibonacci Decimal to Binary conversion Travelling Salesman Problem Knight’s Tour.
Introduction to Python Damian Gordon e:
DIVISIBILITY RULES. A number is divisible by: If the number ends in 0 the number is also divisible by 2 and 3 the sum of the number's digits.
Objective - To find the Least Common Multiple (LCM) of numerical and variable expressions and use it to compare and order fractions. Smallest number that.
Prime Numbers and Prime Factorization
Prime Numbers and Prime Factorization
3x + 2 6x3 - 5x2 – 12x – 4 2x2 – 3x – 2 6x3 + 4x2 -9x2 – 12x -9x2 – 6x
Introduction to Python
More on Functions (Part 2)
Pseudocode (Revision)
Factors and Primes.
Click the mouse button or press the Space Bar to display the answers.
Warm Up Problem of the Day Lesson Presentation Lesson Quizzes.
Prime Numbers and Prime Factorization
Linked Lists Damian Gordon.
Prime Numbers.
Exercise 24 ÷ 2 12.
Prime Numbers and Prime Factorization
A number that divides evenly into a larger number without a remainder
Review Basic Math Divisibility tests Prime and Composite Numbers
Factors and Simplest Forms
Practice working out numbers factors and multiples
Special Graphs of Inequalities.
Topic 1: Problem Solving
Special Graphs of Inequalities.
Prime Numbers and Prime Factorization
More on Functions (Part 2)
More on Functions (Part 2)
Some Common Issues: Iteration
Prime Numbers and Prime Factorization
Structured Programming
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Special Graphs of Inequalities.
Some Common Issues: Common Algorithms
Prime Factorization FACTOR TREE.
Prime Numbers and Prime Factorization
Section 2-1 Factors.
Prime Numbers and Prime Factorization
COMPUTING.
Presentation transcript:

Python: Algorithms 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. What’s a prime number? A number that’s only divisible by itself and 1, e.g. 7. Or to put it another way, every number other than itself and 1 gives a remainder, e.g. For 7, if 6, 5, 4, 3, and 2 give a remainder then 7 is prime. So all we need to do is divide 7 by all numbers less than it but greater than one, and if any of them have no remainder, we know it’s not prime.

Prime Numbers So, If the number is 7, as long as 6, 5, 4, 3, and 2 give a remainder, 7 is prime. If the number is 9, we know that 8, 7, 6, 5, and 4, all give remainders, but 3 does not give a remainder, it goes evenly into 9 so we can say 9 is not prime

Prime Numbers So remember, So, in general, if the number is 7, as long as 6, 5, 4, 3, and 2 give a remainder, 7 is prime. So, in general, if the number is A, as long as A-1, A-2, A-3, A-4, ... 2 give a remainder, A is prime.

# PROGRAM CheckPrime: a = int(input("Please input value:")) b = a - 1 IsPrime = True while b != 1: # DO if a % b == 0: # THEN IsPrime = False # ENDIF; b = b - 1 # ENDWHILE; if IsPrime: print(a, "is a prime number") else: print(a, "is not a prime number") # END.

Fibonacci Numbers The Fibonacci numbers are numbers where the next number in the sequence is the sum of the previous two. The sequence starts with 1, 1, And then it’s 2 Then 3 Then 5 Then 8 Then 13

# PROGRAM FibonacciNumbers: a = int(input("Please input value:")) FirstNum = 1 SecondNum = 1 while a != 1: # DO total = SecondNum + FirstNum FirstNum = SecondNum SecondNum = total a = a - 1 # ENDWHILE; print(total) # END.

etc.